Skip to content

Commit 9f1bd57

Browse files
authored
Update chat message test (#3174)
* fully updated tests * fixes * fixes * retest
1 parent 404240b commit 9f1bd57

File tree

2 files changed

+566
-121
lines changed

2 files changed

+566
-121
lines changed

src/graphql/types/Mutation/updateChatMessage.ts

Lines changed: 129 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -8,160 +8,168 @@ import {
88
} from "~/src/graphql/inputs/MutationUpdateChatMessageInput";
99
import { ChatMessage } from "~/src/graphql/types/ChatMessage/ChatMessage";
1010
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
11+
import type { GraphQLContext } from "../../context";
1112

13+
// Create a schema for the mutation arguments:
1214
const mutationUpdateChatMessageArgumentsSchema = z.object({
1315
input: mutationUpdateChatMessageInputSchema,
1416
});
1517

16-
builder.mutationField("updateChatMessage", (t) =>
17-
t.field({
18-
args: {
19-
input: t.arg({
20-
description: "",
21-
required: true,
22-
type: MutationUpdateChatMessageInput,
23-
}),
24-
},
25-
description: "Mutation field to update a chat message.",
26-
resolve: async (_parent, args, ctx) => {
27-
if (!ctx.currentClient.isAuthenticated) {
28-
throw new TalawaGraphQLError({
29-
extensions: {
30-
code: "unauthenticated",
31-
},
32-
});
33-
}
18+
// Export the resolver function so your test can import it:
19+
export async function updateChatMessageResolver(
20+
_parent: unknown,
21+
args: { input: { body: string; id: string } },
22+
ctx: GraphQLContext,
23+
) {
24+
if (!ctx.currentClient.isAuthenticated) {
25+
throw new TalawaGraphQLError({
26+
extensions: {
27+
code: "unauthenticated",
28+
},
29+
});
30+
}
3431

35-
const {
36-
success,
37-
data: parsedArgs,
38-
error,
39-
} = mutationUpdateChatMessageArgumentsSchema.safeParse(args);
32+
const {
33+
success,
34+
data: parsedArgs,
35+
error,
36+
} = mutationUpdateChatMessageArgumentsSchema.safeParse(args);
4037

41-
if (!success) {
42-
throw new TalawaGraphQLError({
43-
extensions: {
44-
code: "invalid_arguments",
45-
issues: error.issues.map((issue) => ({
46-
argumentPath: issue.path,
47-
message: issue.message,
48-
})),
49-
},
50-
});
51-
}
38+
if (!success) {
39+
throw new TalawaGraphQLError({
40+
extensions: {
41+
code: "invalid_arguments",
42+
issues: error.issues.map((issue) => ({
43+
argumentPath: issue.path,
44+
message: issue.message,
45+
})),
46+
},
47+
});
48+
}
5249

53-
const currentUserId = ctx.currentClient.user.id;
50+
const currentUserId = ctx.currentClient.user.id;
5451

55-
const [currentUser, existingChatMessage] = await Promise.all([
56-
ctx.drizzleClient.query.usersTable.findFirst({
52+
const [currentUser, existingChatMessage] = await Promise.all([
53+
ctx.drizzleClient.query.usersTable.findFirst({
54+
columns: {
55+
role: true,
56+
},
57+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
58+
}),
59+
ctx.drizzleClient.query.chatMessagesTable.findFirst({
60+
columns: {
61+
creatorId: true,
62+
},
63+
with: {
64+
chat: {
5765
columns: {
58-
role: true,
59-
},
60-
where: (fields, operators) => operators.eq(fields.id, currentUserId),
61-
}),
62-
ctx.drizzleClient.query.chatMessagesTable.findFirst({
63-
columns: {
64-
creatorId: true,
66+
avatarMimeType: true,
6567
},
6668
with: {
67-
chat: {
69+
chatMembershipsWhereChat: {
6870
columns: {
69-
avatarMimeType: true,
71+
role: true,
72+
},
73+
where: (fields, operators) =>
74+
operators.eq(fields.memberId, currentUserId),
75+
},
76+
organization: {
77+
columns: {
78+
countryCode: true,
7079
},
7180
with: {
72-
chatMembershipsWhereChat: {
81+
membershipsWhereOrganization: {
7382
columns: {
7483
role: true,
7584
},
7685
where: (fields, operators) =>
7786
operators.eq(fields.memberId, currentUserId),
7887
},
79-
organization: {
80-
columns: {
81-
countryCode: true,
82-
},
83-
with: {
84-
membershipsWhereOrganization: {
85-
columns: {
86-
role: true,
87-
},
88-
where: (fields, operators) =>
89-
operators.eq(fields.memberId, currentUserId),
90-
},
91-
},
92-
},
9388
},
9489
},
9590
},
96-
where: (fields, operators) =>
97-
operators.eq(fields.id, parsedArgs.input.id),
98-
}),
99-
]);
91+
},
92+
},
93+
where: (fields, operators) =>
94+
operators.eq(fields.id, parsedArgs.input.id),
95+
}),
96+
]);
10097

101-
if (currentUser === undefined) {
102-
throw new TalawaGraphQLError({
103-
extensions: {
104-
code: "unauthenticated",
105-
},
106-
});
107-
}
98+
if (currentUser === undefined) {
99+
throw new TalawaGraphQLError({
100+
extensions: {
101+
code: "unauthenticated",
102+
},
103+
});
104+
}
108105

109-
if (existingChatMessage === undefined) {
110-
throw new TalawaGraphQLError({
111-
extensions: {
112-
code: "arguments_associated_resources_not_found",
113-
issues: [
114-
{
115-
argumentPath: ["input", "id"],
116-
},
117-
],
106+
if (existingChatMessage === undefined) {
107+
throw new TalawaGraphQLError({
108+
extensions: {
109+
code: "arguments_associated_resources_not_found",
110+
issues: [
111+
{
112+
argumentPath: ["input", "id"],
118113
},
119-
});
120-
}
114+
],
115+
},
116+
});
117+
}
121118

122-
const currentUserOrganizationMembership =
123-
existingChatMessage.chat.organization.membershipsWhereOrganization[0];
124-
const currentUserChatMembership =
125-
existingChatMessage.chat.chatMembershipsWhereChat[0];
119+
const currentUserOrganizationMembership =
120+
existingChatMessage.chat.organization.membershipsWhereOrganization[0];
121+
const currentUserChatMembership =
122+
existingChatMessage.chat.chatMembershipsWhereChat[0];
126123

127-
if (
128-
(currentUser.role !== "administrator" &&
129-
(currentUserOrganizationMembership === undefined ||
130-
(currentUserOrganizationMembership.role !== "administrator" &&
131-
currentUserChatMembership === undefined))) ||
132-
currentUserId !== existingChatMessage.creatorId
133-
) {
134-
throw new TalawaGraphQLError({
135-
extensions: {
136-
code: "unauthorized_action_on_arguments_associated_resources",
137-
issues: [
138-
{
139-
argumentPath: ["input", "id"],
140-
},
141-
],
124+
if (
125+
(currentUser.role !== "administrator" &&
126+
(currentUserOrganizationMembership === undefined ||
127+
(currentUserOrganizationMembership.role !== "administrator" &&
128+
currentUserChatMembership === undefined))) ||
129+
currentUserId !== existingChatMessage.creatorId
130+
) {
131+
throw new TalawaGraphQLError({
132+
extensions: {
133+
code: "unauthorized_action_on_arguments_associated_resources",
134+
issues: [
135+
{
136+
argumentPath: ["input", "id"],
142137
},
143-
});
144-
}
138+
],
139+
},
140+
});
141+
}
145142

146-
const [updatedChatMessage] = await ctx.drizzleClient
147-
.update(chatMessagesTable)
148-
.set({
149-
body: parsedArgs.input.body,
150-
})
151-
.where(eq(chatMessagesTable.id, parsedArgs.input.id))
152-
.returning();
143+
const [updatedChatMessage] = await ctx.drizzleClient
144+
.update(chatMessagesTable)
145+
.set({
146+
body: parsedArgs.input.body,
147+
})
148+
.where(eq(chatMessagesTable.id, parsedArgs.input.id))
149+
.returning();
153150

154-
// Updated chat message not being returned means that either it was updated or its `id` column was changed by external entities before this update operation could take place.
155-
if (updatedChatMessage === undefined) {
156-
throw new TalawaGraphQLError({
157-
extensions: {
158-
code: "unexpected",
159-
},
160-
});
161-
}
151+
if (updatedChatMessage === undefined) {
152+
throw new TalawaGraphQLError({
153+
extensions: {
154+
code: "unexpected",
155+
},
156+
});
157+
}
162158

163-
return updatedChatMessage;
164-
},
159+
return updatedChatMessage;
160+
}
161+
162+
// Wire the resolver into your GraphQL schema:
163+
builder.mutationField("updateChatMessage", (t) =>
164+
t.field({
165165
type: ChatMessage,
166+
description: "Mutation field to update a chat message.",
167+
args: {
168+
input: t.arg({
169+
type: MutationUpdateChatMessageInput,
170+
required: true,
171+
}),
172+
},
173+
resolve: updateChatMessageResolver,
166174
}),
167175
);

0 commit comments

Comments
 (0)