Skip to content

Commit

Permalink
Fix/notice like 2 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 authored Sep 26, 2024
1 parent 3903563 commit a907049
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ repos:
name: Run ESLint
entry: npm run lint
language: system
types: [javascript]
types: [javascript, ts]
- id: npm-run-format
name: Run Prettier
entry: npm run format
language: system
types: [javascript]
types: [javascript, ts]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"dev": "nest start --watch",
"format": "prettier --w **/*.ts",
"format:check": "prettier --c **/*.ts",
"lint": "eslint . --ext .ts --ignore-pattern dist/",
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class AuthController {

@Get('me/reservation')
@UseGuards(JwtAuthGuard)
async getOwnReservations(@Req() req) {
async getOwnReservations(@Req() req: Request) {
const user = req.user as JwtPayload;
const existUser = await this.userService.findOneByEmail(user.email);

Expand Down
14 changes: 12 additions & 2 deletions src/popo/noticeLike/noticeLike.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NoticeLikeController } from './noticeLike.controller';
import { NoticeLikeService } from './noticeLike.service';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { NoticeLike } from './noticeLike.entity';
import { JwtPayload } from 'src/auth/strategies/jwt.payload';

describe('NoticeLikeController', () => {
let controller: NoticeLikeController;
Expand Down Expand Up @@ -58,7 +59,11 @@ describe('NoticeLikeController', () => {
const noticeId = 1;
noticeLikeService.findByUserIdAndNoticeId.mockResolvedValue(null);

await expect(controller.delete(userId, noticeId)).rejects.toThrow();
const user = { uuid: '1' } as JwtPayload;

await expect(
controller.delete(userId, noticeId, { user: user }),
).rejects.toThrow();
});

it('should delete like', async () => {
Expand All @@ -70,10 +75,15 @@ describe('NoticeLikeController', () => {
notice_id: 1,
created_at: new Date(),
};

const user = { uuid: '1' } as JwtPayload;

const mockedDeletedResult = { affected: 1, raw: null };
noticeLikeService.findByUserIdAndNoticeId.mockResolvedValue(like);
noticeLikeService.delete.mockResolvedValue(mockedDeletedResult);

expect(await controller.delete(userId, noticeId)).toBe(mockedDeletedResult);
expect(await controller.delete(userId, noticeId, { user: user })).toBe(
mockedDeletedResult,
);
});
});
33 changes: 23 additions & 10 deletions src/popo/noticeLike/noticeLike.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {
Controller,
Delete,
Get,
Param,
Post,
Query,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiBody, ApiTags } from '@nestjs/swagger';
import { Request } from 'express';

import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { NoticeLikeDto } from './noticeLike.dto';
Expand All @@ -29,7 +30,10 @@ export class NoticeLikeController {
@Post()
@UseGuards(JwtAuthGuard)
@ApiBody({ type: NoticeLikeDto })
async create(@Body() dto: NoticeLikeDto, @Req() req): Promise<NoticeLike> {
async create(
@Body() dto: NoticeLikeDto,
@Req() req: Request,
): Promise<NoticeLike> {
const user = req.user as JwtPayload;

if (user.uuid != dto.user_id) {
Expand All @@ -39,16 +43,19 @@ export class NoticeLikeController {
return this.noticeLikeService.save(dto);
}

@Get('count')
countLikes(@Query('notice_id') notice_id: number): Promise<number> {
@Get('count/:notice_id')
countLikes(@Param('notice_id') notice_id: number): Promise<number> {
return this.noticeLikeService.countLikes(notice_id);
}

@Get('status')
@Get('status/:user_id/:notice_id')
async getStatus(
@Query('user_id') user_id: string,
@Query('notice_id') notice_id: number,
@Param('user_id') user_id: string,
@Param('notice_id') notice_id: number,
): Promise<boolean> {
if (!user_id || !notice_id) {
return false;
}
return (await this.noticeLikeService.findByUserIdAndNoticeId(
user_id,
notice_id,
Expand All @@ -57,12 +64,18 @@ export class NoticeLikeController {
: false;
}

@Delete()
@Delete(':user_id/:notice_id')
@UseGuards(JwtAuthGuard)
async delete(
@Query('user_id') user_id: string,
@Query('notice_id') notice_id: number,
@Param('user_id') user_id: string,
@Param('notice_id') notice_id: number,
@Req() req: Request | any,
) {
const user = req.user as JwtPayload;
if (user.uuid != user_id) {
throw new BadRequestException('User ID does not match.');
}

const target = await this.noticeLikeService.findByUserIdAndNoticeId(
user_id,
notice_id,
Expand Down
8 changes: 4 additions & 4 deletions src/popo/reservation/equip/reserve.equip.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
UseGuards,
} from '@nestjs/common';
import { ApiQuery, ApiTags } from '@nestjs/swagger';

import { Request } from 'express';
import { ReserveEquipService } from './reserve.equip.service';
import { CreateReserveEquipDto } from './reserve.equip.dto';
import { MailService } from '../../../mail/mail.service';
Expand All @@ -38,7 +38,7 @@ export class ReserveEquipController {

@Post()
@UseGuards(JwtAuthGuard)
async post(@Req() req, @Body() dto: CreateReserveEquipDto) {
async post(@Req() req: Request, @Body() dto: CreateReserveEquipDto) {
const user = req.user as JwtPayload;

const saveDto = Object.assign(dto, { booker_id: user.uuid });
Expand Down Expand Up @@ -118,7 +118,7 @@ export class ReserveEquipController {

@Get('user')
@UseGuards(JwtAuthGuard)
async getMyReservation(@Req() req) {
async getMyReservation(@Req() req: Request) {
const user = req.user as JwtPayload;

const reservations = await this.reserveEquipService.find({
Expand Down Expand Up @@ -166,7 +166,7 @@ export class ReserveEquipController {

@Delete(':uuid')
@UseGuards(JwtAuthGuard)
async delete(@Param('uuid') uuid: string, @Req() req) {
async delete(@Param('uuid') uuid: string, @Req() req: Request) {
const reservation = await this.reserveEquipService.findOneByUuid(uuid);
const user = req.user as JwtPayload;

Expand Down
7 changes: 5 additions & 2 deletions src/popo/reservation/place/reserve.place.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export class ReservePlaceController {

@Post()
@UseGuards(JwtAuthGuard)
async createWithNameAndId(@Req() req, @Body() dto: CreateReservePlaceDto) {
async createWithNameAndId(
@Req() req: Request,
@Body() dto: CreateReservePlaceDto,
) {
const user = req.user as JwtPayload;
const existPlace = await this.placeService.findOneByUuidOrFail(
dto.place_id,
Expand Down Expand Up @@ -290,7 +293,7 @@ export class ReservePlaceController {

@Delete(':uuid')
@UseGuards(JwtAuthGuard)
async delete(@Param('uuid') uuid: string, @Req() req) {
async delete(@Param('uuid') uuid: string, @Req() req: Request) {
const reservation =
await this.reservePlaceService.findOneByUuidOrFail(uuid);
const user = req.user as JwtPayload;
Expand Down

0 comments on commit a907049

Please sign in to comment.