Browse Source

fix: unable to add a class property

Gabriel Horner 1 year ago
parent
commit
438763d530
1 changed files with 34 additions and 26 deletions
  1. 34 26
      src/main/frontend/handler/db_based/property.cljs

+ 34 - 26
src/main/frontend/handler/db_based/property.cljs

@@ -104,30 +104,32 @@
          :db/cardinality cardinality})))
 
 (defn upsert-property!
-  "If property-id is given, updates property or creates a new property"
+  "Updates property for property-id if it exists or creates a new property.
+   Two main  ways to create a property are to set property-id to a qualified keyword
+   or to set it to nil and pass :property-name as an option"
   [repo property-id schema {:keys [property-name properties]}]
-  (if property-id
-    (let [property (db/entity property-id)
-          tx-data (->>
-                   (conj
-                    [(cond->
-                      (merge
-                       (outliner-core/block-with-updated-at
-                        {:db/ident property-id
-                         :block/schema schema})
-                       properties)
-                       property-name
-                       (assoc :block/original-name property-name))]
-                    (update-schema property schema))
-                   (remove nil?))]
-      (db/transact! repo tx-data {:outliner-op :save-block}))
-    (let [k-name (or (and property-name (name property-name))
-                     (name property-id))
-          db-ident (db-property/get-db-ident-from-name property-name)]
-      (assert (some? k-name)
-              (prn "property-id: " property-id ", property-name: " property-name))
-      (db/transact! repo [(sqlite-util/build-new-property db-ident k-name schema)]
-                    {:outliner-op :new-property}))))
+  (let [db-ident (or property-id (db-property/get-db-ident-from-name property-name))]
+    (assert (qualified-keyword? db-ident))
+    (if-let [property (db/entity db-ident)]
+      (let [tx-data (->>
+                     (conj
+                      [(cond->
+                        (merge
+                         (outliner-core/block-with-updated-at
+                          {:db/ident db-ident
+                           :block/schema schema})
+                         properties)
+                         property-name
+                         (assoc :block/original-name property-name))]
+                      (update-schema property schema))
+                     (remove nil?))]
+        (db/transact! repo tx-data {:outliner-op :save-block}))
+      (let [k-name (or (and property-name (name property-name))
+                       (name property-id))]
+        (assert (some? k-name)
+                (prn "property-id: " property-id ", property-name: " property-name))
+        (db/transact! repo [(sqlite-util/build-new-property db-ident k-name schema)]
+                      {:outliner-op :new-property})))))
 
 (defn validate-property-value
   [schema value]
@@ -261,15 +263,21 @@
   [repo class-uuid property-id]
   (when-let [class (db/entity repo [:block/uuid class-uuid])]
     (when (contains? (:block/type class) "class")
-      (let [property (db/entity property-id)
+      (let [[db-ident property]
+            ;; strings come from user
+            (if (string? property-id)
+              (if-let [ent (db/entity [:block/original-name property-id])]
+                [(:db/ident ent) ent]
+                [(db-property/get-db-ident-from-name property-id) nil])
+              [property-id (db/entity property-id)])
             property-type (get-in property [:block/schema :type])
-            _ (upsert-property! repo property-id
+            _ (upsert-property! repo db-ident
                                 (cond-> (:block/schema property)
                                   (some? property-type)
                                   (assoc :type property-type))
                                 {})]
         (db/transact! repo
-                      [[:db/add (:db/id class) :class/schema.properties property-id]]
+                      [[:db/add (:db/id class) :class/schema.properties db-ident]]
                       {:outliner-op :save-block})))))
 
 (defn class-remove-property!