-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutations.py
More file actions
368 lines (296 loc) · 12 KB
/
mutations.py
File metadata and controls
368 lines (296 loc) · 12 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
Mutations for Clubs
"""
import strawberry
from fastapi.encoders import jsonable_encoder
from db import clubsdb
from models import Club, create_utc_time
# import all models and types
from otypes import (
FullClubInput,
FullClubType,
Info,
SimpleClubInput,
SimpleClubType,
)
from utils import (
check_remove_old_file,
getUser,
invalidate_active_clubs_cache,
invalidate_club_cache,
update_events_members_cid,
update_role,
)
@strawberry.mutation
async def createClub(clubInput: FullClubInput, info: Info) -> SimpleClubType:
"""
Mutation for creation of a new club by CC.
Args:
clubInput (otypes.FullClubInput): Full details of the club.
info (otypes.Info): User metadata and cookies.
Returns:
(otypes.SimpleClubType): Details of the created club.
Raises:
Exception: Not Authenticated
Exception: A club with this cid already exists
Exception: A club with this short code already exists
Exception: Invalid Club ID/Club Email
Exception: Error in updating the role for the club
Exception: Not Authenticated to access this API
"""
user = info.context.user
if user is None:
raise Exception("Not Authenticated")
role = user["role"]
club_input = jsonable_encoder(clubInput.to_pydantic())
if role in ["cc"]:
club_input["cid"] = club_input["email"].split("@")[0]
cid_exists = await clubsdb.find_one({"cid": club_input["cid"]})
if cid_exists:
raise Exception("A club with this cid already exists")
# Check whether this cid is valid or not
clubMember = await getUser(club_input["cid"], info.context.cookies)
if clubMember is None:
raise Exception("Invalid Club ID/Club Email")
code_exists = await clubsdb.find_one({"code": club_input["code"]})
if code_exists:
raise Exception("A club with this short code already exists")
created_record = await clubsdb.insert_one(club_input)
created_sample = Club.model_validate(
await clubsdb.find_one({"_id": created_record.inserted_id})
)
if not await update_role(club_input["cid"], info.context.cookies):
raise Exception("Error in updating the role for the club")
await invalidate_active_clubs_cache()
return SimpleClubType.from_pydantic(created_sample)
else:
raise Exception("Not Authenticated to access this API")
@strawberry.mutation
async def editClub(clubInput: FullClubInput, info: Info) -> FullClubType:
"""
Mutation for editing of the club details either by that specific club or
the cc
This method is used for editing the club details.
CC can edit any club details, but the club can only edit its own details.
Only CC can change a clubs name/email and category.
Args:
clubInput (otypes.FullClubInput): Full details of the club to be updated to.
info (otypes.Info): User metadata and cookies.
Returns:
(otypes.FullClubType): Full Details of the edited club.
Raises:
Exception: Not Authenticated.
Exception: A club with this code does not exist.
Exception: Invalid Club ID/Club Email.
Exception: Error in updating the role/cid.
Exception: Authentication Error! (CLUB ID CHANGED).
Exception: You dont have permission to change the name/email of the
club. Please contact CC for it.
Exception: Only CC is allowed to change the category of club.
Exception: Not Authenticated to access this API.
""" # noqa: E501
user = info.context.user
if user is None:
raise Exception("Not Authenticated")
role = user["role"]
uid = user["uid"]
club_input = jsonable_encoder(clubInput.to_pydantic())
if role in ["cc"]:
exists = await clubsdb.find_one({"code": club_input["code"]})
if not exists:
raise Exception("A club with this code doesn't exist")
# Check whether this cid is valid or not
clubMember = await getUser(club_input["cid"], info.context.cookies)
if clubMember is None:
raise Exception("Invalid Club ID/Club Email")
club_input["state"] = exists["state"]
club_input["_id"] = exists["_id"]
await check_remove_old_file(exists, club_input, "logo")
await check_remove_old_file(exists, club_input, "banner")
await check_remove_old_file(exists, club_input, "banner_square")
await clubsdb.replace_one({"code": club_input["code"]}, club_input)
if "socials" in club_input.keys():
await clubsdb.update_one(
{"code": club_input["code"]},
{
"$set": {
"socials.website": club_input["socials"]["website"],
"socials.instagram": club_input["socials"][
"instagram"
],
"socials.facebook": club_input["socials"]["facebook"],
"socials.youtube": club_input["socials"]["youtube"],
"socials.twitter": club_input["socials"]["twitter"],
"socials.linkedin": club_input["socials"]["linkedin"],
"socials.discord": club_input["socials"]["discord"],
"socials.whatsapp": club_input["socials"]["whatsapp"],
"socials.other_links": club_input["socials"][
"other_links"
],
}
},
)
await clubsdb.update_one(
{"code": club_input["code"]},
{
"$set": {
"created_time": exists["created_time"],
"updated_time": create_utc_time(),
}
},
)
await invalidate_club_cache(club_input["cid"])
await invalidate_active_clubs_cache()
if exists["cid"] != club_input["cid"]:
await invalidate_club_cache(exists["cid"])
return1 = await update_role(
exists["cid"], info.context.cookies, role="public"
)
return2 = await update_role(
club_input["cid"], info.context.cookies, role="club"
)
return3 = await update_events_members_cid(
exists["cid"], club_input["cid"], cookies=info.context.cookies
)
if not return1 or not return2 or not return3:
raise Exception("Error in updating the role/cid.")
result = Club.model_validate(
await clubsdb.find_one({"code": club_input["code"]})
)
return FullClubType.from_pydantic(result)
elif role in ["club"]:
if uid != club_input["cid"]:
raise Exception("Authentication Error! (CLUB ID CHANGED)")
exists = await clubsdb.find_one({"cid": club_input["cid"]})
if not exists:
raise Exception("A club with this cid doesn't exist")
if (
club_input["name"] != exists["name"]
or club_input["email"] != exists["email"]
):
raise Exception(
"You don't have permission to change the name/email of the"
"club. Please contact CC for it" # noqa: E501
)
if club_input["category"] != exists["category"]:
raise Exception(
"Only CC is allowed to change the category of club."
)
club_input["state"] = exists["state"]
club_input["_id"] = exists["_id"]
await check_remove_old_file(exists, club_input, "logo")
await check_remove_old_file(exists, club_input, "banner")
await check_remove_old_file(exists, club_input, "banner_square")
await clubsdb.replace_one({"cid": uid}, club_input)
if "socials" in club_input.keys():
await clubsdb.update_one(
{"cid": club_input["cid"]},
{
"$set": {
"socials.website": club_input["socials"]["website"],
"socials.instagram": club_input["socials"][
"instagram"
],
"socials.facebook": club_input["socials"]["facebook"],
"socials.youtube": club_input["socials"]["youtube"],
"socials.twitter": club_input["socials"]["twitter"],
"socials.linkedin": club_input["socials"]["linkedin"],
"socials.discord": club_input["socials"]["discord"],
"socials.whatsapp": club_input["socials"]["whatsapp"],
"socials.other_links": club_input["socials"][
"other_links"
],
}
},
)
# also autofills the updated time
await clubsdb.update_one(
{"cid": club_input["cid"]},
{
"$set": {
"created_time": exists["created_time"],
"updated_time": create_utc_time(),
}
},
)
await invalidate_club_cache(club_input["cid"])
await invalidate_active_clubs_cache()
result = Club.model_validate(
await clubsdb.find_one({"cid": club_input["cid"]})
)
return FullClubType.from_pydantic(result)
else:
raise Exception("Not Authenticated to access this API")
@strawberry.mutation
async def deleteClub(clubInput: SimpleClubInput, info: Info) -> SimpleClubType:
"""
Mutation for the cc to move a club to deleted state.
Args:
clubInput (otypes.SimpleClubInput): The club cid.
info (otypes.Info): User metadata and cookies.
Returns:
(otypes.SimpleClubType): Details of the deleted club.
Raises:
Exception: Not Authenticated.
Exception: Not Authenticated to access this API.
"""
user = info.context.user
if user is None:
raise Exception("Not Authenticated")
role = user["role"]
club_input = jsonable_encoder(clubInput)
if role not in ["cc"]:
raise Exception("Not Authenticated to access this API")
# also autofills the updated time
await clubsdb.update_one(
{"cid": club_input["cid"]},
{"$set": {"state": "deleted", "updated_time": create_utc_time()}},
)
await update_role(club_input["cid"], info.context.cookies, "public")
updated_sample = Club.model_validate(
await clubsdb.find_one({"cid": club_input["cid"]})
)
await invalidate_active_clubs_cache()
await invalidate_club_cache(club_input["cid"])
return SimpleClubType.from_pydantic(updated_sample)
@strawberry.mutation
async def restartClub(
clubInput: SimpleClubInput, info: Info
) -> SimpleClubType:
"""
Mutation for cc to move a club from deleted state to active state.
Args:
clubInput (otypes.SimpleClubInput): The club cid.
info (otypes.Info): User metadata and cookies.
Returns:
(otypes.SimpleClubType): Details of the restarted clubs cid.
Raises:
Exception: Not Authenticated.
Exception: Not Authenticated to access this API.
"""
user = info.context.user
if user is None:
raise Exception("Not Authenticated")
role = user["role"]
club_input = jsonable_encoder(clubInput)
if role not in ["cc"]:
raise Exception("Not Authenticated to access this API")
# also autofills the updated time
await clubsdb.update_one(
{"cid": club_input["cid"]},
{"$set": {"state": "active", "updated_time": create_utc_time()}},
)
await update_role(club_input["cid"], info.context.cookies, "club")
updated_sample = Club.model_validate(
await clubsdb.find_one({"cid": club_input["cid"]})
)
await invalidate_active_clubs_cache()
await invalidate_club_cache(club_input["cid"])
return SimpleClubType.from_pydantic(updated_sample)
# register all mutations
mutations = [
createClub,
editClub,
deleteClub,
restartClub,
]