|
| 1 | +import type { |
| 2 | + ExplicitGraphQLContext, |
| 3 | + ImplicitMercuriusContext, |
| 4 | +} from "~/src/graphql/context"; |
1 | 5 | import { User } from "~/src/graphql/types/User/User";
|
2 | 6 | import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
|
3 | 7 | import { FundCampaignPledge } from "./FundCampaignPledge";
|
4 | 8 |
|
5 |
| -FundCampaignPledge.implement({ |
6 |
| - fields: (t) => ({ |
7 |
| - updater: t.field({ |
8 |
| - description: "User who last updated the fund campaign pledge.", |
9 |
| - resolve: async (parent, _args, ctx) => { |
10 |
| - if (!ctx.currentClient.isAuthenticated) { |
11 |
| - throw new TalawaGraphQLError({ |
12 |
| - extensions: { |
13 |
| - code: "unauthenticated", |
14 |
| - }, |
15 |
| - }); |
16 |
| - } |
17 |
| - |
18 |
| - const currentUserId = ctx.currentClient.user.id; |
19 |
| - |
20 |
| - const [currentUser, existingFundCampaign] = await Promise.all([ |
21 |
| - ctx.drizzleClient.query.usersTable.findFirst({ |
22 |
| - where: (fields, operators) => |
23 |
| - operators.eq(fields.id, currentUserId), |
24 |
| - }), |
25 |
| - ctx.drizzleClient.query.fundCampaignsTable.findFirst({ |
26 |
| - columns: { |
27 |
| - currencyCode: true, |
28 |
| - }, |
29 |
| - with: { |
30 |
| - fund: { |
31 |
| - columns: { |
32 |
| - isTaxDeductible: true, |
33 |
| - }, |
34 |
| - with: { |
35 |
| - organization: { |
36 |
| - columns: { |
37 |
| - countryCode: true, |
38 |
| - }, |
39 |
| - with: { |
40 |
| - membershipsWhereOrganization: { |
41 |
| - columns: { |
42 |
| - role: true, |
43 |
| - }, |
44 |
| - where: (fields, operators) => |
45 |
| - operators.eq(fields.memberId, currentUserId), |
46 |
| - }, |
47 |
| - }, |
| 9 | +type ContextType = ExplicitGraphQLContext & ImplicitMercuriusContext; |
| 10 | + |
| 11 | +export const resolveUpdater = async ( |
| 12 | + parent: FundCampaignPledge, |
| 13 | + _args: Record<string, never>, |
| 14 | + ctx: ContextType, |
| 15 | +) => { |
| 16 | + if (!ctx.currentClient.isAuthenticated) { |
| 17 | + throw new TalawaGraphQLError({ |
| 18 | + extensions: { |
| 19 | + code: "unauthenticated", |
| 20 | + }, |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + const currentUserId = ctx.currentClient.user.id; |
| 25 | + |
| 26 | + const [currentUser, existingFundCampaign] = await Promise.all([ |
| 27 | + ctx.drizzleClient.query.usersTable.findFirst({ |
| 28 | + where: (fields, operators) => operators.eq(fields.id, currentUserId), |
| 29 | + }), |
| 30 | + ctx.drizzleClient.query.fundCampaignsTable.findFirst({ |
| 31 | + columns: { |
| 32 | + currencyCode: true, |
| 33 | + }, |
| 34 | + with: { |
| 35 | + fund: { |
| 36 | + columns: { |
| 37 | + isTaxDeductible: true, |
| 38 | + }, |
| 39 | + with: { |
| 40 | + organization: { |
| 41 | + columns: { |
| 42 | + countryCode: true, |
| 43 | + }, |
| 44 | + with: { |
| 45 | + membershipsWhereOrganization: { |
| 46 | + columns: { |
| 47 | + role: true, |
48 | 48 | },
|
| 49 | + where: (fields, operators) => |
| 50 | + operators.eq(fields.memberId, currentUserId), |
49 | 51 | },
|
50 | 52 | },
|
51 | 53 | },
|
52 |
| - where: (fields, operators) => |
53 |
| - operators.eq(fields.id, parent.campaignId), |
54 |
| - }), |
55 |
| - ]); |
56 |
| - |
57 |
| - if (currentUser === undefined) { |
58 |
| - throw new TalawaGraphQLError({ |
59 |
| - extensions: { |
60 |
| - code: "unauthenticated", |
61 |
| - }, |
62 |
| - }); |
63 |
| - } |
64 |
| - |
65 |
| - // Fund campaign id existing but the associated fund campaign 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. |
66 |
| - if (existingFundCampaign === undefined) { |
67 |
| - ctx.log.error( |
68 |
| - "Postgres select operation returned an empty array for a fund campaign pledge's campaign id that isn't null.", |
69 |
| - ); |
70 |
| - |
71 |
| - throw new TalawaGraphQLError({ |
72 |
| - extensions: { |
73 |
| - code: "unexpected", |
74 |
| - }, |
75 |
| - }); |
76 |
| - } |
77 |
| - |
78 |
| - const currentUserOrganizationMembership = |
79 |
| - existingFundCampaign.fund.organization |
80 |
| - .membershipsWhereOrganization[0]; |
81 |
| - |
82 |
| - if ( |
83 |
| - currentUser.role !== "administrator" && |
84 |
| - (currentUserOrganizationMembership === undefined || |
85 |
| - currentUserOrganizationMembership.role !== "administrator") |
86 |
| - ) { |
87 |
| - throw new TalawaGraphQLError({ |
88 |
| - extensions: { |
89 |
| - code: "unauthorized_action", |
90 |
| - }, |
91 |
| - }); |
92 |
| - } |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + where: (fields, operators) => operators.eq(fields.id, parent.campaignId), |
| 58 | + }), |
| 59 | + ]); |
93 | 60 |
|
94 |
| - if (parent.updaterId === null) { |
95 |
| - return null; |
96 |
| - } |
| 61 | + if (currentUser === undefined) { |
| 62 | + throw new TalawaGraphQLError({ |
| 63 | + extensions: { |
| 64 | + code: "unauthenticated", |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
97 | 68 |
|
98 |
| - if (parent.updaterId === currentUserId) { |
99 |
| - return currentUser; |
100 |
| - } |
| 69 | + if (existingFundCampaign === undefined) { |
| 70 | + ctx.log.error( |
| 71 | + "Postgres select operation returned an empty array for a fund campaign pledge's campaign id that isn't null.", |
| 72 | + ); |
101 | 73 |
|
102 |
| - const updaterId = parent.updaterId; |
| 74 | + throw new TalawaGraphQLError({ |
| 75 | + extensions: { |
| 76 | + code: "unexpected", |
| 77 | + }, |
| 78 | + }); |
| 79 | + } |
103 | 80 |
|
104 |
| - const existingUser = await ctx.drizzleClient.query.usersTable.findFirst( |
105 |
| - { |
106 |
| - where: (fields, operators) => operators.eq(fields.id, updaterId), |
107 |
| - }, |
108 |
| - ); |
| 81 | + const currentUserOrganizationMembership = |
| 82 | + existingFundCampaign.fund.organization.membershipsWhereOrganization[0]; |
| 83 | + |
| 84 | + if ( |
| 85 | + currentUser.role !== "administrator" && |
| 86 | + (currentUserOrganizationMembership === undefined || |
| 87 | + currentUserOrganizationMembership.role !== "administrator") |
| 88 | + ) { |
| 89 | + throw new TalawaGraphQLError({ |
| 90 | + extensions: { |
| 91 | + code: "unauthorized_action", |
| 92 | + message: "Only administrators can perform this action", |
| 93 | + }, |
| 94 | + }); |
| 95 | + } |
109 | 96 |
|
110 |
| - // Updater 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. |
111 |
| - if (existingUser === undefined) { |
112 |
| - ctx.log.error( |
113 |
| - "Postgres select operation returned an empty array for a fund campaign pledge's updater id that isn't null.", |
114 |
| - ); |
| 97 | + if (parent.updaterId === null) { |
| 98 | + return null; |
| 99 | + } |
115 | 100 |
|
116 |
| - throw new TalawaGraphQLError({ |
117 |
| - extensions: { |
118 |
| - code: "unexpected", |
119 |
| - }, |
120 |
| - }); |
121 |
| - } |
| 101 | + if (parent.updaterId === currentUserId) { |
| 102 | + return currentUser; |
| 103 | + } |
122 | 104 |
|
123 |
| - return existingUser; |
| 105 | + const updaterId = parent.updaterId; |
| 106 | + |
| 107 | + const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({ |
| 108 | + where: (fields, operators) => operators.eq(fields.id, updaterId), |
| 109 | + }); |
| 110 | + |
| 111 | + if (existingUser === undefined) { |
| 112 | + ctx.log.error( |
| 113 | + "Postgres select operation returned an empty array for a fund campaign pledge's updater id that isn't null.", |
| 114 | + ); |
| 115 | + |
| 116 | + throw new TalawaGraphQLError({ |
| 117 | + extensions: { |
| 118 | + code: "unexpected", |
124 | 119 | },
|
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + return existingUser; |
| 124 | +}; |
| 125 | + |
| 126 | +FundCampaignPledge.implement({ |
| 127 | + fields: (t) => ({ |
| 128 | + updater: t.field({ |
| 129 | + description: "User who last updated the fund campaign pledge.", |
| 130 | + resolve: resolveUpdater, |
125 | 131 | type: User,
|
126 | 132 | }),
|
127 | 133 | }),
|
|
0 commit comments