|
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 { Organization } from "./Organization";
|
| 5 | +import type { Organization as Organizationtype } from "./Organization"; |
4 | 6 |
|
5 |
| -Organization.implement({ |
6 |
| - fields: (t) => ({ |
7 |
| - creator: t.field({ |
8 |
| - description: "User who created the organization.", |
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 OrganizationCreatorResolver = async ( |
| 8 | + parent: Organizationtype, |
| 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({ |
| 23 | + const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 24 | + columns: { |
| 25 | + role: true, |
| 26 | + }, |
| 27 | + with: { |
| 28 | + organizationMembershipsWhereMember: { |
21 | 29 | columns: {
|
22 | 30 | role: true,
|
23 | 31 | },
|
24 |
| - with: { |
25 |
| - organizationMembershipsWhereMember: { |
26 |
| - columns: { |
27 |
| - role: true, |
28 |
| - }, |
29 |
| - where: (fields, operators) => |
30 |
| - operators.eq(fields.organizationId, parent.id), |
31 |
| - }, |
32 |
| - }, |
33 |
| - where: (fields, operators) => operators.eq(fields.id, currentUserId), |
34 |
| - }); |
| 32 | + where: (fields, operators) => |
| 33 | + operators.eq(fields.organizationId, parent.id), |
| 34 | + }, |
| 35 | + }, |
| 36 | + where: (fields, operators) => operators.eq(fields.id, currentUserId), |
| 37 | + }); |
35 | 38 |
|
36 |
| - if (currentUser === undefined) { |
37 |
| - throw new TalawaGraphQLError({ |
38 |
| - extensions: { |
39 |
| - code: "unauthenticated", |
40 |
| - }, |
41 |
| - }); |
42 |
| - } |
| 39 | + if (currentUser === undefined) { |
| 40 | + throw new TalawaGraphQLError({ |
| 41 | + extensions: { |
| 42 | + code: "unauthenticated", |
| 43 | + }, |
| 44 | + }); |
| 45 | + } |
43 | 46 |
|
44 |
| - const currentUserOrganizationMembership = |
45 |
| - currentUser.organizationMembershipsWhereMember[0]; |
| 47 | + const currentUserOrganizationMembership = |
| 48 | + currentUser.organizationMembershipsWhereMember[0]; |
46 | 49 |
|
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 |
| - } |
| 50 | + if ( |
| 51 | + currentUser.role !== "administrator" && |
| 52 | + (currentUserOrganizationMembership === undefined || |
| 53 | + currentUserOrganizationMembership.role !== "administrator") |
| 54 | + ) { |
| 55 | + throw new TalawaGraphQLError({ |
| 56 | + extensions: { |
| 57 | + code: "unauthorized_action", |
| 58 | + }, |
| 59 | + }); |
| 60 | + } |
58 | 61 |
|
59 |
| - if (parent.creatorId === null) { |
60 |
| - return null; |
61 |
| - } |
| 62 | + if (parent.creatorId === null) { |
| 63 | + return null; |
| 64 | + } |
62 | 65 |
|
63 |
| - const creatorId = parent.creatorId; |
64 |
| - const existingUser = await ctx.drizzleClient.query.usersTable.findFirst( |
65 |
| - { |
66 |
| - where: (fields, operators) => operators.eq(fields.id, creatorId), |
67 |
| - }, |
68 |
| - ); |
| 66 | + const creatorId = parent.creatorId; |
| 67 | + const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 68 | + where: (fields, operators) => operators.eq(fields.id, creatorId), |
| 69 | + }); |
69 | 70 |
|
70 |
| - // Creator id existing but the associated user not existing is either a business logic error which means that the corresponding data in the database is in a corrupted state or it is a rare race condition. It must be investigated and fixed as soon as possible to prevent further data corruption if the former case is true. |
71 |
| - if (existingUser === undefined) { |
72 |
| - ctx.log.warn( |
73 |
| - "Postgres select operation returned an empty array for an organization's creator id that isn't null.", |
74 |
| - ); |
| 71 | + // Creator id existing but the associated user not existing is either a business logic error which means that the corresponding data in the database is in a corrupted state or it is a rare race condition. It must be investigated and fixed as soon as possible to prevent further data corruption if the former case is true. |
| 72 | + if (existingUser === undefined) { |
| 73 | + ctx.log.warn( |
| 74 | + "Postgres select operation returned an empty array for an organization's creator id that isn't null.", |
| 75 | + ); |
75 | 76 |
|
76 |
| - throw new TalawaGraphQLError({ |
77 |
| - extensions: { |
78 |
| - code: "unexpected", |
79 |
| - }, |
80 |
| - }); |
81 |
| - } |
| 77 | + throw new TalawaGraphQLError({ |
| 78 | + extensions: { |
| 79 | + code: "unexpected", |
| 80 | + }, |
| 81 | + }); |
| 82 | + } |
82 | 83 |
|
83 |
| - return existingUser; |
| 84 | + return existingUser; |
| 85 | + } catch (error) { |
| 86 | + if (error instanceof TalawaGraphQLError) { |
| 87 | + throw error; |
| 88 | + } |
| 89 | + ctx.log.error(error); |
| 90 | + throw new TalawaGraphQLError({ |
| 91 | + message: "Internal server error", |
| 92 | + extensions: { |
| 93 | + code: "unexpected", |
84 | 94 | },
|
| 95 | + }); |
| 96 | + } |
| 97 | +}; |
| 98 | +Organization.implement({ |
| 99 | + fields: (t) => ({ |
| 100 | + creator: t.field({ |
| 101 | + description: "User who created the organization.", |
| 102 | + resolve: OrganizationCreatorResolver, |
85 | 103 | type: User,
|
86 | 104 | }),
|
87 | 105 | }),
|
|
0 commit comments