-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections-entries.service.ts
executable file
·291 lines (265 loc) · 8.59 KB
/
collections-entries.service.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
} from "@nestjs/common";
import { CollectionEntry } from "./entities/collection-entry.entity";
import { InjectRepository } from "@nestjs/typeorm";
import { FindOptionsRelations, In, Repository } from "typeorm";
import { CreateCollectionEntryDto } from "./dto/create-collection-entry.dto";
import { ActivitiesQueueService } from "../../activities/activities-queue/activities-queue.service";
import { FindCollectionEntriesDto } from "./dto/find-collection-entries.dto";
import { buildBaseFindOptions } from "../../utils/buildBaseFindOptions";
import { ActivityType } from "../../activities/activities-queue/activities-queue.constants";
import { ReviewsService } from "../../reviews/reviews.service";
import { AchievementsQueueService } from "../../achievements/achievements-queue/achievements-queue.service";
import { AchievementCategory } from "../../achievements/achievements.constants";
import { Length } from "class-validator";
@Injectable()
export class CollectionsEntriesService {
private readonly relations: FindOptionsRelations<CollectionEntry> = {
collections: true,
game: false,
ownedPlatforms: true,
};
constructor(
@InjectRepository(CollectionEntry)
private collectionEntriesRepository: Repository<CollectionEntry>,
private activitiesQueueService: ActivitiesQueueService,
@Inject(forwardRef(() => ReviewsService))
private reviewsService: ReviewsService,
private achievementsQueueService: AchievementsQueueService,
) {}
async findOneById(id: string) {
return await this.collectionEntriesRepository.findOne({
where: {
id,
},
});
}
/**
* This is mainly used to check if a given entry exists in a given user's library.
* @param userId
* @param gameId
*/
async findOneByUserIdAndGameId(userId: string, gameId: number) {
return await this.collectionEntriesRepository.findOne({
where: {
collections: {
library: {
userId,
},
},
game: {
id: gameId,
},
},
relations: this.relations,
});
}
async findOneByUserIdAndGameIdOrFail(userId: string, gameId: number) {
const entry = await this.findOneByUserIdAndGameId(userId, gameId);
if (!entry) {
throw new HttpException(
"Collection entry not found.",
HttpStatus.NOT_FOUND,
);
}
return entry;
}
async findAllByIdIn(
collectionEntryIds: string[],
dto?: FindCollectionEntriesDto,
) {
const findOptions = buildBaseFindOptions<CollectionEntry>(dto);
return await this.collectionEntriesRepository.find({
...findOptions,
where: {
id: In(collectionEntryIds),
},
});
}
async findAllByCollectionId(
userId: string | undefined,
collectionId: string,
dto?: FindCollectionEntriesDto,
) {
const findOptions = buildBaseFindOptions<CollectionEntry>(dto);
const results = await this.collectionEntriesRepository.findAndCount({
...findOptions,
where: {
collections: [
{
id: collectionId,
isPublic: true,
},
{
id: collectionId,
library: {
userId,
},
},
],
},
relations: this.relations,
});
return results;
}
async findAllByUserIdWithPermissions(
userId: string | undefined,
targetUserId: string,
dto: FindCollectionEntriesDto,
) {
const isOwnQuery = userId === targetUserId;
const findOptions = buildBaseFindOptions(dto);
if (isOwnQuery) {
return await this.collectionEntriesRepository.findAndCount({
...findOptions,
where: {
collections: {
library: {
userId,
},
},
},
relations: this.relations,
});
}
return await this.collectionEntriesRepository.findAndCount({
...findOptions,
where: {
collections: {
isPublic: true,
library: {
userId,
},
},
},
relations: this.relations,
});
}
async findFavoritesByUserId(
userId: string | undefined,
targetUserId: string,
dto: FindCollectionEntriesDto,
) {
const findOptions = buildBaseFindOptions(dto);
const isOwnQuery = userId === targetUserId;
if (isOwnQuery) {
return await this.collectionEntriesRepository.findAndCount({
...findOptions,
where: {
isFavorite: true,
collections: {
library: {
userId,
},
},
},
relations: this.relations,
});
}
return await this.collectionEntriesRepository.findAndCount({
...findOptions,
where: {
isFavorite: true,
collections: {
library: {
userId: targetUserId,
},
isPublic: true,
},
},
});
}
/**
* Create or update a user's Collection Entry
* @param userId
* @param createEntryDto
*/
async createOrUpdate(
userId: string,
createEntryDto: CreateCollectionEntryDto,
) {
const { collectionIds, gameId, platformIds, isFavorite } =
createEntryDto;
const uniqueCollectionIds = Array.from(new Set(collectionIds));
const uniquePlatformIds = Array.from(new Set(platformIds));
const collections = uniqueCollectionIds.map((id) => ({
id: id,
}));
const ownedPlatforms = uniquePlatformIds.map((id) => ({
id: id,
}));
const entry = await this.findOneByUserIdAndGameId(userId, gameId);
const upsertedEntry = await this.collectionEntriesRepository.save({
...entry,
isFavorite,
collections,
game: {
id: gameId,
},
ownedPlatforms,
});
this.activitiesQueueService.addActivity({
sourceId: upsertedEntry.id,
type: ActivityType.COLLECTION_ENTRY,
profileUserId: userId,
metadata: null,
});
this.achievementsQueueService.addTrackingJob({
targetUserId: userId,
category: AchievementCategory.COLLECTIONS,
});
}
async changeFavoriteStatus(
userId: string,
gameId: number,
isFavorite: boolean,
) {
const entity = await this.findOneByUserIdAndGameIdOrFail(
userId,
gameId,
);
await this.collectionEntriesRepository.update(
{
id: entity.id,
},
{
isFavorite,
},
);
}
/**
* Removes a collection entry, and detaches it's dependencies (like collections, platforms and review).
* @param userId
* @param entryId
*/
async delete(userId: string, entryId: string) {
const entry = await this.collectionEntriesRepository.findOne({
where: {
id: entryId,
collections: {
library: {
userId,
},
},
},
relations: {
ownedPlatforms: true,
review: true,
},
});
if (!entry) {
throw new HttpException("Entry not found.", HttpStatus.NOT_FOUND);
}
// This also deletes entries in the many-to-many tables.
await this.collectionEntriesRepository.delete(entry.id);
if (entry.review) {
await this.reviewsService.delete(userId, entry.review.id);
}
this.activitiesQueueService.deleteActivity(entry.id);
}
}