Skip to content

Commit 808ff35

Browse files
authored
Migrated to Integration-Test : Test/routes/graphql/Mutation/deletePost : (PalisadoesFoundation#3289)
* added tests in pre-commit * fixes * test * test * added test * rabbit ai fixes * rabbit ai fixes * rabbit ai fixes * rabbit ai fixes * rabbit ai fixes * retest
1 parent b8d4a95 commit 808ff35

File tree

5 files changed

+643
-397
lines changed

5 files changed

+643
-397
lines changed

src/graphql/types/Mutation/deletePost.ts

Lines changed: 126 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,145 +2,159 @@ import { eq } from "drizzle-orm";
22
import { z } from "zod";
33
import { postsTable } from "~/src/drizzle/tables/posts";
44
import { builder } from "~/src/graphql/builder";
5-
import type { GraphQLContext } from "~/src/graphql/context";
65
import {
76
MutationDeletePostInput,
87
mutationDeletePostInputSchema,
98
} from "~/src/graphql/inputs/MutationDeletePostInput";
109
import { Post } from "~/src/graphql/types/Post/Post";
1110
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
1211

13-
// Schema for the arguments object
1412
const mutationDeletePostArgumentsSchema = z.object({
1513
input: mutationDeletePostInputSchema,
1614
});
1715

18-
// Extract and export the resolver logic so it can be imported in your tests.
19-
export async function deletePostResolver(
20-
_parent: unknown,
21-
args: z.infer<typeof mutationDeletePostArgumentsSchema>,
22-
ctx: GraphQLContext,
23-
) {
24-
if (!ctx.currentClient.isAuthenticated) {
25-
throw new TalawaGraphQLError({
26-
extensions: { code: "unauthenticated" },
27-
});
28-
}
16+
builder.mutationField("deletePost", (t) =>
17+
t.field({
18+
args: {
19+
input: t.arg({
20+
description: "",
21+
required: true,
22+
type: MutationDeletePostInput,
23+
}),
24+
},
25+
description: "Mutation field to delete a post.",
26+
resolve: async (_parent, args, ctx) => {
27+
if (!ctx.currentClient.isAuthenticated) {
28+
throw new TalawaGraphQLError({
29+
extensions: {
30+
code: "unauthenticated",
31+
},
32+
});
33+
}
2934

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

36-
if (!success) {
37-
throw new TalawaGraphQLError({
38-
extensions: {
39-
code: "invalid_arguments",
40-
issues: error.issues.map((issue) => ({
41-
argumentPath: issue.path,
42-
message: issue.message,
43-
})),
44-
},
45-
});
46-
}
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+
}
4752

48-
const currentUserId = ctx.currentClient.user.id;
53+
const currentUserId = ctx.currentClient.user.id;
4954

50-
const [currentUser, existingPost] = await Promise.all([
51-
ctx.drizzleClient.query.usersTable.findFirst({
52-
columns: { role: true },
53-
where: (fields, operators: { eq: typeof eq }) =>
54-
operators.eq(fields.id, currentUserId),
55-
}),
56-
ctx.drizzleClient.query.postsTable.findFirst({
57-
columns: { creatorId: true },
58-
with: {
59-
attachmentsWherePost: true,
60-
organization: {
61-
columns: { countryCode: true },
55+
const [currentUser, existingPost] = await Promise.all([
56+
ctx.drizzleClient.query.usersTable.findFirst({
57+
columns: {
58+
role: true,
59+
},
60+
where: (fields, operators) => operators.eq(fields.id, currentUserId),
61+
}),
62+
ctx.drizzleClient.query.postsTable.findFirst({
63+
columns: {
64+
creatorId: true,
65+
},
6266
with: {
63-
membershipsWhereOrganization: {
64-
columns: { role: true },
65-
where: (fields, operators: { eq: typeof eq }) =>
66-
operators.eq(fields.memberId, currentUserId),
67+
attachmentsWherePost: true,
68+
organization: {
69+
columns: {
70+
countryCode: true,
71+
},
72+
with: {
73+
membershipsWhereOrganization: {
74+
columns: {
75+
role: true,
76+
},
77+
where: (fields, operators) =>
78+
operators.eq(fields.memberId, currentUserId),
79+
},
80+
},
6781
},
6882
},
69-
},
70-
},
71-
where: eq(postsTable.id, parsedArgs.input.id),
72-
}),
73-
]);
83+
where: (fields, operators) =>
84+
operators.eq(fields.id, parsedArgs.input.id),
85+
}),
86+
]);
7487

75-
if (currentUser === undefined) {
76-
throw new TalawaGraphQLError({
77-
extensions: { code: "unauthenticated" },
78-
});
79-
}
80-
81-
if (existingPost === undefined) {
82-
throw new TalawaGraphQLError({
83-
extensions: {
84-
code: "arguments_associated_resources_not_found",
85-
issues: [{ argumentPath: ["input", "id"] }],
86-
},
87-
});
88-
}
88+
if (currentUser === undefined) {
89+
throw new TalawaGraphQLError({
90+
extensions: {
91+
code: "unauthenticated",
92+
},
93+
});
94+
}
8995

90-
if (currentUser.role !== "administrator") {
91-
const currentUserOrganizationMembership =
92-
existingPost.organization.membershipsWhereOrganization[0];
96+
if (existingPost === undefined) {
97+
throw new TalawaGraphQLError({
98+
extensions: {
99+
code: "arguments_associated_resources_not_found",
100+
issues: [
101+
{
102+
argumentPath: ["input", "id"],
103+
},
104+
],
105+
},
106+
});
107+
}
93108

94-
if (
95-
currentUserOrganizationMembership === undefined ||
96-
(currentUserOrganizationMembership.role !== "administrator" &&
97-
existingPost.creatorId !== currentUserId)
98-
) {
99-
throw new TalawaGraphQLError({
100-
extensions: {
101-
code: "unauthorized_action_on_arguments_associated_resources",
102-
issues: [{ argumentPath: ["input", "id"] }],
103-
},
104-
});
105-
}
106-
}
109+
if (currentUser.role !== "administrator") {
110+
const currentUserOrganizationMembership =
111+
existingPost.organization.membershipsWhereOrganization[0];
107112

108-
return await ctx.drizzleClient.transaction(async (tx) => {
109-
const [deletedPost] = await tx
110-
.delete(postsTable)
111-
.where(eq(postsTable.id, parsedArgs.input.id))
112-
.returning();
113+
if (
114+
currentUserOrganizationMembership === undefined ||
115+
(currentUserOrganizationMembership.role !== "administrator" &&
116+
existingPost.creatorId !== currentUserId)
117+
) {
118+
throw new TalawaGraphQLError({
119+
extensions: {
120+
code: "unauthorized_action_on_arguments_associated_resources",
121+
issues: [
122+
{
123+
argumentPath: ["input", "id"],
124+
},
125+
],
126+
},
127+
});
128+
}
129+
}
113130

114-
if (deletedPost === undefined) {
115-
throw new TalawaGraphQLError({
116-
extensions: { code: "unexpected" },
117-
});
118-
}
131+
return await ctx.drizzleClient.transaction(async (tx) => {
132+
const [deletedPost] = await tx
133+
.delete(postsTable)
134+
.where(eq(postsTable.id, parsedArgs.input.id))
135+
.returning();
119136

120-
await ctx.minio.client.removeObjects(
121-
ctx.minio.bucketName,
122-
existingPost.attachmentsWherePost.map(
123-
(attachment: { name: string }) => attachment.name,
124-
),
125-
);
137+
// Deleted post not being returned means that either it was deleted or its `id` column was changed by external entities before this delete operation.
138+
if (deletedPost === undefined) {
139+
throw new TalawaGraphQLError({
140+
extensions: {
141+
code: "unexpected",
142+
},
143+
});
144+
}
126145

127-
return Object.assign(deletedPost, {
128-
attachments: existingPost.attachmentsWherePost,
129-
});
130-
});
131-
}
146+
await ctx.minio.client.removeObjects(
147+
ctx.minio.bucketName,
148+
existingPost.attachmentsWherePost.map(
149+
(attachment) => attachment.name,
150+
),
151+
);
132152

133-
builder.mutationField("deletePost", (t) =>
134-
t.field({
135-
args: {
136-
input: t.arg({
137-
description: "",
138-
required: true,
139-
type: MutationDeletePostInput,
140-
}),
153+
return Object.assign(deletedPost, {
154+
attachments: existingPost.attachmentsWherePost,
155+
});
156+
});
141157
},
142-
description: "Mutation field to delete a post.",
143-
resolve: deletePostResolver,
144158
type: Post,
145159
}),
146160
);

0 commit comments

Comments
 (0)