Skip to content

Commit c9452cd

Browse files
authored
Merge pull request #75 from GeneralMagicio/update-polls-count-on-delete-poll
Update polls count on deletePoll
2 parents ca61022 + c50666f commit c9452cd

File tree

4 files changed

+91
-67
lines changed

4 files changed

+91
-67
lines changed

src/poll/poll.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { Module } from '@nestjs/common';
1+
import { forwardRef, Module } from '@nestjs/common';
22
import { PollService } from './poll.service';
33
import { PollController } from './poll.controller';
4+
import { UserModule } from '../user/user.module';
5+
import { UserService } from '../user/user.service';
46

57
@Module({
8+
imports: [forwardRef(() => UserModule)],
69
controllers: [PollController],
7-
providers: [PollService],
10+
providers: [PollService, UserService],
811
})
912
export class PollModule {}

src/poll/poll.service.ts

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { BadRequestException, Injectable } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
forwardRef,
4+
Inject,
5+
Injectable,
6+
} from '@nestjs/common';
27
import { ActionType, Prisma } from '@prisma/client';
38
import { DatabaseService } from 'src/database/database.service';
9+
import { UserService } from 'src/user/user.service';
410
import {
511
PollNotFoundException,
612
UnauthorizedActionException,
@@ -10,7 +16,25 @@ import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
1016

1117
@Injectable()
1218
export class PollService {
13-
constructor(private readonly databaseService: DatabaseService) {}
19+
constructor(
20+
private readonly databaseService: DatabaseService,
21+
@Inject(forwardRef(() => UserService))
22+
private readonly userService: UserService,
23+
) {}
24+
25+
async updatePollParticipantCount(
26+
pollId: number,
27+
prismaClient?: Prisma.TransactionClient, // To ensure Prisma transaction function runs queries in order
28+
) {
29+
const prisma = prismaClient || this.databaseService;
30+
const participantCount = await prisma.userAction.count({
31+
where: { pollId, type: ActionType.VOTED },
32+
});
33+
await prisma.poll.update({
34+
where: { pollId },
35+
data: { participantCount },
36+
});
37+
}
1438

1539
private async searchPolls(searchTerm: string): Promise<number[]> {
1640
const searchQuery = searchTerm
@@ -64,18 +88,11 @@ export class PollService {
6488
type: ActionType.CREATED,
6589
},
6690
});
67-
const pollsCreatedCount = await tx.userAction.count({
68-
where: {
69-
userId: user.id,
70-
type: ActionType.CREATED,
71-
},
72-
});
73-
await tx.user.update({
74-
where: { worldID: createPollDto.worldID },
75-
data: {
76-
pollsCreatedCount,
77-
},
78-
});
91+
await this.userService.updateUserPollsCount(
92+
user.id,
93+
ActionType.CREATED,
94+
tx,
95+
);
7996
return newPoll;
8097
});
8198
}
@@ -209,19 +226,30 @@ export class PollService {
209226
throw new UnauthorizedActionException();
210227
}
211228
return this.databaseService.$transaction(async (tx) => {
229+
const pollParticipants = await tx.userAction.findMany({
230+
where: { pollId, type: ActionType.VOTED },
231+
select: { userId: true },
232+
});
233+
const participantUserIds = [
234+
...new Set(pollParticipants.map((v) => v.userId)),
235+
];
212236
const deleted = await tx.poll.delete({
213237
where: {
214238
pollId,
215239
},
216240
});
217-
await tx.user.update({
218-
where: { id: deleted.authorUserId },
219-
data: {
220-
pollsCreatedCount: {
221-
decrement: 1,
222-
},
223-
},
224-
});
241+
await this.userService.updateUserPollsCount(
242+
deleted.authorUserId,
243+
ActionType.CREATED,
244+
tx,
245+
);
246+
for (const userId of participantUserIds) {
247+
await this.userService.updateUserPollsCount(
248+
userId,
249+
ActionType.VOTED,
250+
tx,
251+
);
252+
}
225253
return deleted;
226254
});
227255
}

src/user/user.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { Module } from '@nestjs/common';
2-
import { PollService } from '../poll/poll.service';
1+
import { forwardRef, Module } from '@nestjs/common';
32
import { UserController } from './user.controller';
43
import { UserService } from './user.service';
4+
import { PollModule } from '../poll/poll.module';
5+
import { PollService } from '../poll/poll.service';
56

67
@Module({
8+
imports: [forwardRef(() => PollModule)],
79
controllers: [UserController],
810
providers: [UserService, PollService],
911
})

src/user/user.service.ts

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Injectable } from '@nestjs/common';
2-
import { ActionType } from '@prisma/client';
1+
import { forwardRef, Inject, Injectable } from '@nestjs/common';
2+
import { ActionType, Prisma } from '@prisma/client';
33
import { VOTING_POWER } from '../common/constants';
44
import {
55
CreateUserException,
@@ -47,6 +47,7 @@ type UserActionFilters = {
4747
export class UserService {
4848
constructor(
4949
private readonly databaseService: DatabaseService,
50+
@Inject(forwardRef(() => PollService))
5051
private readonly pollService: PollService,
5152
) {}
5253

@@ -82,29 +83,41 @@ export class UserService {
8283
}
8384
}
8485

86+
async updateUserPollsCount(
87+
userId: number,
88+
type: ActionType,
89+
prismaClient?: Prisma.TransactionClient, // To ensure Prisma transaction function runs queries in order
90+
) {
91+
const prisma = prismaClient || this.databaseService;
92+
const pollsCount = await prisma.userAction.count({
93+
where: { userId, type },
94+
});
95+
await prisma.user.update({
96+
where: { id: userId },
97+
data: {
98+
pollsCreatedCount: type === ActionType.CREATED ? pollsCount : undefined,
99+
pollsParticipatedCount:
100+
type === ActionType.VOTED ? pollsCount : undefined,
101+
},
102+
});
103+
}
104+
85105
async getUserData(dto: GetUserDataDto): Promise<UserDataResponseDto> {
86106
const user = await this.databaseService.user.findUnique({
87107
where: { worldID: dto.worldID },
88-
select: { id: true, worldID: true },
108+
select: {
109+
id: true,
110+
worldID: true,
111+
pollsCreatedCount: true,
112+
pollsParticipatedCount: true,
113+
},
89114
});
90115
if (!user) {
91116
throw new UserNotFoundException();
92117
}
93-
const pollsCreated = await this.databaseService.userAction.count({
94-
where: {
95-
userId: user.id,
96-
type: ActionType.CREATED,
97-
},
98-
});
99-
const pollsParticipated = await this.databaseService.userAction.count({
100-
where: {
101-
userId: user.id,
102-
type: ActionType.VOTED,
103-
},
104-
});
105118
return {
106-
pollsCreated,
107-
pollsParticipated,
119+
pollsCreated: user.pollsCreatedCount,
120+
pollsParticipated: user.pollsParticipatedCount,
108121
worldID: user.worldID,
109122
worldProfilePic: null,
110123
};
@@ -262,30 +275,8 @@ export class UserService {
262275
type: ActionType.VOTED,
263276
},
264277
});
265-
const pollsParticipatedCount = await prisma.userAction.count({
266-
where: {
267-
userId: user.id,
268-
type: ActionType.VOTED,
269-
},
270-
});
271-
const participantCount = await prisma.userAction.count({
272-
where: {
273-
pollId: dto.pollId,
274-
type: ActionType.VOTED,
275-
},
276-
});
277-
await prisma.user.update({
278-
where: { worldID: dto.worldID },
279-
data: {
280-
pollsParticipatedCount,
281-
},
282-
});
283-
await prisma.poll.update({
284-
where: { pollId: dto.pollId },
285-
data: {
286-
participantCount,
287-
},
288-
});
278+
await this.updateUserPollsCount(user.id, ActionType.VOTED, prisma);
279+
await this.pollService.updatePollParticipantCount(dto.pollId, prisma);
289280
return {
290281
voteID: vote.voteID,
291282
actionId: action.id,

0 commit comments

Comments
 (0)