Skip to content

Commit 7ca21c4

Browse files
committed
added createuser endpoint
1 parent efba03e commit 7ca21c4

File tree

6 files changed

+114
-11
lines changed

6 files changed

+114
-11
lines changed

src/poll/Poll.dto.ts

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

1414
export class CreatePollDto {
15+
@IsString()
16+
@IsNotEmpty()
17+
worldID: string;
18+
1519
@IsString()
1620
@IsNotEmpty()
1721
title: string;
@@ -44,6 +48,10 @@ export class CreatePollDto {
4448
}
4549

4650
export class GetPollsDto {
51+
@IsString()
52+
@IsOptional()
53+
worldID?: string;
54+
4755
@IsOptional()
4856
@IsInt()
4957
@Min(1)

src/poll/poll.controller.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,29 @@ export class PollController {
2222
@Post()
2323
@UsePipes(ValidationPipe)
2424
create(@Body() createPollDto: CreatePollDto) {
25-
const userId = 1; // need to implement Auth
26-
return this.pollService.createPoll(userId, createPollDto);
25+
return this.pollService.createPoll(createPollDto);
2726
}
2827

2928
@Get()
3029
@UsePipes(ValidationPipe)
31-
getPolls(@Req() req, @Query() query: GetPollsDto) {
32-
const userId = 1;
33-
return this.pollService.getPolls(userId, query);
30+
async getPolls(
31+
@Req() req,
32+
@Query() query: GetPollsDto,
33+
@Res() res: Response,
34+
) {
35+
try {
36+
const polls = await this.pollService.getPolls(query);
37+
return res.status(200).json(polls);
38+
} catch (error) {
39+
if (error.message === 'User not found') {
40+
return res.status(404).json({ message: error.message });
41+
} else if (error.message === 'worldId Not Provided') {
42+
return res.status(404).json({ message: error.message });
43+
}
44+
return res
45+
.status(500)
46+
.json({ message: 'Internal server error', error: error.message });
47+
}
3448
}
3549

3650
@Get(':id')

src/poll/poll.service.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { CreatePollDto, GetPollsDto } from './Poll.dto';
77
export class PollService {
88
constructor(private readonly databaseService: DatabaseService) {}
99

10-
async createPoll(userId: number, createPollDto: CreatePollDto) {
10+
async createPoll(createPollDto: CreatePollDto) {
1111
const user = await this.databaseService.user.findUnique({
12-
where: { id: userId },
12+
where: { worldID: createPollDto.worldID },
1313
});
1414
if (!user) {
1515
throw new BadRequestException('User does not exist');
@@ -30,7 +30,7 @@ export class PollService {
3030
// Create the poll
3131
const newPoll = await tx.poll.create({
3232
data: {
33-
authorUserId: userId,
33+
authorUserId: user.id,
3434
title: createPollDto.title,
3535
description: createPollDto.description,
3636
options: createPollDto.options,
@@ -45,15 +45,15 @@ export class PollService {
4545
// Create user action for CREATED
4646
await tx.userAction.create({
4747
data: {
48-
userId,
48+
userId: user.id,
4949
pollId: newPoll.pollId,
5050
type: ActionType.CREATED,
5151
},
5252
});
5353

5454
// Update user's pollsCreatedCount
5555
await tx.user.update({
56-
where: { id: userId },
56+
where: { worldID: createPollDto.worldID },
5757
data: {
5858
pollsCreatedCount: {
5959
increment: 1,
@@ -65,7 +65,7 @@ export class PollService {
6565
});
6666
}
6767

68-
async getPolls(userId: number, query: GetPollsDto) {
68+
async getPolls(query: GetPollsDto) {
6969
const {
7070
page = 1,
7171
limit = 10,
@@ -78,10 +78,27 @@ export class PollService {
7878
const skip = (page - 1) * limit;
7979
const now = new Date();
8080
const filters: any = {};
81+
let userId: number | undefined;
82+
8183
if (isActive) {
8284
filters.startDate = { lte: now };
8385
filters.endDate = { gt: now };
8486
}
87+
88+
if ((userCreated || userVoted) && query.worldID) {
89+
const user = await this.databaseService.user.findUnique({
90+
where: { worldID: query.worldID },
91+
select: { id: true },
92+
});
93+
94+
if (!user) {
95+
throw new Error('User not found');
96+
}
97+
userId = user.id;
98+
} else if (userCreated || userVoted) {
99+
throw new Error('worldId Not Provided');
100+
}
101+
85102
if (userCreated) {
86103
filters.authorUserId = userId;
87104
}

src/user/user.controller.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import {
55
Get,
66
Post,
77
Query,
8+
UsePipes,
9+
ValidationPipe,
810
} from '@nestjs/common';
911
import { UserService } from './user.service';
1012
import {
13+
CreateUserDto,
14+
CreateUserResponseDto,
1115
EditVoteDto,
1216
EditVoteResponseDto,
1317
GetUserActivitiesDto,
@@ -84,4 +88,15 @@ export class UserController {
8488
throw new BadRequestException(errorMessage);
8589
}
8690
}
91+
92+
@Post('createUser')
93+
async createUser(@Body() dto: CreateUserDto): Promise<CreateUserResponseDto> {
94+
try {
95+
return await this.userService.createUser(dto);
96+
} catch (error: unknown) {
97+
const errorMessage =
98+
error instanceof Error ? error.message : 'An unexpected error occurred';
99+
throw new BadRequestException(errorMessage);
100+
}
101+
}
87102
}

src/user/user.dto.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
2+
13
export class GetUserDataDto {
24
worldID: string;
35
}
@@ -60,3 +62,21 @@ export class EditVoteDto {
6062
export class EditVoteResponseDto {
6163
actionId: number;
6264
}
65+
66+
export class CreateUserDto {
67+
@IsString()
68+
@IsNotEmpty()
69+
name: string;
70+
71+
@IsString()
72+
@IsNotEmpty()
73+
worldID: string;
74+
75+
@IsString()
76+
@IsOptional()
77+
profilePicture?: string;
78+
}
79+
80+
export class CreateUserResponseDto {
81+
userId: number;
82+
}

src/user/user.service.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
SetVoteResponseDto,
1313
EditVoteDto,
1414
EditVoteResponseDto,
15+
CreateUserDto,
16+
CreateUserResponseDto,
1517
} from './user.dto';
1618
import { ActionType } from '@prisma/client';
1719

@@ -232,4 +234,31 @@ export class UserService {
232234
actionId: userAction.id,
233235
};
234236
}
237+
238+
async createUser(dto: CreateUserDto): Promise<CreateUserResponseDto> {
239+
const existingUser = await this.databaseService.user.findUnique({
240+
where: { worldID: dto.worldID },
241+
});
242+
if (existingUser) {
243+
return {
244+
userId: existingUser?.id,
245+
};
246+
}
247+
248+
const newUser = await this.databaseService.user.create({
249+
data: {
250+
name: dto.name,
251+
worldID: dto.worldID,
252+
profilePicture: dto.profilePicture || null,
253+
},
254+
});
255+
256+
if (!newUser) {
257+
throw new Error('User not created');
258+
}
259+
260+
return {
261+
userId: newUser.id,
262+
};
263+
}
235264
}

0 commit comments

Comments
 (0)