Skip to content

Commit 79ed21b

Browse files
committed
refactor poll controller
1 parent f48140b commit 79ed21b

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

src/auth/auth.controller.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Body, Controller, Get, Post, Req, Res } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
Body,
4+
Controller,
5+
Get,
6+
Post,
7+
Req,
8+
Res,
9+
} from '@nestjs/common';
210
import { MiniAppWalletAuthSuccessPayload } from '@worldcoin/minikit-js';
311
import { Request, Response } from 'express';
412
import { AuthService } from './auth.service';
@@ -33,28 +41,20 @@ export class AuthController {
3341
maxAge: 2 * 60 * 1000, //2 minutes
3442
});
3543

36-
return res.json({ nonce });
44+
return { nonce };
3745
}
3846

3947
@Post('verifyPayload')
4048
async verifyPayload(
4149
@Req() req: RequestWithCookies,
4250
@Body() body: IRequestPayload,
43-
@Res() res: Response,
4451
) {
4552
const { payload } = body;
4653
const storedNonce = req.cookies.siwe;
4754
if (!storedNonce) {
48-
return res.status(400).json({
49-
status: 'error',
50-
isValid: false,
51-
message: 'No nonce found in cookies',
52-
});
55+
throw new BadRequestException('No nonce found in cookies');
5356
}
54-
const validMessage = await this.authService.verifyPayload(
55-
payload,
56-
storedNonce,
57-
);
58-
return res.status(200).json({ isValid: validMessage });
57+
const isValid = await this.authService.verifyPayload(payload, storedNonce);
58+
return { isValid };
5959
}
6060
}

src/poll/poll.controller.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import {
66
Param,
77
Post,
88
Query,
9-
Req,
10-
Res,
119
} from '@nestjs/common';
12-
import { Response } from 'express';
1310
import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
1411
import { PollService } from './poll.service';
1512

@@ -23,28 +20,18 @@ export class PollController {
2320
}
2421

2522
@Get()
26-
async getPolls(
27-
@Req() req,
28-
@Query() query: GetPollsDto,
29-
@Res() res: Response,
30-
) {
31-
const polls = await this.pollService.getPolls(query);
32-
return res.status(200).json(polls);
23+
async getPolls(@Query() query: GetPollsDto) {
24+
return await this.pollService.getPolls(query);
3325
}
3426

3527
@Get(':id')
36-
async getPollDetails(@Param('id') id: number, @Res() res: Response) {
37-
const poll = await this.pollService.getPollDetails(Number(id));
38-
return res.status(200).json(poll);
28+
async getPollDetails(@Param('id') id: number) {
29+
return await this.pollService.getPollDetails(Number(id));
3930
}
4031

4132
@Delete(':id')
42-
async deletePoll(
43-
@Param('id') id: number,
44-
@Body() query: DeletePollDto,
45-
@Res() res: Response,
46-
) {
33+
async deletePoll(@Param('id') id: number, @Body() query: DeletePollDto) {
4734
const poll = await this.pollService.deletePoll(Number(id), query);
48-
return res.status(200).json({ message: 'Poll deleted', poll: poll });
35+
return { message: 'Poll deleted', poll };
4936
}
5037
}

0 commit comments

Comments
 (0)