Skip to content

Commit

Permalink
CRUD Operations for tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
MugemaneBertin2001 committed Sep 11, 2024
1 parent 3c32210 commit 2821122
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 51 additions & 7 deletions src/resolvers/ticket.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,7 @@ const resolvers = {
Mutation: {
createTicket: async (_: any, args: TicketType, context: Context) => {
try {
;(await checkUserLoggedIn(context))([
'admin',
'coordinator',
'manager',
'trainee',
'user',
])
;(await checkUserLoggedIn(context))(['admin', 'superAdmin'])
const { subject, message }: TicketType = args

const ticket = await Ticket.create({
Expand Down Expand Up @@ -340,6 +334,56 @@ const resolvers = {
}
},
},
deleteTicket: async (
_: any,
args: { ticketId: string },
context: Context
) => {
try {
const user = (await checkUserLoggedIn(context))(['admin', 'superAdmin'])

if (!user || (user.role !== 'admin' && user.role !== 'superAdmin')) {
throw new GraphQLError(
'Access denied! Only admins or super admins can delete tickets.',
{
extensions: {
code: 'FORBIDDEN',
},
}
)
}

const { ticketId } = args

const ticket = await Ticket.findById(ticketId)

if (!ticket) {
throw new GraphQLError('Ticket not found.', {
extensions: {
code: 'NOT_FOUND',
},
})
}

await ticket.deleteOne()

pubsub.publish('TICKET_DELETED', {
ticketDeleted: {
id: ticketId,
},
})

return {
responseMsg: 'Ticket has been successfully deleted.',
}
} catch (error: any) {
throw new GraphQLError(error.message, {
extensions: {
code: '500',
},
})
}
},
}

export default resolvers

0 comments on commit 2821122

Please sign in to comment.