Line data Source code
1 4 : (ns matchscore.db.postgres.payment-methods
2 : (:require
3 : [honey.sql :as sql]
4 : [next.jdbc :as jdbc]
5 : [next.jdbc.result-set :as rs]))
6 :
7 :
8 4 : (def ^:private columns
9 52 : [:uuid
10 : :user-id
11 : :kind
12 : :label
13 : :value
14 : :note
15 : :bank-id
16 : :bank-name
17 : :recipient-name
18 : :tax-id
19 : :purpose
20 : :position])
21 :
22 :
23 4 : (def ^:private base-query
24 16 : {:select columns
25 8 : :from [:user-payment-methods]})
26 :
27 :
28 16 : (def ^:private jdbc-options {:builder-fn rs/as-unqualified-kebab-maps})
29 :
30 :
31 4 : (defn- row->method
32 : [row]
33 6 : (when row
34 4 : (->
35 2 : row
36 8 : (update :uuid str)
37 4 : (update :user-id str))))
38 :
39 :
40 18 : (defn- execute! [ds query] (jdbc/execute! ds (sql/format query) jdbc-options))
41 :
42 :
43 4 : (defn list-by-user
44 : [ds user-id]
45 1 : (->>
46 2 : (execute!
47 1 : ds
48 5 : (assoc base-query
49 13 : :where [:and [:= :user-id [:cast user-id :uuid]] [:is :deleted-at nil]]
50 7 : :order-by [[:position :asc] [:created-datetime :asc]]))
51 2 : (mapv row->method)))
52 :
53 :
54 4 : (defn find-live-by-ids
55 : [ds ids]
56 12 : (if (empty? ids)
57 2 : []
58 2 : (->>
59 4 : (execute!
60 2 : ds
61 10 : (assoc base-query
62 36 : :where [:and [:in :uuid (mapv #(vector :cast % :uuid) ids)] [:is :deleted-at nil]]
63 14 : :order-by [[:position :asc] [:created-datetime :asc]]))
64 4 : (mapv row->method))))
65 :
66 :
67 4 : (defn owner-names
68 : "Display names of the given users, keyed by user UUID string."
69 : [ds user-ids]
70 4 : (if (empty? user-ids)
71 0 : {}
72 1 : (->>
73 2 : (execute!
74 1 : ds
75 7 : {:select [:uuid :name]
76 2 : :from [:users]
77 14 : :where [:in :uuid (mapv #(vector :cast % :uuid) (distinct user-ids))]})
78 11 : (into {} (map (juxt (comp str :uuid) :name))))))
79 :
80 :
81 4 : (defn grouped-by-owner
82 : "Orders methods so each owner's methods stay together, owners in the order
83 : their first method was selected and methods in each owner's own order."
84 : [selected-ids methods]
85 8 : (let [index (into
86 2 : {}
87 4 : (map-indexed
88 2 : (fn [i id]
89 10 : [(str id) i]))
90 6 : (distinct selected-ids))
91 12 : rank #(get index (:uuid %) Long/MAX_VALUE)
92 4 : owner-index (reduce
93 2 : (fn [acc method]
94 26 : (update acc (:user-id method) (fnil min Long/MAX_VALUE) (rank method)))
95 2 : {}
96 2 : methods)]
97 26 : (vec (sort-by (juxt (comp owner-index :user-id) :position rank) methods))))
98 :
99 :
100 4 : (defn create!
101 : [ds user-id {:keys [kind label value note bank-id bank-name recipient-name tax-id purpose]}]
102 2 : (let [next-position-query
103 26 : {:select [[[:coalesce [:max :position] 0] :max-position]]
104 4 : :from [:user-payment-methods]
105 26 : :where [:and [:= :user-id [:cast user-id :uuid]] [:is :deleted-at nil]]}]
106 8 : (jdbc/with-transaction
107 2 : [tx ds]
108 18 : (let [max-position (:max-position (first (execute! tx next-position-query)))
109 10 : query {:insert-into :user-payment-methods
110 34 : :values [{:user-id [:cast user-id :uuid]
111 2 : :kind kind
112 2 : :label label
113 2 : :value value
114 2 : :note note
115 2 : :bank-id bank-id
116 2 : :bank-name bank-name
117 2 : :recipient-name recipient-name
118 2 : :tax-id tax-id
119 2 : :purpose purpose
120 4 : :position (inc max-position)}]
121 2 : :returning columns}]
122 30 : (some->
123 8 : (execute! tx query)
124 2 : first
125 2 : row->method)))))
126 :
127 :
128 4 : (defn update!
129 : [ds user-id uuid {:keys [kind label value note bank-id bank-name recipient-name tax-id purpose]}]
130 16 : (some->
131 2 : (execute!
132 1 : ds
133 6 : {:update :user-payment-methods
134 11 : :set {:kind kind
135 1 : :label label
136 1 : :value value
137 1 : :note note
138 1 : :bank-id bank-id
139 1 : :bank-name bank-name
140 1 : :recipient-name recipient-name
141 1 : :tax-id tax-id
142 1 : :purpose purpose}
143 2 : :where [:and
144 7 : [:= :uuid [:cast uuid :uuid]]
145 7 : [:= :user-id [:cast user-id :uuid]]
146 4 : [:is :deleted-at nil]]
147 1 : :returning columns})
148 1 : first
149 1 : row->method))
150 :
151 :
152 4 : (defn soft-delete!
153 : [ds user-id uuid]
154 1 : (boolean
155 2 : (seq
156 2 : (execute!
157 1 : ds
158 6 : {:update :user-payment-methods
159 4 : :set {:deleted-at [:now]}
160 2 : :where [:and
161 7 : [:= :uuid [:cast uuid :uuid]]
162 7 : [:= :user-id [:cast user-id :uuid]]
163 4 : [:is :deleted-at nil]]
164 2 : :returning [:uuid]}))))
165 :
166 :
167 4 : (defn reorder!
168 : [ds user-id uuids]
169 4 : (jdbc/with-transaction
170 1 : [tx ds]
171 2 : (let [existing (->>
172 2 : (execute!
173 1 : tx
174 6 : (assoc base-query
175 13 : :where [:and [:= :user-id [:cast user-id :uuid]] [:is :deleted-at nil]]
176 : :for :update))
177 2 : (mapv row->method))]
178 21 : (when (and (= (count existing) (count uuids)) (= (set (map :uuid existing)) (set uuids)))
179 29 : (doseq [[position uuid] (map-indexed vector uuids)]
180 2 : (execute!
181 1 : tx
182 5 : {:update :user-payment-methods
183 3 : :set {:position position}
184 16 : :where [:and [:= :uuid [:cast uuid :uuid]] [:= :user-id [:cast user-id :uuid]]]}))
185 4 : (list-by-user tx user-id)))))
|