-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.cljc
87 lines (79 loc) · 3.39 KB
/
data.cljc
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
(ns sb.app.account.data
(:require
#?(:clj [sb.server.account :as server.account])
[re-db.api :as db]
[sb.app.entity.data :as entity.data]
[sb.authorize :as az]
[sb.query :as q]
[sb.schema :as sch :refer [?]]
[sb.util :as u]))
(sch/register!
{:account/email sch/unique-id-str
:account/email-verified? {:malli/schema :boolean}
:account/display-name {:malli/schema :string
:db/fulltext true}
:account.provider.google/sub sch/unique-id-str
:account/last-sign-in {:malli/schema 'inst?}
:account/password-hash {:malli/schema :string}
:account/password-salt {:malli/schema :string}
:account/locale {:malli/schema :i18n/locale}
:account/as-map {:malli/schema [:map {:closed true}
:entity/id
:account/email
:account/email-verified?
:entity/created-at
(? :account/locale)
(? :account/last-sign-in)
(? :account/display-name)
(? :account/password-hash)
(? :account/password-salt)
(? :image/avatar)
(? :account.provider.google/sub)]}})
(q/defquery account-orgs
{:endpoint {:query true}
:prepare az/with-account-id!}
[{:keys [account-id]}]
(into []
(comp (map :membership/entity)
(filter (comp #{:org} :entity/kind))
(map (q/pull entity.data/entity-keys)))
(db/where [[:membership/account account-id]])))
(q/defquery all
{:endpoint {:query true}
:prepare az/with-account-id!}
[{:keys [account-id]}]
(u/timed `all
(->> (q/pull '[{:membership/_account
[:membership/roles
{:membership/entity [:entity/id
:entity/kind
:entity/title
:entity/created-at
{:image/avatar [:entity/id]}
{:image/background [:entity/id]}]}]}]
account-id)
:membership/_account
(map #(u/lift-key % :membership/entity)))))
#?(:clj
(defn login!
{:endpoint {:post "/login"}
:endpoint/public? true}
[req params]
(server.account/login! req params)))
#?(:clj
(defn logout!
{:endpoint {:get "/logout"}
:endpoint/public? true}
[req params]
(server.account/logout! req params)))
#?(:clj
(defn google-landing
{:endpoint {:get "/oauth2/google/landing"}
:endpoint/public? true}
[req params]
(server.account/google-landing req params)))
(defn account-as-entity [account]
(u/select-as account {:entity/id :entity/id
:entity/kind :entity/kind
:account/display-name :entity/title
:image/avatar :image/avatar}))