-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmocks.clj
146 lines (136 loc) · 4.14 KB
/
mocks.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(ns back.api.db.mocks
(:require
[back.api.db.utils :as db.utils]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]
[com.moclojer.components.logs :as logs]))
(defn- cast-mock [mock]
(letfn [(cast-if [m status]
(if (get m status)
(update-in m [status] #(identity [:cast % :publication_status]))
m))]
(-> mock
(cast-if :mock/dns_status)
(cast-if :mock/unification_status))))
(comment
(cast-mock {:mock/dns_status "test"})
;;
)
(defn insert!
([mock db]
(insert! mock db {}))
([mock db ctx]
(logs/trace
::insert!
{:mock-id (:mock/id mock)}
(-> (sql.helpers/insert-into :mock)
(sql.helpers/values [(cast-mock mock)])
(sql.helpers/returning :*)
(sql/format {:quoted false})
((db.utils/build-execute-with-ctx db ctx))
first))))
(defn update!
([mock db]
(update! mock db {}))
([mock db ctx]
(logs/trace
::update!
{:mock-id (:mock/id mock)}
(-> (sql.helpers/update :mock)
(sql.helpers/set (cast-mock mock))
(sql.helpers/where [:= :id (:mock/id mock)])
(sql.helpers/returning :*)
(sql/format {:quoted false})
((db.utils/build-execute-with-ctx db ctx))
first))))
(defn get-mock-by-id
([id db]
(get-mock-by-id id db {}))
([id db ctx]
(logs/trace
::get-mock-by-id
{:mock-id id}
(-> (sql.helpers/select :*)
(sql.helpers/from :mock)
(sql.helpers/where [:= :id id])
sql/format
((db.utils/build-execute-with-ctx db ctx))
first))))
(defn get-mock-by-wildcard
([mock db]
(get-mock-by-wildcard mock db {}))
([{:mock/keys [wildcard subdomain user_id org_id]} db ctx]
(logs/trace
::get-mock-by-wildcard
{:mock-wildcard wildcard
:mock-subdomain subdomain
:user-id user_id
:org-id org_id}
(let [where [:and
[:= :wildcard wildcard]
[:= :subdomain subdomain]]
owner-where (if org_id
[:= :org_id org_id]
[:= :user_id user_id])]
(-> (sql.helpers/select :*)
(sql.helpers/from :mock)
(sql.helpers/where (if (or user_id org_id)
(conj where owner-where)
where))
sql/format
((db.utils/build-execute-with-ctx db ctx))
first)))))
(defn get-mocks
([user-id db]
(get-mocks user-id db {}))
([user-id db ctx]
(logs/trace
::get-mocks
{:user-id user-id}
(-> (sql.helpers/select :m.*)
(sql.helpers/from [:mock :m])
(sql.helpers/left-join [:org_user :ou] [:= :ou.org_id :m.org_id])
(sql.helpers/where [:or
[:= :m.user_id user-id]
[:= :ou.user_id user-id]])
sql/format
((db.utils/build-execute-with-ctx db ctx))))))
(defn get-org-mocks
([org-id db]
(get-org-mocks org-id db {}))
([org-id db ctx]
(-> (sql.helpers/select :*)
(sql.helpers/from :mock)
(sql.helpers/where [:= :org_id org-id])
sql/format
((db.utils/build-execute-with-ctx db ctx)))))
(defn get-mocks-by-publication
[stt-type pub-stts db ctx & [user-id]]
(let [where-pub-status (->> (map
#(identity [:= stt-type [:cast % :publication_status]])
pub-stts)
vec
(into [:or]))]
(logs/trace
::get-mock-by-publication
{:status-type stt-type
:pub-status pub-stts
:user-id user-id}
(-> (sql.helpers/select :*)
(sql.helpers/from :mock)
(sql.helpers/where (if (some? user-id)
(conj [:and [:= :user_id user-id]] where-pub-status)
where-pub-status))
sql/format
((db.utils/build-execute-with-ctx db ctx))))))
(defn delete-mock-by-id
([id db]
(delete-mock-by-id id db {}))
([id db ctx]
(logs/trace
::delete-mock-by-id
{:mock-id id}
(-> (sql.helpers/delete-from :mock)
(sql.helpers/where [:= :id id])
sql/format
((db.utils/build-execute-with-ctx db ctx))))))