Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for : src/graphql/types/Mutation/deleteComment.ts #3234

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 105 additions & 111 deletions src/graphql/types/Mutation/deleteComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,147 +8,141 @@ import {
} from "~/src/graphql/inputs/MutationDeleteCommentInput";
import { Comment } from "~/src/graphql/types/Comment/Comment";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
import type { GraphQLContext } from "../../context";

const mutationDeleteCommentArgumentsSchema = z.object({
input: mutationDeleteCommentInputSchema,
});

builder.mutationField("deleteComment", (t) =>
t.field({
args: {
input: t.arg({
description: "",
required: true,
type: MutationDeleteCommentInput,
}),
},
description: "Mutation field to delete a comment.",
resolve: async (_parent, args, ctx) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
export async function deleteCommentResolver(
_parent: unknown,
args: z.infer<typeof mutationDeleteCommentArgumentsSchema>,
ctx: GraphQLContext,
) {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: { code: "unauthenticated" },
});
}

const {
data: parsedArgs,
error,
success,
} = mutationDeleteCommentArgumentsSchema.safeParse(args);
const {
success,
data: parsedArgs,
error,
} = mutationDeleteCommentArgumentsSchema.safeParse(args);

if (!success) {
throw new TalawaGraphQLError({
extensions: {
code: "invalid_arguments",
issues: error.issues.map((issue) => ({
argumentPath: issue.path,
message: issue.message,
})),
},
});
}
if (!success) {
throw new TalawaGraphQLError({
extensions: {
code: "invalid_arguments",
issues: error.issues.map((issue) => ({
argumentPath: issue.path,
message: issue.message,
})),
},
});
}

const currentUserId = ctx.currentClient.user.id;
const currentUserId = ctx.currentClient.user.id;

const [currentUser, existingComment] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
const [currentUser, existingComment] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
columns: { role: true },
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.commentsTable.findFirst({
columns: { creatorId: true },
with: {
post: {
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.commentsTable.findFirst({
columns: {
creatorId: true,
pinnedAt: true,
},
with: {
post: {
organization: {
columns: {
pinnedAt: true,
countryCode: true,
},
with: {
organization: {
membershipsWhereOrganization: {
columns: {
countryCode: true,
},
with: {
membershipsWhereOrganization: {
columns: {
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
},
role: true,
},
where: (fields, operators) =>
operators.eq(fields.memberId, currentUserId),
},
},
},
},
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.input.id),
}),
]);
},
},
where: (fields, operators) =>
operators.eq(fields.id, parsedArgs.input.id),
}),
]);

if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
if (currentUser === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

if (existingComment === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["input", "id"],
},
],
if (existingComment === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["input", "id"],
},
});
}
],
},
});
}

const currentUserOrganizationMembership =
existingComment.post.organization.membershipsWhereOrganization[0];
const currentUserOrganizationMembership =
existingComment.post.organization.membershipsWhereOrganization[0];

if (
currentUser.role !== "administrator" &&
(currentUserOrganizationMembership === undefined ||
(currentUserOrganizationMembership.role !== "administrator" &&
existingComment.creatorId !== currentUserId))
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action_on_arguments_associated_resources",
issues: [
{
argumentPath: ["input", "id"],
},
],
},
});
}
if (
currentUser.role !== "administrator" &&
(currentUserOrganizationMembership === undefined ||
(currentUserOrganizationMembership.role !== "administrator" &&
existingComment.creatorId !== currentUserId))
) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthorized_action_on_arguments_associated_resources",
issues: [{ argumentPath: ["input", "id"] }],
},
});
}

const [deletedComment] = await ctx.drizzleClient
.delete(commentsTable)
.where(eq(commentsTable.id, parsedArgs.input.id))
.returning();
const [deletedComment] = await ctx.drizzleClient
.delete(commentsTable)
.where(eq(commentsTable.id, parsedArgs.input.id))
.returning();

// Deleted comment not being returned means that either it was deleted or its `id` column was changed by external entities before this delete operation could take place.
if (deletedComment === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}
if (deletedComment === undefined) {
throw new TalawaGraphQLError({
extensions: { code: "unexpected" },
});
}

return deletedComment;
}

return deletedComment;
builder.mutationField("deleteComment", (t) =>
t.field({
args: {
input: t.arg({
description: "",
required: true,
type: MutationDeleteCommentInput,
}),
},
description: "Mutation field to delete a comment.",
resolve: deleteCommentResolver,
type: Comment,
}),
);
Loading
Loading