Skip to content

Commit

Permalink
Test to src/graphql/types/Mutation/deleteAgendaItem.ts (PalisadoesFou…
Browse files Browse the repository at this point in the history
…ndation#3256)

* Add test file for deleteAgendaItem mutation

* fix code quality
  • Loading branch information
im-vedant authored Feb 19, 2025
1 parent 84562a3 commit dee12a8
Show file tree
Hide file tree
Showing 3 changed files with 482 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/graphql/inputs/MutationDeleteAgendaItemInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { z } from "zod";
import { agendaItemsTableInsertSchema } from "~/src/drizzle/tables/agendaItems";
import { builder } from "~/src/graphql/builder";

export const mutationDeleteAgendaItemInputSchema = z.object({
export const MutationDeleteAgendaItemInputSchema = z.object({
id: agendaItemsTableInsertSchema.shape.id.unwrap(),
});

export const MutationDeleteAgendaItemInput = builder
.inputRef<z.infer<typeof mutationDeleteAgendaItemInputSchema>>(
.inputRef<z.infer<typeof MutationDeleteAgendaItemInputSchema>>(
"MutationDeleteAgendaItemInput",
)
.implement({
Expand Down
239 changes: 125 additions & 114 deletions src/graphql/types/Mutation/deleteAgendaItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,160 +2,171 @@ import { eq } from "drizzle-orm";
import { z } from "zod";
import { agendaItemsTable } from "~/src/drizzle/tables/agendaItems";
import { builder } from "~/src/graphql/builder";
import type { GraphQLContext } from "~/src/graphql/context";
import {
MutationDeleteAgendaItemInput,
mutationDeleteAgendaItemInputSchema,
MutationDeleteAgendaItemInputSchema,
} from "~/src/graphql/inputs/MutationDeleteAgendaItemInput";
import { AgendaItem } from "~/src/graphql/types/AgendaItem/AgendaItem";
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";

const mutationDeleteAgendaItemArgumentsSchema = z.object({
input: mutationDeleteAgendaItemInputSchema,
input: MutationDeleteAgendaItemInputSchema,
});

builder.mutationField("deleteAgendaItem", (t) =>
t.field({
args: {
input: t.arg({
description: "",
required: true,
type: MutationDeleteAgendaItemInput,
}),
},
description: "Mutation field to delete an agenda item.",
resolve: async (_parent, args, ctx) => {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}
export async function deleteAgendaItemResolver(
_parent: unknown,
args: {
input: {
id: string;
};
},
ctx: GraphQLContext,
) {
if (!ctx.currentClient.isAuthenticated) {
throw new TalawaGraphQLError({
extensions: {
code: "unauthenticated",
},
});
}

const {
data: parsedArgs,
error,
success,
} = mutationDeleteAgendaItemArgumentsSchema.safeParse(args);
const {
data: parsedArgs,
error,
success,
} = mutationDeleteAgendaItemArgumentsSchema.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, existingAgendaItem] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
const [currentUser, existingAgendaItem] = await Promise.all([
ctx.drizzleClient.query.usersTable.findFirst({
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.agendaItemsTable.findFirst({
columns: {
type: true,
},
with: {
folder: {
columns: {
role: true,
},
where: (fields, operators) => operators.eq(fields.id, currentUserId),
}),
ctx.drizzleClient.query.agendaItemsTable.findFirst({
columns: {
type: true,
isAgendaItemFolder: true,
},
with: {
folder: {
event: {
columns: {
isAgendaItemFolder: true,
startAt: true,
},
with: {
event: {
organization: {
columns: {
startAt: 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 (existingAgendaItem === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["input", "id"],
},
],
if (existingAgendaItem === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "arguments_associated_resources_not_found",
issues: [
{
argumentPath: ["input", "id"],
},
});
}
],
},
});
}

const currentUserOrganizationMembership =
existingAgendaItem.folder.event.organization
.membershipsWhereOrganization[0];
const currentUserOrganizationMembership =
existingAgendaItem.folder.event.organization
.membershipsWhereOrganization[0];

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

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

// Deleted agenda item 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 (deletedAgendaItem === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}
// Deleted agenda item 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 (deletedAgendaItem === undefined) {
throw new TalawaGraphQLError({
extensions: {
code: "unexpected",
},
});
}

return deletedAgendaItem;
return deletedAgendaItem;
}

builder.mutationField("deleteAgendaItem", (t) =>
t.field({
args: {
input: t.arg({
description: "",
required: true,
type: MutationDeleteAgendaItemInput,
}),
},
description: "Mutation field to delete an agenda item.",
resolve: deleteAgendaItemResolver,
type: AgendaItem,
}),
);
Loading

0 comments on commit dee12a8

Please sign in to comment.