Skip to content

Commit 8547a42

Browse files
authored
Test: Added test cases for src/graphql/types/Post/updater.ts (PalisadoesFoundation#3310)
1 parent 4f56a53 commit 8547a42

File tree

2 files changed

+363
-70
lines changed

2 files changed

+363
-70
lines changed

src/graphql/types/Post/updater.ts

Lines changed: 76 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,94 @@
1+
import type { GraphQLContext } from "~/src/graphql/context";
12
import { User } from "~/src/graphql/types/User/User";
23
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
34
import { Post } from "./Post";
5+
import type { Post as PostType } from "./Post";
46

5-
Post.implement({
6-
fields: (t) => ({
7-
updater: t.field({
8-
description: "User who last updated the post.",
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;
7+
export const resolveUpdater = async (
8+
parent: PostType,
9+
_args: Record<string, never>,
10+
ctx: GraphQLContext,
11+
) => {
12+
if (!ctx.currentClient.isAuthenticated) {
13+
throw new TalawaGraphQLError({
14+
extensions: {
15+
code: "unauthenticated",
16+
},
17+
});
18+
}
1919

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-
});
20+
const currentUserId = ctx.currentClient.user.id;
3221

33-
if (currentUser === undefined) {
34-
throw new TalawaGraphQLError({
35-
extensions: {
36-
code: "unauthenticated",
37-
},
38-
});
39-
}
22+
const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({
23+
with: {
24+
organizationMembershipsWhereMember: {
25+
columns: {
26+
role: true,
27+
},
28+
where: (fields, operators) =>
29+
operators.eq(fields.organizationId, parent.organizationId),
30+
},
31+
},
32+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
33+
});
4034

41-
const currentUserOrganizationMembership =
42-
currentUser.organizationMembershipsWhereMember[0];
35+
if (currentUser === undefined) {
36+
throw new TalawaGraphQLError({
37+
extensions: {
38+
code: "unauthenticated",
39+
},
40+
});
41+
}
4342

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-
}
43+
const currentUserOrganizationMembership =
44+
currentUser.organizationMembershipsWhereMember[0];
5545

56-
if (parent.updaterId === null) {
57-
return null;
58-
}
46+
if (
47+
currentUser.role !== "administrator" &&
48+
(currentUserOrganizationMembership === undefined ||
49+
currentUserOrganizationMembership.role !== "administrator")
50+
) {
51+
throw new TalawaGraphQLError({
52+
extensions: {
53+
code: "unauthorized_action",
54+
},
55+
});
56+
}
5957

60-
if (parent.updaterId === currentUserId) {
61-
return currentUser;
62-
}
58+
if (parent.updaterId === null) {
59+
return null;
60+
}
6361

64-
const updaterId = parent.updaterId;
62+
if (parent.updaterId === currentUserId) {
63+
return currentUser;
64+
}
6565

66-
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst(
67-
{
68-
where: (fields, operators) => operators.eq(fields.id, updaterId),
69-
},
70-
);
66+
const updaterId = parent.updaterId;
7167

72-
// 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.
73-
if (existingUser === undefined) {
74-
ctx.log.error(
75-
"Postgres select operation returned an empty array for a post's updater id that isn't null.",
76-
);
77-
throw new TalawaGraphQLError({
78-
extensions: {
79-
code: "unexpected",
80-
},
81-
});
82-
}
68+
const existingUser = await ctx.drizzleClient.query.usersTable.findFirst({
69+
where: (fields, operators) => operators.eq(fields.id, updaterId),
70+
});
8371

84-
return existingUser;
72+
// 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.
73+
if (existingUser === undefined) {
74+
ctx.log.error(
75+
"Postgres select operation returned an empty array for a post's updater id that isn't null.",
76+
);
77+
throw new TalawaGraphQLError({
78+
extensions: {
79+
code: "unexpected",
8580
},
81+
});
82+
}
83+
84+
return existingUser;
85+
};
86+
87+
Post.implement({
88+
fields: (t) => ({
89+
updater: t.field({
90+
description: "User who last updated the post.",
91+
resolve: resolveUpdater,
8692
type: User,
8793
}),
8894
}),

0 commit comments

Comments
 (0)