-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.ts
221 lines (205 loc) · 4.43 KB
/
storage.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as AWS from 'aws-sdk'
/* USERS */
export type User = {
githubId: string
email: string
login: string
stripeId?: string
valid: boolean
validEnterprise: boolean
createdAt: number
lastSeenAt: number
orgs: string[]
}
/* ORGS */
export type Org = {
id: string
admins: string[]
members: string[]
stripeId?: string
valid: boolean
validEnterprise: boolean
createdAt: number
prepaidFor?: number
}
const db = new AWS.DynamoDB.DocumentClient()
export const findOne = async (githubId: string): Promise<User | void> => {
if (!githubId) {
return undefined
}
const meta = await db
.get({
TableName: process.env.USERS_TABLE_NAME,
Key: {
githubId: String(githubId),
},
})
.promise()
// @ts-ignore
return (meta || {}).Item
}
export const findOneByName = async (
githubUsername: string
): Promise<Pick<User, 'githubId' | 'login'> | void> => {
if (!githubUsername) {
return undefined
}
const meta = await db
.query({
TableName: process.env.USERS_TABLE_NAME,
IndexName: 'indexByLogin',
ProjectionExpression: 'githubId, login',
KeyConditionExpression: 'login = :githubUsername',
ExpressionAttributeValues: {
':githubUsername': String(githubUsername),
},
})
.promise()
// @ts-ignore
return ((meta || {}).Items || [])[0]
}
export const update = async (data: User, body?: Partial<User>) => {
data.lastSeenAt = Date.now()
if (body) {
data = Object.assign(data, body)
}
await db
.put({
TableName: process.env.USERS_TABLE_NAME,
Item: data,
})
.promise()
return data
}
export const create = async (
data: Pick<User, 'githubId' | 'email' | 'login'>
) => {
// @ts-ignore
delete data.enterprise
const user: User = {
valid: false,
validEnterprise: false,
createdAt: Date.now(),
lastSeenAt: Date.now(),
orgs: [],
...data,
}
await db
.put({
TableName: process.env.USERS_TABLE_NAME,
Item: user,
})
.promise()
return user
}
export const findOneOrg = async (id: string): Promise<Org | void> => {
if (!id) {
return undefined
}
const meta = await db
.get({
TableName: process.env.ORGS_TABLE_NAME,
Key: {
id: String(id),
},
})
.promise()
// @ts-ignore
return (meta || {}).Item
}
export const findOrgs = async (
ids: string[]
): Promise<
Pick<
Org,
'id' | 'members' | 'admins' | 'stripeId' | 'valid' | 'validEnterprise'
>[]
> => {
if (!ids || !ids.length) {
return []
}
return Promise.all(
ids.map(id =>
db
.get({
TableName: process.env.ORGS_TABLE_NAME,
ProjectionExpression:
'id, members, admins, stripeId, valid, validEnterprise, prepaidFor',
Key: {
id: String(id),
},
})
.promise()
.then(meta => (meta || {}).Item as Org)
)
).then(x => x.filter(y => !!y))
}
export const addUserToOrg = async (org: Org, user: User) => {
org.members = (org.members || []).concat(String(user.githubId))
user.orgs = (user.orgs || []).concat(String(org.id))
await Promise.all([
db
.put({
TableName: process.env.USERS_TABLE_NAME,
Item: user,
})
.promise(),
db
.put({
TableName: process.env.ORGS_TABLE_NAME,
Item: org,
})
.promise(),
])
return org
}
export const removeUserFromOrg = async (org: Org, user: User) => {
org.members = (org.members || []).filter(u => u !== String(user.githubId))
user.orgs = (user.orgs || []).filter(o => o !== String(org.id))
await Promise.all([
db
.put({
TableName: process.env.USERS_TABLE_NAME,
Item: user,
})
.promise(),
db
.put({
TableName: process.env.ORGS_TABLE_NAME,
Item: org,
})
.promise(),
])
return org
}
export const createOrg = async (
data: Pick<Org, 'id' | 'admins' | 'members'>
) => {
// @ts-ignore
delete data.enterprise
const org: Org = {
createdAt: Date.now(),
valid: false,
validEnterprise: false,
...data,
}
await db
.put({
TableName: process.env.ORGS_TABLE_NAME,
Item: org,
})
.promise()
return org
}
export const updateOrg = async (data: Org, body?: Partial<Org>) => {
if (body) {
data = Object.assign(data, body)
}
await db
.put({
TableName: process.env.ORGS_TABLE_NAME,
Item: data,
})
.promise()
return data
}