Skip to content

Commit be5a37b

Browse files
committed
Added test for fund creator
1 parent 808ff35 commit be5a37b

File tree

2 files changed

+406
-79
lines changed

2 files changed

+406
-79
lines changed

src/graphql/types/Fund/creator.ts

Lines changed: 85 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,95 @@
11
import { User } from "~/src/graphql/types/User/User";
22
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
3+
import type { GraphQLContext } from "../../context";
34
import { Fund } from "./Fund";
5+
import type { Fund as Fundtype } from "./Fund";
46

7+
export const FundCreatorResolver = async (
8+
parent: Fundtype,
9+
_args: unknown,
10+
ctx: GraphQLContext,
11+
) => {
12+
if (!ctx.currentClient.isAuthenticated) {
13+
throw new TalawaGraphQLError({
14+
extensions: {
15+
code: "unauthenticated",
16+
},
17+
});
18+
}
19+
try {
20+
const currentUserId = ctx.currentClient.user.id;
21+
const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({
22+
with: {
23+
organizationMembershipsWhereMember: {
24+
columns: {
25+
role: true,
26+
},
27+
where: (fields, operators) =>
28+
operators.eq(fields.organizationId, parent.organizationId),
29+
},
30+
},
31+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
32+
});
33+
if (currentUser === undefined) {
34+
throw new TalawaGraphQLError({
35+
extensions: {
36+
code: "unauthenticated",
37+
},
38+
});
39+
}
40+
const currentUserOrganizationMembership =
41+
currentUser.organizationMembershipsWhereMember[0];
42+
if (
43+
currentUser.role !== "administrator" &&
44+
(currentUserOrganizationMembership === undefined ||
45+
currentUserOrganizationMembership.role !== "administrator")
46+
) {
47+
throw new TalawaGraphQLError({
48+
extensions: {
49+
code: "unauthorized_action",
50+
},
51+
});
52+
}
53+
if (parent.creatorId === null) {
54+
return null;
55+
}
56+
if (parent.creatorId === currentUserId) {
57+
return currentUser;
58+
}
59+
const creatorId = parent.creatorId;
60+
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({
61+
where: (fields, operators) => operators.eq(fields.id, creatorId),
62+
});
63+
// 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.
64+
if (existingUser === undefined) {
65+
ctx.log.warn(
66+
"Postgres select operation returned an empty array for an organization's creator id that isn't null.",
67+
);
68+
throw new TalawaGraphQLError({
69+
extensions: {
70+
code: "unexpected",
71+
},
72+
});
73+
}
74+
return existingUser;
75+
} catch (error) {
76+
if (error instanceof TalawaGraphQLError) {
77+
throw error;
78+
}
79+
ctx.log.error(error);
80+
throw new TalawaGraphQLError({
81+
message: "Internal server error",
82+
extensions: {
83+
code: "unexpected",
84+
},
85+
});
86+
}
87+
};
588
Fund.implement({
689
fields: (t) => ({
790
creator: t.field({
8-
description: "User who created the fund.",
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 = 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-
},
29-
},
30-
where: (fields, operators) => operators.eq(fields.id, currentUserId),
31-
});
32-
33-
if (currentUser === undefined) {
34-
throw new TalawaGraphQLError({
35-
extensions: {
36-
code: "unauthenticated",
37-
},
38-
});
39-
}
40-
41-
const currentUserOrganizationMembership =
42-
currentUser.organizationMembershipsWhereMember[0];
43-
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-
}
55-
56-
if (parent.creatorId === null) {
57-
return null;
58-
}
59-
60-
if (parent.creatorId === currentUserId) {
61-
return currentUser;
62-
}
63-
64-
const creatorId = parent.creatorId;
65-
66-
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst(
67-
{
68-
where: (fields, operators) => operators.eq(fields.id, creatorId),
69-
},
70-
);
71-
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 a fund's creator id that isn't null.",
76-
);
77-
78-
throw new TalawaGraphQLError({
79-
extensions: {
80-
code: "unexpected",
81-
},
82-
});
83-
}
84-
85-
return existingUser;
86-
},
91+
description: "User who created the Fund.",
92+
resolve: FundCreatorResolver,
8793
type: User,
8894
}),
8995
}),

0 commit comments

Comments
 (0)