generated from bhubr/node-express-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUpdateTaskStatusResolver.ts
More file actions
42 lines (41 loc) · 1.38 KB
/
UpdateTaskStatusResolver.ts
File metadata and controls
42 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { TaskStatusInput } from '../../Inputs/updateTaskStatus';
import { ApolloError } from 'apollo-server-core';
import { GQLContext } from 'src/interfaces';
import { Resolver, Mutation, Ctx, Arg, Authorized } from 'type-graphql';
import { TaskWhereUniqueInput } from '../../../generated/graphql';
import { Task } from '../../../generated/graphql/models/Task';
@Resolver()
export class UpdateTaskStatusResolver {
@Authorized(['ADMIN', 'SUPER_ADMIN', 'USER', 'MANAGER'])
@Mutation(() => Task)
async updateTaskStatus(
@Ctx() ctx: GQLContext,
@Arg('data') data: TaskStatusInput
): Promise<Task> {
const task = await ctx.prisma.task.findUnique({
where: { id: data.taskId as TaskWhereUniqueInput['id'] },
});
if (!task) {
throw new ApolloError('No task found');
}
const projectUsers = await ctx.prisma.project
.findUnique({
where: {
id: task.project_id,
},
})
.users();
if (!projectUsers.some((user) => user.id === ctx.user!.id)) {
throw new ApolloError('You are not allowed to update this task');
}
if (task.status_task !== data.status) {
const updatedTask = await ctx.prisma.task.update({
where: { id: task.id },
data: { status_task: data.status },
});
return updatedTask;
}
return task;
}
}