   [conn entities]
   (let [entity-array (text-array conn entities)]
     (->> (concat
-           (fetch-dataset-ids conn {:ids entity-array})
-           (fetch-visualisation-ids conn {:ids entity-array})
-           (fetch-dashboard-ids conn {:ids entity-array})
-           (fetch-raster-dataset-ids conn {:ids entity-array}))
+           (db.collection/fetch-dataset-ids conn {:ids entity-array})
+           (db.collection/fetch-visualisation-ids conn {:ids entity-array})
+           (db.collection/fetch-dashboard-ids conn {:ids entity-array})
+           (db.collection/fetch-raster-dataset-ids conn {:ids entity-array}))
       (map #(merge {:dataset-id nil :visualisation-id nil :dashboard-id nil
                     :raster-dataset-id nil} %)))))
 


(alias 'core 'clojure.core)

(hugsql/def-db-fns "akvo/lumen/lib/collection.sql")

(defn all
  ([tenant-conn]
   (all tenant-conn nil))
  ([tenant-conn ids]
   (mapv (fn [collection]
           (core/update collection :entities #(vec (.getArray %))))
         (all-collections tenant-conn (if ids {:ids ids} {})))))

(defn fetch [tenant-conn id]
  (if-let [collection (fetch-collection tenant-conn {:id id})]
    (lib/ok (core/update collection :entities #(vec (.getArray %))))
    (lib/not-found {:id id})))


    :else
    (jdbc/with-db-transaction [tx-conn tenant-conn]
      (try
        (let [{:keys [id]} (db.collection/create-collection tenant-conn {:title title})]
          (when entities
            (doseq [entity (categorize-entities tx-conn entities)]
              (db.collection/insert-collection-entity tx-conn (assoc entity :collection-id id))))
          (lib/created (second (fetch tx-conn id))))
        (catch SQLException e
          (if (unique-violation? e)
