-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsectors.resolver.ts
204 lines (180 loc) · 5.92 KB
/
sectors.resolver.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
Resolver,
Mutation,
Args,
ResolveField,
Parent,
Query,
} from '@nestjs/graphql';
import { Roles } from '../../auth/decorators/roles.decorator';
import {
UseInterceptors,
UseFilters,
UseGuards,
BadRequestException,
ForbiddenException,
} from '@nestjs/common';
import { AuditInterceptor } from '../../audit/interceptors/audit.interceptor';
import { NotFoundFilter } from '../filters/not-found.filter';
import { Sector } from '../entities/sector.entity';
import { SectorsService } from '../services/sectors.service';
import { CreateSectorInput } from '../dtos/create-sector.input';
import { UpdateSectorInput } from '../dtos/update-sector.input';
import { Route } from '../entities/route.entity';
import { AllowAny } from '../../auth/decorators/allow-any.decorator';
import { UserAuthGuard } from '../../auth/guards/user-auth.guard';
import { ForeignKeyConstraintFilter } from '../filters/foreign-key-constraint.filter';
import { CurrentUser } from '../../auth/decorators/current-user.decorator';
import { User } from '../../users/entities/user.entity';
import { NotificationService } from '../../notification/services/notification.service';
import { SectorRoutesLoader } from '../loaders/sector-routes.loader';
import DataLoader from 'dataloader';
import {
DataLoaderInterceptor,
Loader,
} from '../../core/interceptors/data-loader.interceptor';
import { CragsService } from '../services/crags.service';
import { UserLoader } from '../../users/loaders/user.loader';
import { Parking } from '../entities/parking.entity';
import { ParkingsService } from '../services/parkings.service';
@Resolver(() => Sector)
@UseInterceptors(DataLoaderInterceptor)
export class SectorsResolver {
constructor(
private cragsService: CragsService,
private sectorsService: SectorsService,
private notificationService: NotificationService,
private parkingsService: ParkingsService,
) {}
/* QUERIES */
@Query(() => Sector)
@UseFilters(NotFoundFilter)
@AllowAny()
@UseGuards(UserAuthGuard)
sector(@Args('id') id: string, @CurrentUser() user: User): Promise<Sector> {
return this.sectorsService.findOne({ id: id, user: user });
}
/* MUTATIONS */
@Mutation(() => Sector)
@UseGuards(UserAuthGuard)
@UseInterceptors(AuditInterceptor)
@UseFilters(NotFoundFilter)
async createSector(
@Args('input', { type: () => CreateSectorInput }) input: CreateSectorInput,
@CurrentUser() user: User,
): Promise<Sector> {
if (!user.isAdmin() && input.publishStatus == 'published') {
throw new BadRequestException();
}
return this.sectorsService.create(input, user);
}
@Mutation(() => Sector)
@UseGuards(UserAuthGuard)
@UseFilters(NotFoundFilter)
@UseInterceptors(AuditInterceptor)
async updateSector(
@Args('input', { type: () => UpdateSectorInput }) input: UpdateSectorInput,
@CurrentUser() user: User,
): Promise<Sector> {
const sector = await this.sectorsService.findOne({
id: input.id,
user,
});
if (!(await user.isAdmin()) && sector.publishStatus != 'draft') {
throw new ForbiddenException();
}
if (!(await user.isAdmin()) && input.publishStatus == 'published') {
throw new BadRequestException('publish_status_unavailable_to_user');
}
const crag = await sector.crag;
if (
input.publishStatus != null &&
crag.publishStatus < input.publishStatus
) {
throw new BadRequestException('publish_status_incompatible_with_crag');
}
if (sector.publishStatus == 'in_review' && input.publishStatus == 'draft') {
this.notificationService.contributionRejection(
{ sector: sector },
await sector.user,
user,
input.rejectionMessage,
);
}
return this.sectorsService.update(input);
}
@Mutation(() => [Sector])
@Roles('admin')
@UseInterceptors(AuditInterceptor)
@UseFilters(NotFoundFilter)
async updateSectors(
@Args('input', { type: () => [UpdateSectorInput] })
input: UpdateSectorInput[],
): Promise<Sector[]> {
return Promise.all(input.map((input) => this.sectorsService.update(input)));
}
@Mutation(() => Boolean)
@UseGuards(UserAuthGuard)
@UseInterceptors(AuditInterceptor)
@UseFilters(NotFoundFilter, ForeignKeyConstraintFilter)
async deleteSector(
@Args('id') id: string,
@CurrentUser() user: User,
): Promise<boolean> {
const sector = await this.sectorsService.findOne({
id: id,
user,
});
if (!user.isAdmin() && sector.publishStatus != 'draft') {
throw new ForbiddenException();
}
return this.sectorsService.delete(id);
}
@Mutation(() => Boolean)
@UseGuards(UserAuthGuard)
@UseFilters(NotFoundFilter)
@UseInterceptors(AuditInterceptor)
async moveSectorToCrag(
@Args('id') id: string,
@Args('cragId') cragId: string,
@CurrentUser() user: User,
): Promise<boolean> {
const sector = await this.sectorsService.findOne({
id,
user,
});
const crag = await this.cragsService.findOne({
id: cragId,
user,
});
if (!user.isAdmin()) {
throw new ForbiddenException();
}
return this.sectorsService.moveToCrag(sector, crag);
}
/* FIELDS */
@ResolveField('routes', () => [Route])
async getRoutes(
@Parent() sector: Sector,
@Loader(SectorRoutesLoader)
loader: DataLoader<Route['id'], Route[]>,
): Promise<Route[]> {
return loader.load(sector.id);
}
@ResolveField('bouldersOnly', () => Boolean)
async bouldersOnly(@Parent() sector: Sector) {
return this.sectorsService.bouldersOnly(sector.id);
}
@ResolveField('user', () => User, { nullable: true })
async getUser(
@Parent() sector: Sector,
@Loader(UserLoader)
loader: DataLoader<Sector['userId'], User>,
): Promise<User> {
return sector.userId ? loader.load(sector.userId) : null;
}
@ResolveField('parkings', () => [Parking])
async getParkings(@Parent() sector: Sector): Promise<Parking[]> {
return this.parkingsService.getParkings(sector.id);
}
}