-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Added mutations for action Item Category #3432
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
Closed
NishantSinghhhhh
wants to merge
54
commits into
PalisadoesFoundation:develop-postgres
from
NishantSinghhhhh:ActionItemCategory
Closed
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
996dc79
Added mutations for action Item Category
NishantSinghhhhh 5c86d3b
Merge branch 'develop-postgres' into ActionItemCategory
NishantSinghhhhh 75d910e
Added tests
NishantSinghhhhh 04eba4f
Added test for createAction Item Category
NishantSinghhhhh 0bb9e11
rabbits changes
NishantSinghhhhh c123acb
rabbits changes
NishantSinghhhhh 0a07f4f
rabbits changes
NishantSinghhhhh 5858194
rabbits changes
NishantSinghhhhh 3645672
Merge branch 'develop-postgres' into ActionItemCategory
NishantSinghhhhh 3372bd2
Created an input type for createActionItem
NishantSinghhhhh f689e29
Created an input type for createActionItem
NishantSinghhhhh cc2e986
Created an input type for createActionItem
NishantSinghhhhh 9e3bb02
Created an input type for createActionItem
NishantSinghhhhh b03f5c2
Added types for 2 mutations and edited 1 mutation named createActionI…
NishantSinghhhhh 040fef1
Merge branch 'develop-postgres' into ActionItemCategory
NishantSinghhhhh 0143f00
changed files
NishantSinghhhhh ce056bc
Addedd changes
NishantSinghhhhh 4889eeb
Merge branch 'ActionItemCategory' of https://github.com/NishantSinghh…
NishantSinghhhhh 255c319
Added changes
NishantSinghhhhh 6e3ce55
Added changes
NishantSinghhhhh 8ffa4f0
Added changes
NishantSinghhhhh 5ce99b4
Added changes
NishantSinghhhhh 0ac4781
Added changes
NishantSinghhhhh 4efeea4
Added changes
NishantSinghhhhh 66de1bd
Added changes
NishantSinghhhhh e736e0a
Added changes
NishantSinghhhhh a2d07f1
Added changes
NishantSinghhhhh eaa3b9f
Added tests
NishantSinghhhhh 56b290c
Added tests
NishantSinghhhhh 00646cd
Added tests
NishantSinghhhhh b6d8ade
Added tests
NishantSinghhhhh 335a92d
Added tests
NishantSinghhhhh 1f96ef2
Added tests
NishantSinghhhhh 95fdf64
Merge branch 'develop-postgres' into ActionItemCategory
NishantSinghhhhh 267bb0c
Added tests
NishantSinghhhhh 86c83e4
Added tests
NishantSinghhhhh 6127a9a
Added tests
NishantSinghhhhh 31b85f0
Added tests
NishantSinghhhhh 8d5649d
Added tests
NishantSinghhhhh 43ce3ff
Added tests
NishantSinghhhhh 0b7b030
Added tests
NishantSinghhhhh a473043
Added alloted hours
NishantSinghhhhh b918f6d
Added changes
NishantSinghhhhh 42610e6
Merge branch 'develop-postgres' into ActionItemCategory
NishantSinghhhhh 9f8d528
Added changes
NishantSinghhhhh 8fb5aa8
'Merge branch 'ActionItemCategory' of https://github.com/NishantSingh…
NishantSinghhhhh f1b412c
removed type errors
NishantSinghhhhh 0eb60de
removed type errors
NishantSinghhhhh d8a85ca
removed type errors
NishantSinghhhhh fa3373a
changed names of tables
NishantSinghhhhh 24804c3
Added alloted hours data in sample data for actionItems
NishantSinghhhhh c5a75eb
Added alloted hours data in sample data for actionItems
NishantSinghhhhh 280ed54
Added alloted hours data in sample data for actionItems
NishantSinghhhhh a0ea106
Added changes in table
NishantSinghhhhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { sql } from "drizzle-orm"; | ||
import { uuidv7 } from "uuidv7"; | ||
import { z } from "zod"; | ||
import { actionCategoriesTable } from "~/src/drizzle/tables/actionCategories"; | ||
import { builder } from "~/src/graphql/builder"; | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError"; | ||
|
||
// 1. Define your ActionItemCategory GraphQL output type (example) | ||
import { ActionItemCategory } from "../ActionItemCategory/ActionItemCategory"; | ||
|
||
// 2. Create Zod schema for input validation | ||
const mutationCreateActionItemCategoryArgumentsSchema = z.object({ | ||
input: z.object({ | ||
name: z.string().min(1, "Category name cannot be empty"), | ||
organizationId: z.string().uuid(), | ||
// Optional fields like isDisabled, if you allow it to be set: | ||
isDisabled: z.boolean().optional(), | ||
}), | ||
}); | ||
|
||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builder.mutationField("createActionItemCategory", (t) => | ||
t.field({ | ||
type: ActionItemCategory, // The return type | ||
args: { | ||
input: t.arg({ | ||
required: true, | ||
type: builder.inputType("CreateActionItemCategoryInput", { | ||
fields: (t) => ({ | ||
name: t.field({ type: "String", required: true }), | ||
organizationId: t.field({ type: "ID", required: true }), | ||
isDisabled: t.field({ type: "Boolean" }), // Allow setting isDisabled from input | ||
}), | ||
}), | ||
}), | ||
}, | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: "Mutation field to create a new Action Item Category.", | ||
resolve: async (_parent, args, ctx) => { | ||
// 4. Check authentication | ||
if (!ctx.currentClient.isAuthenticated) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { code: "unauthenticated" }, | ||
}); | ||
} | ||
|
||
// 5. Validate input with Zod | ||
const parsedArgs = | ||
mutationCreateActionItemCategoryArgumentsSchema.parse(args); | ||
const currentUserId = ctx.currentClient.user.id; | ||
|
||
// 6. Check if the organization exists | ||
const existingOrganization = | ||
await ctx.drizzleClient.query.organizationsTable.findFirst({ | ||
columns: { id: true }, | ||
where: (fields, operators) => | ||
operators.eq(fields.id, parsedArgs.input.organizationId), | ||
}); | ||
|
||
if (!existingOrganization) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "arguments_associated_resources_not_found", | ||
issues: [{ argumentPath: ["input", "organizationId"] }], | ||
}, | ||
}); | ||
} | ||
|
||
// 7. Check if the user is part of the organization | ||
const userMembership = | ||
await ctx.drizzleClient.query.organizationMembershipsTable.findFirst({ | ||
columns: { role: true }, | ||
where: (fields, operators) => | ||
sql`${operators.eq(fields.memberId, currentUserId)} | ||
AND ${operators.eq(fields.organizationId, parsedArgs.input.organizationId)}`, | ||
}); | ||
|
||
if (!userMembership) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "unauthorized_action_on_arguments_associated_resources", | ||
issues: [{ argumentPath: ["input", "organizationId"] }], | ||
}, | ||
}); | ||
} | ||
|
||
// 8. Check if the user is an admin (similar logic as action item creation) | ||
if (userMembership.role !== "administrator") { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "forbidden_action_on_arguments_associated_resources", | ||
issues: [ | ||
{ | ||
argumentPath: ["input", "organizationId"], | ||
message: | ||
"Only administrators can create categories for this organization.", | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
|
||
// 9. Insert the new category | ||
const [createdCategory] = await ctx.drizzleClient | ||
.insert(actionCategoriesTable) | ||
.values({ | ||
id: uuidv7(), | ||
name: parsedArgs.input.name, | ||
organizationId: parsedArgs.input.organizationId, | ||
creatorId: currentUserId, | ||
isDisabled: parsedArgs.input.isDisabled ?? false, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}) | ||
.returning(); | ||
|
||
if (!createdCategory) { | ||
ctx.log.error( | ||
"Postgres insert operation unexpectedly returned an empty array.", | ||
); | ||
throw new TalawaGraphQLError({ extensions: { code: "unexpected" } }); | ||
} | ||
|
||
return createdCategory; | ||
}, | ||
}), | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { eq, sql } from "drizzle-orm"; | ||
import { z } from "zod"; | ||
import { actionCategoriesTable } from "~/src/drizzle/tables/actionCategories"; | ||
import { builder } from "~/src/graphql/builder"; | ||
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError"; | ||
import { ActionItemCategory } from "../ActionItemCategory/ActionItemCategory"; | ||
|
||
const mutationUpdateActionItemCategoryArgumentsSchema = z.object({ | ||
input: z.object({ | ||
categoryId: z.string().uuid(), // The ID of the category to update | ||
name: z.string().optional(), // New name | ||
isDisabled: z.boolean().optional(), // Enable/disable | ||
}), | ||
}); | ||
|
||
builder.mutationField("updateActionItemCategory", (t) => | ||
t.field({ | ||
type: ActionItemCategory, | ||
args: { | ||
input: t.arg({ | ||
required: true, | ||
type: builder.inputType("UpdateActionItemCategoryInput", { | ||
fields: (t) => ({ | ||
categoryId: t.field({ type: "ID", required: true }), | ||
name: t.field({ type: "String" }), | ||
isDisabled: t.field({ type: "Boolean" }), | ||
}), | ||
}), | ||
}), | ||
}, | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: "Mutation field to update an existing Action Item Category.", | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolve: async (_parent, args, ctx) => { | ||
// 1. Check user authentication | ||
if (!ctx.currentClient.isAuthenticated) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { code: "unauthenticated" }, | ||
}); | ||
} | ||
|
||
// 2. Validate input | ||
const parsedArgs = | ||
mutationUpdateActionItemCategoryArgumentsSchema.parse(args); | ||
const { categoryId, name, isDisabled } = parsedArgs.input; | ||
const currentUserId = ctx.currentClient.user.id; | ||
|
||
// 3. Find the existing category | ||
const existingCategory = | ||
await ctx.drizzleClient.query.actionCategoriesTable.findFirst({ | ||
columns: { | ||
id: true, | ||
organizationId: true, | ||
}, | ||
where: (fields, operators) => operators.eq(fields.id, categoryId), | ||
}); | ||
|
||
if (!existingCategory) { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "arguments_associated_resources_not_found", | ||
issues: [{ argumentPath: ["input", "categoryId"] }], | ||
}, | ||
}); | ||
} | ||
|
||
// 4. Check if the user is an admin in that category's organization | ||
const userMembership = | ||
await ctx.drizzleClient.query.organizationMembershipsTable.findFirst({ | ||
columns: { role: true }, | ||
where: (fields, operators) => | ||
sql`${operators.eq(fields.memberId, currentUserId)} | ||
AND ${operators.eq(fields.organizationId, existingCategory.organizationId)}`, | ||
}); | ||
|
||
if (!userMembership || userMembership.role !== "administrator") { | ||
throw new TalawaGraphQLError({ | ||
extensions: { | ||
code: "forbidden_action_on_arguments_associated_resources", | ||
issues: [ | ||
{ | ||
argumentPath: ["input", "categoryId"], | ||
message: "Only administrators can update this category.", | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
|
||
// 5. Update the category | ||
// Build a "set" object only with provided fields | ||
const updates: Record<string, unknown> = { updatedAt: new Date() }; | ||
if (typeof name === "string") { | ||
updates.name = name; | ||
} | ||
if (typeof isDisabled === "boolean") { | ||
updates.isDisabled = isDisabled; | ||
} | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// ... | ||
|
||
const [updatedCategory] = await ctx.drizzleClient | ||
.update(actionCategoriesTable) | ||
.set(updates) | ||
.where(eq(actionCategoriesTable.id, categoryId)) | ||
.returning(); | ||
|
||
if (!updatedCategory) { | ||
ctx.log.error( | ||
"Postgres update operation unexpectedly returned an empty array.", | ||
); | ||
throw new TalawaGraphQLError({ extensions: { code: "unexpected" } }); | ||
} | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return updatedCategory; | ||
}, | ||
NishantSinghhhhh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.