Skip to content

Commit bf24cd9

Browse files
authored
Merge pull request #76 from GeneralMagicio/fix/pull_worldid_from_midleware
Pull worldlD from middleware instead or request query/body
2 parents c32a0c9 + c402818 commit bf24cd9

File tree

7 files changed

+66
-55
lines changed

7 files changed

+66
-55
lines changed

src/poll/Poll.dto.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import {
1212
} from 'class-validator';
1313

1414
export class CreatePollDto {
15-
@IsString()
16-
@IsNotEmpty()
17-
worldID: string;
18-
1915
@IsString()
2016
@IsNotEmpty()
2117
title: string;
@@ -48,10 +44,6 @@ export class CreatePollDto {
4844
}
4945

5046
export class GetPollsDto {
51-
@IsString()
52-
@IsOptional()
53-
worldID?: string;
54-
5547
@IsString()
5648
@IsOptional()
5749
search?: string;
@@ -91,9 +83,3 @@ export class GetPollsDto {
9183
@IsEnum(['asc', 'desc'])
9284
sortOrder?: 'asc' | 'desc';
9385
}
94-
95-
export class DeletePollDto {
96-
@IsString()
97-
@IsNotEmpty()
98-
worldID: string;
99-
}

src/poll/poll.controller.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ import {
77
Post,
88
Query,
99
} from '@nestjs/common';
10-
import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
10+
import { CreatePollDto, GetPollsDto } from './Poll.dto';
1111
import { PollService } from './poll.service';
12-
import { User } from 'src/auth/user.docerator';
12+
import { User } from 'src/auth/user.decorator';
1313

1414
@Controller('poll')
1515
export class PollController {
1616
constructor(private readonly pollService: PollService) {}
1717

1818
@Post()
19-
async createPoll(@Body() dto: CreatePollDto) {
20-
return await this.pollService.createPoll(dto);
19+
async createPoll(
20+
@Body() dto: CreatePollDto,
21+
@User('worldID') worldID: string,
22+
) {
23+
return await this.pollService.createPoll(dto, worldID);
2124
}
2225

2326
@Get()
2427
async getPolls(
2528
@Query() query: GetPollsDto,
2629
@User('worldID') worldID: string,
2730
) {
28-
query.worldID = worldID;
29-
return await this.pollService.getPolls(query);
31+
return await this.pollService.getPolls(query, worldID);
3032
}
3133

3234
@Get(':id')
@@ -35,8 +37,8 @@ export class PollController {
3537
}
3638

3739
@Delete(':id')
38-
async deletePoll(@Param('id') id: number, @Body() query: DeletePollDto) {
39-
const poll = await this.pollService.deletePoll(Number(id), query);
40+
async deletePoll(@Param('id') id: number, @User('worldID') worldID: string) {
41+
const poll = await this.pollService.deletePoll(Number(id), worldID);
4042
return { message: 'Poll deleted', poll };
4143
}
4244
}

src/poll/poll.service.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
UnauthorizedActionException,
1313
UserNotFoundException,
1414
} from '../common/exceptions';
15-
import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
15+
import { CreatePollDto, GetPollsDto } from './Poll.dto';
1616

1717
@Injectable()
1818
export class PollService {
@@ -51,17 +51,26 @@ export class PollService {
5151
return searchResults.map((result) => result.pollId);
5252
}
5353

54-
async createPoll(createPollDto: CreatePollDto) {
54+
async createPoll(createPollDto: CreatePollDto, worldID: string) {
5555
const user = await this.databaseService.user.findUnique({
56-
where: { worldID: createPollDto.worldID },
56+
where: { worldID },
5757
});
5858
if (!user) {
5959
throw new UserNotFoundException();
6060
}
6161
const startDate = new Date(createPollDto.startDate);
62+
// temporary fix by adding 1min for tiny delay in time from receiving the request
63+
const checkDate = new Date(startDate.getTime() + 60000);
6264
const endDate = new Date(createPollDto.endDate);
6365
const now = new Date();
64-
if (startDate < now) {
66+
67+
console.log('date', {
68+
checkDate,
69+
startDate,
70+
now,
71+
});
72+
73+
if (checkDate < now) {
6574
throw new BadRequestException('Start date cannot be in the past');
6675
}
6776
if (endDate <= startDate) {
@@ -97,7 +106,7 @@ export class PollService {
97106
});
98107
}
99108

100-
async getPolls(query: GetPollsDto) {
109+
async getPolls(query: GetPollsDto, worldID: string) {
101110
const {
102111
page = 1,
103112
limit = 10,
@@ -122,9 +131,9 @@ export class PollService {
122131
filters.OR = [{ startDate: { gt: now } }, { endDate: { lte: now } }];
123132
}
124133

125-
if ((userCreated || userVoted) && query.worldID) {
134+
if ((userCreated || userVoted) && worldID) {
126135
const user = await this.databaseService.user.findUnique({
127-
where: { worldID: query.worldID },
136+
where: { worldID },
128137
select: { id: true },
129138
});
130139

@@ -178,6 +187,9 @@ export class PollService {
178187
const [polls, total] = await this.databaseService.$transaction([
179188
this.databaseService.poll.findMany({
180189
where: filters,
190+
include: {
191+
author: true,
192+
},
181193
orderBy,
182194
skip,
183195
take: Number(limit),
@@ -208,9 +220,9 @@ export class PollService {
208220
return { poll, isActive, optionsTotalVotes, totalVotes };
209221
}
210222

211-
async deletePoll(pollId: number, query: DeletePollDto) {
223+
async deletePoll(pollId: number, worldID: string) {
212224
const user = await this.databaseService.user.findUnique({
213-
where: { worldID: query.worldID },
225+
where: { worldID },
214226
select: { id: true },
215227
});
216228
if (!user) {

src/user/user.controller.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
UserVotesResponseDto,
1515
} from './user.dto';
1616
import { UserService } from './user.service';
17+
import { User } from 'src/auth/user.decorator';
1718

1819
@Controller('user')
1920
export class UserController {
@@ -36,18 +37,25 @@ export class UserController {
3637
@Get('getUserVotes')
3738
async getUserVotes(
3839
@Query() query: GetUserVotesDto,
40+
@User('worldID') worldID: string,
3941
): Promise<UserVotesResponseDto> {
40-
return await this.userService.getUserVotes(query);
42+
return await this.userService.getUserVotes(query, worldID);
4143
}
4244

4345
@Post('setVote')
44-
async setVote(@Body() dto: SetVoteDto): Promise<SetVoteResponseDto> {
45-
return await this.userService.setVote(dto);
46+
async setVote(
47+
@Body() dto: SetVoteDto,
48+
@User('worldID') worldID: string,
49+
): Promise<SetVoteResponseDto> {
50+
return await this.userService.setVote(dto, worldID);
4651
}
4752

4853
@Post('editVote')
49-
async editVote(@Body() dto: EditVoteDto): Promise<EditVoteResponseDto> {
50-
return await this.userService.editVote(dto);
54+
async editVote(
55+
@Body() dto: EditVoteDto,
56+
@User('worldID') worldID: string,
57+
): Promise<EditVoteResponseDto> {
58+
return await this.userService.editVote(dto, worldID);
5159
}
5260

5361
@Post('createUser')

src/user/user.dto.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ export class GetUserVotesDto {
9595
@Validate(IsPositiveInteger)
9696
@Transform(({ value }) => parseInt(value, 10))
9797
pollId: number;
98-
99-
@IsString()
100-
@IsNotEmpty()
101-
worldID: string;
10298
}
10399

104100
export class UserVotesResponseDto {
101+
@IsNotEmpty()
102+
@IsString()
103+
voteID: string;
104+
105105
@IsNotEmpty()
106106
@IsString({ each: true })
107107
options: string[];
@@ -120,10 +120,6 @@ export class SetVoteDto {
120120
@Validate(IsPositiveInteger)
121121
pollId: number;
122122

123-
@IsNotEmpty()
124-
@IsString()
125-
worldID: string;
126-
127123
@IsNotEmpty()
128124
@Validate(IsRecordStringNumber)
129125
weightDistribution: Record<string, number>;
@@ -140,10 +136,6 @@ export class SetVoteResponseDto {
140136
}
141137

142138
export class EditVoteDto {
143-
@IsNotEmpty()
144-
@IsString()
145-
worldID: string;
146-
147139
@IsNotEmpty()
148140
@IsString()
149141
voteID: string;

src/user/user.service.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ export class UserService {
201201
return { userActions: actions };
202202
}
203203

204-
async getUserVotes(dto: GetUserVotesDto): Promise<UserVotesResponseDto> {
204+
async getUserVotes(
205+
dto: GetUserVotesDto,
206+
worldID: string,
207+
): Promise<UserVotesResponseDto> {
205208
const user = await this.databaseService.user.findUnique({
206-
where: { worldID: dto.worldID },
209+
where: { worldID },
207210
select: { id: true },
208211
});
209212
if (!user) {
@@ -221,21 +224,26 @@ export class UserService {
221224
pollId: dto.pollId,
222225
userId: user.id,
223226
},
224-
select: { votingPower: true, weightDistribution: true },
227+
select: {
228+
voteID: true,
229+
votingPower: true,
230+
weightDistribution: true,
231+
},
225232
});
226233
if (!vote) {
227234
throw new VoteNotFoundException();
228235
}
229236
return {
237+
voteID: vote.voteID,
230238
options: poll.options,
231239
votingPower: vote.votingPower,
232240
weightDistribution: vote.weightDistribution as Record<string, number>,
233241
};
234242
}
235243

236-
async setVote(dto: SetVoteDto): Promise<SetVoteResponseDto> {
244+
async setVote(dto: SetVoteDto, worldID: string): Promise<SetVoteResponseDto> {
237245
const user = await this.databaseService.user.findUnique({
238-
where: { worldID: dto.worldID },
246+
where: { worldID },
239247
select: { id: true },
240248
});
241249
if (!user) {
@@ -284,7 +292,10 @@ export class UserService {
284292
});
285293
}
286294

287-
async editVote(dto: EditVoteDto): Promise<EditVoteResponseDto> {
295+
async editVote(
296+
dto: EditVoteDto,
297+
worldID: string,
298+
): Promise<EditVoteResponseDto> {
288299
const vote = await this.databaseService.vote.findUnique({
289300
where: { voteID: dto.voteID },
290301
select: {
@@ -305,7 +316,7 @@ export class UserService {
305316
where: { id: vote.userId },
306317
select: { worldID: true },
307318
});
308-
if (user?.worldID !== dto.worldID) {
319+
if (user?.worldID !== worldID) {
309320
throw new UnauthorizedActionException();
310321
}
311322
this.validateWeightDistribution(dto.weightDistribution, vote.poll.options);

0 commit comments

Comments
 (0)