<commit_before>(ns braid.server.quests.db
  (:require [datomic.api :as d]
            [braid.server.db.common :refer [quest-record-pull-pattern]]))

; Queries

(defn get-active-quests-for-user-id [conn user-id]
  (->> (d/q '[:find (pull ?qr pull-pattern)
              :in $ ?user-id pull-pattern
              :where
              [?qr :quest-record/user ?u]
              [?u :user/id ?user-id]
              [?qr :quest-record/state :active]]
            (d/db conn)
            user-id
            quest-record-pull-pattern)
       (map first)))

; Transactions

(defn upsert-quest-record! [conn user-id quest-record]
  (let [db-id (if (d/entity (d/db conn) [:quest-record/id (quest-record :id)])
                [:quest-record/id (quest-record :quest-record/id)]
                (d/tempid :entities))]
    @(d/transact conn [(assoc quest-record :db/id db-id)])))

(defn activate-first-quests! [conn user-id]
  (let [txs (->> [:quest/quest-complete :quest/conversation-new :quest/conversation-reply]
                 (map (fn [quest-id]
                        {:db/id (d/tempid :entities)
                         :quest-record/id (d/squuid)
                         :quest-record/user [:user/id user-id]
                         :quest-record/quest-id quest-id
                         :quest-record/progress 0
                         :quest-record/state :active})))]
    @(d/transact conn txs)))
<commit_msg>Oops, commit changes for quest db<commit_after>(ns braid.server.quests.db
  (:require [datomic.api :as d]
            [braid.server.db :as db]
            [braid.server.db.common :refer [quest-record-pull-pattern]]))

; Queries

(defn get-active-quests-for-user-id
  [user-id]
  (->> (d/q '[:find (pull ?qr pull-pattern)
              :in $ ?user-id pull-pattern
              :where
              [?qr :quest-record/user ?u]
              [?u :user/id ?user-id]
              [?qr :quest-record/state :active]]
            (db/db)
            user-id
            quest-record-pull-pattern)
       (map first)))

; Transactions

(defn upsert-quest-record-txn
  [user-id quest-record]
  (let [db-id (if (d/entity (db/db) [:quest-record/id (quest-record :id)])
                [:quest-record/id (quest-record :quest-record/id)]
                (d/tempid :entities))]
    [(assoc quest-record :db/id db-id)]))

(defn activate-first-quests-txn
  [user-id]
  (->> [:quest/quest-complete :quest/conversation-new :quest/conversation-reply]
       (map (fn [quest-id]
              {:db/id (d/tempid :entities)
               :quest-record/id (d/squuid)
               :quest-record/user [:user/id user-id]
               :quest-record/quest-id quest-id
               :quest-record/progress 0
               :quest-record/state :active}))))
{'fraction_non_alphanumeric': 0.15860312841033103, 'fraction_numerical': 0.0007275372862859222, 'mean_word_length': 1.7834008097165992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3493081', 'n_tokens_mistral': 943, 'n_tokens_neox': 922, 'n_words': 182}