Skip to content

Commit c123acb

Browse files
rabbits changes
Signed-off-by: NishantSinghhhhh <[email protected]>
1 parent 0bb9e11 commit c123acb

File tree

1 file changed

+98
-96
lines changed

1 file changed

+98
-96
lines changed

src/graphql/types/Mutation/updateActionItemCategory.ts

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,118 +9,120 @@ const mutationUpdateActionItemCategoryArgumentsSchema = z.object({
99
input: z.object({
1010
categoryId: z.string().uuid(), // The ID of the category to update
1111
name: z.string().optional(), // New name
12-
isDisabled: z.boolean().optional(), // Enable/disable
12+
isDisabled: z.boolean().optional(),
1313
}),
1414
});
1515

16-
export const updateActionItemCategory = builder.mutationField("updateActionItemCategory", (t) =>
17-
t.field({
18-
type: ActionItemCategory,
19-
args: {
20-
input: t.arg({
21-
required: true,
22-
type: builder.inputType("UpdateActionItemCategoryInput", {
23-
fields: (t) => ({
24-
categoryId: t.field({ type: "ID", required: true }),
25-
name: t.field({ type: "String" }),
26-
isDisabled: t.field({ type: "Boolean" }),
16+
export const updateActionItemCategory = builder.mutationField(
17+
"updateActionItemCategory",
18+
(t) =>
19+
t.field({
20+
type: ActionItemCategory,
21+
args: {
22+
input: t.arg({
23+
required: true,
24+
type: builder.inputType("UpdateActionItemCategoryInput", {
25+
fields: (t) => ({
26+
categoryId: t.field({ type: "ID", required: true }),
27+
name: t.field({ type: "String" }),
28+
isDisabled: t.field({ type: "Boolean" }),
29+
}),
2730
}),
2831
}),
29-
}),
30-
},
31-
description: "Mutation field to update an existing Action Item Category.",
32-
resolve: async (_parent, args, ctx) => {
33-
// 1. Check user authentication
34-
if (!ctx.currentClient.isAuthenticated) {
35-
throw new TalawaGraphQLError({
36-
extensions: { code: "unauthenticated" },
37-
});
38-
}
32+
},
33+
description: "Mutation field to update an existing Action Item Category.",
34+
resolve: async (_parent, args, ctx) => {
35+
// 1. Check user authentication
36+
if (!ctx.currentClient.isAuthenticated) {
37+
throw new TalawaGraphQLError({
38+
extensions: { code: "unauthenticated" },
39+
});
40+
}
3941

40-
// 2. Validate input
41-
const parsedArgs =
42-
mutationUpdateActionItemCategoryArgumentsSchema.parse(args);
43-
const { categoryId, name, isDisabled } = parsedArgs.input;
44-
const currentUserId = ctx.currentClient.user.id;
42+
// 2. Validate input
43+
const parsedArgs =
44+
mutationUpdateActionItemCategoryArgumentsSchema.parse(args);
45+
const { categoryId, name, isDisabled } = parsedArgs.input;
46+
const currentUserId = ctx.currentClient.user.id;
4547

46-
// 3. Find the existing category
47-
const existingCategory =
48-
await ctx.drizzleClient.query.actionCategoriesTable.findFirst({
49-
columns: {
50-
id: true,
51-
organizationId: true,
52-
},
53-
where: (fields, operators) => operators.eq(fields.id, categoryId),
54-
});
48+
// 3. Find the existing category
49+
const existingCategory =
50+
await ctx.drizzleClient.query.actionCategoriesTable.findFirst({
51+
columns: {
52+
id: true,
53+
organizationId: true,
54+
},
55+
where: (fields, operators) => operators.eq(fields.id, categoryId),
56+
});
5557

56-
if (!existingCategory) {
57-
throw new TalawaGraphQLError({
58-
extensions: {
59-
code: "arguments_associated_resources_not_found",
60-
issues: [{ argumentPath: ["input", "categoryId"] }],
61-
},
62-
});
63-
}
58+
if (!existingCategory) {
59+
throw new TalawaGraphQLError({
60+
extensions: {
61+
code: "arguments_associated_resources_not_found",
62+
issues: [{ argumentPath: ["input", "categoryId"] }],
63+
},
64+
});
65+
}
6466

65-
// 4. Check if the user is an admin in that category's organization
66-
const userMembership =
67-
await ctx.drizzleClient.query.organizationMembershipsTable.findFirst({
68-
columns: { role: true },
69-
where: (fields, operators) =>
70-
sql`${operators.eq(fields.memberId, currentUserId)}
67+
// 4. Check if the user is an admin in that category's organization
68+
const userMembership =
69+
await ctx.drizzleClient.query.organizationMembershipsTable.findFirst({
70+
columns: { role: true },
71+
where: (fields, operators) =>
72+
sql`${operators.eq(fields.memberId, currentUserId)}
7173
AND ${operators.eq(fields.organizationId, existingCategory.organizationId)}`,
72-
});
74+
});
7375

74-
if (!userMembership || userMembership.role !== "administrator") {
75-
throw new TalawaGraphQLError({
76-
extensions: {
77-
code: "forbidden_action_on_arguments_associated_resources",
78-
issues: [
79-
{
80-
argumentPath: ["input", "categoryId"],
81-
message: "Only administrators can update this category.",
82-
},
83-
],
84-
},
85-
});
86-
}
76+
if (!userMembership || userMembership.role !== "administrator") {
77+
throw new TalawaGraphQLError({
78+
extensions: {
79+
code: "forbidden_action_on_arguments_associated_resources",
80+
issues: [
81+
{
82+
argumentPath: ["input", "categoryId"],
83+
message: "Only administrators can update this category.",
84+
},
85+
],
86+
},
87+
});
88+
}
8789

88-
// Build update fields
89-
const updates: Record<string, unknown> = {};
90-
let hasUpdates = false;
90+
// Build update fields
91+
const updates: Record<string, unknown> = {};
92+
let hasUpdates = false;
9193

92-
if (typeof name === "string") {
93-
updates.name = name;
94-
hasUpdates = true;
95-
}
94+
if (typeof name === "string") {
95+
updates.name = name;
96+
hasUpdates = true;
97+
}
9698

97-
if (typeof isDisabled === "boolean") {
98-
updates.isDisabled = isDisabled;
99-
hasUpdates = true;
100-
}
99+
if (typeof isDisabled === "boolean") {
100+
updates.isDisabled = isDisabled;
101+
hasUpdates = true;
102+
}
101103

102-
// Only update if there's any field to update.
103-
if (hasUpdates) {
104-
updates.updatedAt = new Date();
105-
// Proceed with the update operation
106-
} else {
107-
// Optionally log or handle no-op scenario: nothing to update
108-
}
104+
// Only update if there's any field to update.
105+
if (hasUpdates) {
106+
updates.updatedAt = new Date();
107+
// Proceed with the update operation
108+
} else {
109+
// Optionally log or handle no-op scenario: nothing to update
110+
}
109111

110-
const [updatedCategory] = await ctx.drizzleClient
111-
.update(actionCategoriesTable)
112-
.set(updates)
113-
.where(eq(actionCategoriesTable.id, categoryId))
114-
.returning();
112+
const [updatedCategory] = await ctx.drizzleClient
113+
.update(actionCategoriesTable)
114+
.set(updates)
115+
.where(eq(actionCategoriesTable.id, categoryId))
116+
.returning();
115117

116-
if (!updatedCategory) {
117-
ctx.log.error(
118-
"Postgres update operation unexpectedly returned an empty array.",
119-
);
120-
throw new TalawaGraphQLError({ extensions: { code: "unexpected" } });
121-
}
118+
if (!updatedCategory) {
119+
ctx.log.error(
120+
"Postgres update operation unexpectedly returned an empty array.",
121+
);
122+
throw new TalawaGraphQLError({ extensions: { code: "unexpected" } });
123+
}
122124

123-
return updatedCategory;
124-
},
125-
}),
125+
return updatedCategory;
126+
},
127+
}),
126128
);

0 commit comments

Comments
 (0)