Skip to content

Commit 641d1cf

Browse files
committed
update pollsCreatedCount on create poll
1 parent 8f3a492 commit 641d1cf

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/poll/poll.service.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { BadRequestException, Injectable } from '@nestjs/common';
22
import { ActionType } from '@prisma/client';
33
import { DatabaseService } from 'src/database/database.service';
4-
import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
54
import {
65
PollNotFoundException,
76
UnauthorizedActionException,
87
UserNotFoundException,
98
} from '../common/exceptions';
9+
import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
1010

1111
@Injectable()
1212
export class PollService {
@@ -22,17 +22,13 @@ export class PollService {
2222
const startDate = new Date(createPollDto.startDate);
2323
const endDate = new Date(createPollDto.endDate);
2424
const now = new Date();
25-
2625
if (startDate < now) {
2726
throw new BadRequestException('Start date cannot be in the past');
2827
}
29-
3028
if (endDate <= startDate) {
3129
throw new BadRequestException('End date must be after start date');
3230
}
33-
3431
return this.databaseService.$transaction(async (tx) => {
35-
// Create the poll
3632
const newPoll = await tx.poll.create({
3733
data: {
3834
authorUserId: user.id,
@@ -43,29 +39,28 @@ export class PollService {
4339
endDate,
4440
tags: createPollDto.tags || [],
4541
isAnonymous: createPollDto.isAnonymous || false,
46-
voteResults: {}, // Initialize empty vote results
42+
voteResults: {},
4743
},
4844
});
49-
50-
// Create user action for CREATED
5145
await tx.userAction.create({
5246
data: {
5347
userId: user.id,
5448
pollId: newPoll.pollId,
5549
type: ActionType.CREATED,
5650
},
5751
});
58-
59-
// Update user's pollsCreatedCount
52+
const pollsCreatedCount = await tx.userAction.count({
53+
where: {
54+
userId: user.id,
55+
type: ActionType.CREATED,
56+
},
57+
});
6058
await tx.user.update({
6159
where: { worldID: createPollDto.worldID },
6260
data: {
63-
pollsCreatedCount: {
64-
increment: 1,
65-
},
61+
pollsCreatedCount,
6662
},
6763
});
68-
6964
return newPoll;
7065
});
7166
}
@@ -199,8 +194,6 @@ export class PollService {
199194
pollId,
200195
},
201196
});
202-
203-
// Update user's pollsCreatedCount
204197
await tx.user.update({
205198
where: { id: deleted.authorUserId },
206199
data: {
@@ -209,7 +202,6 @@ export class PollService {
209202
},
210203
},
211204
});
212-
213205
return deleted;
214206
});
215207
}

0 commit comments

Comments
 (0)