Skip to content

Commit d3415dd

Browse files
JXP25coderabbitai[bot]palisadoes
authored
Test: src/graphql/types/FundCampaignPledge/updater.ts (#3190)
* unit test for updater.ts unit test * Update src/graphql/types/FundCampaignPledge/updater.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix * code quality fix * Retest --------- Co-authored-by: JaiPannu-IITI <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Peter Harrison <[email protected]>
1 parent da81d6e commit d3415dd

File tree

2 files changed

+408
-108
lines changed

2 files changed

+408
-108
lines changed

src/graphql/types/FundCampaignPledge/updater.ts

Lines changed: 114 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,133 @@
1+
import type {
2+
ExplicitGraphQLContext,
3+
ImplicitMercuriusContext,
4+
} from "~/src/graphql/context";
15
import { User } from "~/src/graphql/types/User/User";
26
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
37
import { FundCampaignPledge } from "./FundCampaignPledge";
48

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,
4848
},
49+
where: (fields, operators) =>
50+
operators.eq(fields.memberId, currentUserId),
4951
},
5052
},
5153
},
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+
]);
9360

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+
}
9768

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+
);
10173

102-
const updaterId = parent.updaterId;
74+
throw new TalawaGraphQLError({
75+
extensions: {
76+
code: "unexpected",
77+
},
78+
});
79+
}
10380

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+
}
10996

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+
}
115100

116-
throw new TalawaGraphQLError({
117-
extensions: {
118-
code: "unexpected",
119-
},
120-
});
121-
}
101+
if (parent.updaterId === currentUserId) {
102+
return currentUser;
103+
}
122104

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",
124119
},
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,
125131
type: User,
126132
}),
127133
}),

0 commit comments

Comments
 (0)