Skip to content

Commit aa8b50a

Browse files
committed
unit test for updater.ts
unit test
1 parent dcd6ad9 commit aa8b50a

File tree

2 files changed

+325
-108
lines changed

2 files changed

+325
-108
lines changed

src/graphql/types/FundCampaignPledge/updater.ts

Lines changed: 111 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,130 @@
11
import { User } from "~/src/graphql/types/User/User";
22
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
33
import { FundCampaignPledge } from "./FundCampaignPledge";
4+
import type { ExplicitGraphQLContext, ImplicitMercuriusContext } from '~/src/graphql/context';
45

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-
},
6+
type ContextType = ExplicitGraphQLContext & ImplicitMercuriusContext;
7+
8+
9+
export const resolveUpdater = async (
10+
parent: FundCampaignPledge,
11+
_args: Record<string, never>,
12+
ctx: ContextType,
13+
) => {
14+
if (!ctx.currentClient.isAuthenticated) {
15+
throw new TalawaGraphQLError({
16+
extensions: {
17+
code: "unauthenticated",
18+
},
19+
});
20+
}
21+
22+
const currentUserId = ctx.currentClient.user.id;
23+
24+
const [currentUser, existingFundCampaign] = await Promise.all([
25+
ctx.drizzleClient.query.usersTable.findFirst({
26+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
27+
}),
28+
ctx.drizzleClient.query.fundCampaignsTable.findFirst({
29+
columns: {
30+
currencyCode: true,
31+
},
32+
with: {
33+
fund: {
34+
columns: {
35+
isTaxDeductible: true,
36+
},
37+
with: {
38+
organization: {
39+
columns: {
40+
countryCode: true,
41+
},
42+
with: {
43+
membershipsWhereOrganization: {
44+
columns: {
45+
role: true,
4846
},
47+
where: (fields, operators) =>
48+
operators.eq(fields.memberId, currentUserId),
4949
},
5050
},
5151
},
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-
}
52+
},
53+
},
54+
},
55+
where: (fields, operators) => operators.eq(fields.id, parent.campaignId),
56+
}),
57+
]);
9358

94-
if (parent.updaterId === null) {
95-
return null;
96-
}
59+
if (currentUser === undefined) {
60+
throw new TalawaGraphQLError({
61+
extensions: {
62+
code: "unauthenticated",
63+
},
64+
});
65+
}
9766

98-
if (parent.updaterId === currentUserId) {
99-
return currentUser;
100-
}
67+
if (existingFundCampaign === undefined) {
68+
ctx.log.error(
69+
"Postgres select operation returned an empty array for a fund campaign pledge's campaign id that isn't null.",
70+
);
10171

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

104-
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst(
105-
{
106-
where: (fields, operators) => operators.eq(fields.id, updaterId),
107-
},
108-
);
79+
const currentUserOrganizationMembership =
80+
existingFundCampaign.fund.organization.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+
}
10993

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-
);
94+
if (parent.updaterId === null) {
95+
return null;
96+
}
11597

116-
throw new TalawaGraphQLError({
117-
extensions: {
118-
code: "unexpected",
119-
},
120-
});
121-
}
98+
if (parent.updaterId === currentUserId) {
99+
return currentUser;
100+
}
122101

123-
return existingUser;
102+
const updaterId = parent.updaterId;
103+
104+
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({
105+
where: (fields, operators) => operators.eq(fields.id, updaterId),
106+
});
107+
108+
if (existingUser === undefined) {
109+
ctx.log.error(
110+
"Postgres select operation returned an empty array for a fund campaign pledge's updater id that isn't null.",
111+
);
112+
113+
throw new TalawaGraphQLError({
114+
extensions: {
115+
code: "unexpected",
124116
},
117+
});
118+
}
119+
120+
return existingUser;
121+
};
122+
123+
FundCampaignPledge.implement({
124+
fields: (t) => ({
125+
updater: t.field({
126+
description: "User who last updated the fund campaign pledge.",
127+
resolve: resolveUpdater,
125128
type: User,
126129
}),
127130
}),

0 commit comments

Comments
 (0)