Skip to content

Commit 51c11d2

Browse files
Test: src/graphql/types/Organization/creator.ts (#3196)
* Test: src/graphql/types/Organization/creator.ts * CodeRabbit's suggestions. * CodeRabbit's suggestions. * CodeRabbit's suggestions. * check type Error * got code coverage 100 --------- Co-authored-by: Peter Harrison <[email protected]>
1 parent 7fa0ae3 commit 51c11d2

File tree

2 files changed

+457
-66
lines changed

2 files changed

+457
-66
lines changed

src/graphql/types/Organization/creator.ts

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,105 @@
11
import { User } from "~/src/graphql/types/User/User";
22
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
3+
import type { GraphQLContext } from "../../context";
34
import { Organization } from "./Organization";
5+
import type { Organization as Organizationtype } from "./Organization";
46

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

18-
const currentUserId = ctx.currentClient.user.id;
20+
try {
21+
const currentUserId = ctx.currentClient.user.id;
1922

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: {
2129
columns: {
2230
role: true,
2331
},
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+
});
3538

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

44-
const currentUserOrganizationMembership =
45-
currentUser.organizationMembershipsWhereMember[0];
47+
const currentUserOrganizationMembership =
48+
currentUser.organizationMembershipsWhereMember[0];
4649

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

59-
if (parent.creatorId === null) {
60-
return null;
61-
}
62+
if (parent.creatorId === null) {
63+
return null;
64+
}
6265

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+
});
6970

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

76-
throw new TalawaGraphQLError({
77-
extensions: {
78-
code: "unexpected",
79-
},
80-
});
81-
}
77+
throw new TalawaGraphQLError({
78+
extensions: {
79+
code: "unexpected",
80+
},
81+
});
82+
}
8283

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",
8494
},
95+
});
96+
}
97+
};
98+
Organization.implement({
99+
fields: (t) => ({
100+
creator: t.field({
101+
description: "User who created the organization.",
102+
resolve: OrganizationCreatorResolver,
85103
type: User,
86104
}),
87105
}),

0 commit comments

Comments
 (0)