-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPath.resolver.ts
More file actions
97 lines (86 loc) · 2.77 KB
/
Path.resolver.ts
File metadata and controls
97 lines (86 loc) · 2.77 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { UseGuards } from '@nestjs/common';
import { Args, Mutation, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { GQLAuthGuard } from '../Auth/GQLAuth.guard';
import { CMS } from '../CMS/CMS';
import { ModuleUnion } from '../Module/Module.entity';
import { PathUserService } from '../PathUser/PathUser.service';
import { CurrentUser } from '../User/CurrentUser.decorator';
import { User } from '../User/User.entity';
import { Path, PathInput, UpdatePathInput } from './Path.entity';
import { PathService } from './Path.service';
import { Roles } from '../Role/Role.guard';
@Resolver(() => Path)
export class PathResolver {
constructor(
private readonly pathService: PathService,
private readonly pathUserService: PathUserService,
private readonly cms: CMS
) {}
@UseGuards(GQLAuthGuard)
@Query(() => [Path])
paths(
@Args('onlyJoined', {
description: 'Only get the current user\'s paths',
nullable: true,
defaultValue: false
}) onlyJoined: boolean,
@Args('notJoined', {
description: 'Only get paths the current user has not joined',
nullable: true,
defaultValue: false
}) notJoined: boolean,
@CurrentUser() user: User
) {
if (onlyJoined || notJoined) return this.pathService.findByUser(user.id, notJoined);
return this.pathService.findAll();
}
@UseGuards(GQLAuthGuard)
@Query(() => Path)
path(@Args('id') id: string) {
return this.pathService.findById(id);
}
@Roles('admin')
@Mutation(() => Path)
createPath(@Args('path') path: PathInput) {
return this.pathService.create(path);
}
@UseGuards(GQLAuthGuard)
@Mutation(() => Boolean)
async joinPath(
@Args('pathId') pathId: string,
@CurrentUser() user: User
) {
return Boolean(await this.pathService.addUserToPath(user.id, pathId));
}
@UseGuards(GQLAuthGuard)
@Mutation(() => Boolean)
async joinPaths(
@Args('paths', { type: () => [String] }) paths: string[],
@CurrentUser() user: User
) {
return Boolean(await this.pathService.addUserToPath(user.id, paths));
}
@Roles('admin')
@Mutation(() => Path)
async updatePath(
@Args('path') path: UpdatePathInput
) {
return this.pathService.update(path);
}
// ---------------------------------------------------------------------------
// -------------------------------------------------------------------- Fields
// ---------------------------------------------------------------------------
@ResolveField(() => Number)
async progress(
@CurrentUser() user: User,
@Parent() path: Path
) {
return (await this.pathUserService.getProgress(user.id, path.id)) || 0;
}
@ResolveField(() => [ModuleUnion])
async modules(
@Parent() path: Path
) {
return this.cms.findModulesByPathId(path.id);
}
}