|
1 | 1 | import { User } from "~/src/graphql/types/User/User";
|
2 | 2 | import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
|
| 3 | +import type { GraphQLContext } from "../../context"; |
3 | 4 | import { Event } from "./Event";
|
| 5 | +import type { Event as EventType } from "./Event"; |
4 | 6 |
|
5 |
| -Event.implement({ |
6 |
| - fields: (t) => ({ |
7 |
| - creator: t.field({ |
8 |
| - description: "User who created the event.", |
9 |
| - resolve: async (parent, _args, ctx) => { |
10 |
| - if (!ctx.currentClient.isAuthenticated) { |
11 |
| - throw new TalawaGraphQLError({ |
12 |
| - extensions: { |
13 |
| - code: "unauthenticated", |
14 |
| - }, |
15 |
| - }); |
16 |
| - } |
| 7 | +export const eventCreatorResolver = async ( |
| 8 | + parent: EventType, |
| 9 | + _args: unknown, |
| 10 | + ctx: GraphQLContext, |
| 11 | +) => { |
| 12 | + if (!ctx.currentClient.isAuthenticated) { |
| 13 | + throw new TalawaGraphQLError({ |
| 14 | + extensions: { |
| 15 | + code: "unauthenticated", |
| 16 | + }, |
| 17 | + }); |
| 18 | + } |
17 | 19 |
|
18 |
| - const currentUserId = ctx.currentClient.user.id; |
| 20 | + try { |
| 21 | + const currentUserId = ctx.currentClient.user.id; |
19 | 22 |
|
20 |
| - const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
21 |
| - with: { |
22 |
| - organizationMembershipsWhereMember: { |
23 |
| - columns: { |
24 |
| - role: true, |
25 |
| - }, |
26 |
| - where: (fields, operators) => |
27 |
| - operators.eq(fields.organizationId, parent.organizationId), |
28 |
| - }, |
| 23 | + const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 24 | + with: { |
| 25 | + organizationMembershipsWhereMember: { |
| 26 | + columns: { |
| 27 | + role: true, |
29 | 28 | },
|
30 |
| - where: (fields, operators) => operators.eq(fields.id, currentUserId), |
31 |
| - }); |
| 29 | + where: (fields, operators) => |
| 30 | + operators.eq(fields.organizationId, parent.organizationId), |
| 31 | + }, |
| 32 | + }, |
| 33 | + where: (fields, operators) => operators.eq(fields.id, currentUserId), |
| 34 | + }); |
32 | 35 |
|
33 |
| - if (currentUser === undefined) { |
34 |
| - throw new TalawaGraphQLError({ |
35 |
| - extensions: { |
36 |
| - code: "unauthenticated", |
37 |
| - }, |
38 |
| - }); |
39 |
| - } |
| 36 | + if (currentUser === undefined) { |
| 37 | + throw new TalawaGraphQLError({ |
| 38 | + extensions: { |
| 39 | + code: "unauthenticated", |
| 40 | + }, |
| 41 | + }); |
| 42 | + } |
40 | 43 |
|
41 |
| - const currentUserOrganizationMembership = |
42 |
| - currentUser.organizationMembershipsWhereMember[0]; |
| 44 | + const currentUserOrganizationMembership = |
| 45 | + currentUser.organizationMembershipsWhereMember[0]; |
43 | 46 |
|
44 |
| - if ( |
45 |
| - currentUser.role !== "administrator" && |
46 |
| - (currentUserOrganizationMembership === undefined || |
47 |
| - currentUserOrganizationMembership.role !== "administrator") |
48 |
| - ) { |
49 |
| - throw new TalawaGraphQLError({ |
50 |
| - extensions: { |
51 |
| - code: "unauthorized_action", |
52 |
| - }, |
53 |
| - }); |
54 |
| - } |
| 47 | + if ( |
| 48 | + currentUser.role !== "administrator" && |
| 49 | + (currentUserOrganizationMembership === undefined || |
| 50 | + currentUserOrganizationMembership.role !== "administrator") |
| 51 | + ) { |
| 52 | + throw new TalawaGraphQLError({ |
| 53 | + extensions: { |
| 54 | + code: "unauthorized_action", |
| 55 | + }, |
| 56 | + }); |
| 57 | + } |
55 | 58 |
|
56 |
| - if (parent.creatorId === null) { |
57 |
| - return null; |
58 |
| - } |
| 59 | + if (parent.creatorId === null) { |
| 60 | + return null; |
| 61 | + } |
59 | 62 |
|
60 |
| - if (parent.creatorId === currentUserId) { |
61 |
| - return currentUser; |
62 |
| - } |
| 63 | + if (parent.creatorId === currentUserId) { |
| 64 | + return currentUser; |
| 65 | + } |
63 | 66 |
|
64 |
| - const creatorId = parent.creatorId; |
| 67 | + const creatorId = parent.creatorId; |
65 | 68 |
|
66 |
| - const existingUser = await ctx.drizzleClient.query.usersTable.findFirst( |
67 |
| - { |
68 |
| - where: (fields, operators) => operators.eq(fields.id, creatorId), |
69 |
| - }, |
70 |
| - ); |
| 69 | + const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 70 | + where: (fields, operators) => operators.eq(fields.id, creatorId), |
| 71 | + }); |
71 | 72 |
|
72 |
| - // Creator id existing but the associated user not existing is a business logic error and probably means that the corresponding data in the database is in a corrupted state. It must be investigated and fixed as soon as possible to prevent additional data corruption. |
73 |
| - if (existingUser === undefined) { |
74 |
| - ctx.log.error( |
75 |
| - "Postgres select operation returned an empty array for an event's creator id that isn't null.", |
76 |
| - ); |
| 73 | + // Creator id existing but the associated user not existing is a business logic error and probably means that the corresponding data in the database is in a corrupted state. It must be investigated and fixed as soon as possible to prevent additional data corruption. |
| 74 | + if (existingUser === undefined) { |
| 75 | + ctx.log.error( |
| 76 | + "Postgres select operation returned an empty array for an event's creator id that isn't null.", |
| 77 | + ); |
77 | 78 |
|
78 |
| - throw new TalawaGraphQLError({ |
79 |
| - extensions: { |
80 |
| - code: "unexpected", |
81 |
| - }, |
82 |
| - }); |
83 |
| - } |
| 79 | + throw new TalawaGraphQLError({ |
| 80 | + extensions: { |
| 81 | + code: "unexpected", |
| 82 | + }, |
| 83 | + }); |
| 84 | + } |
84 | 85 |
|
85 |
| - return existingUser; |
| 86 | + return existingUser; |
| 87 | + } catch (error) { |
| 88 | + if (error instanceof TalawaGraphQLError) { |
| 89 | + throw error; |
| 90 | + } |
| 91 | + ctx.log.error(error); |
| 92 | + throw new TalawaGraphQLError({ |
| 93 | + message: "Internal server error", |
| 94 | + extensions: { |
| 95 | + code: "unexpected", |
86 | 96 | },
|
| 97 | + }); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +Event.implement({ |
| 102 | + fields: (t) => ({ |
| 103 | + creator: t.field({ |
| 104 | + description: "User who created the event.", |
| 105 | + resolve: eventCreatorResolver, |
87 | 106 | type: User,
|
88 | 107 | }),
|
89 | 108 | }),
|
|
0 commit comments