Files
zod-backend/src/gift/controllers/gifts.controller.ts

83 lines
2.9 KiB
TypeScript

import { Body, Controller, Get, HttpCode, HttpStatus, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Roles } from '~/auth/enums';
import { IJwtPayload } from '~/auth/interfaces';
import { AllowedRoles, AuthenticatedUser } from '~/common/decorators';
import { AccessTokenGuard, RolesGuard } from '~/common/guards';
import { ApiDataPageResponse, ApiDataResponse, ApiLangRequestHeader } from '~/core/decorators';
import { ResponseFactory } from '~/core/utils';
import { CreateGiftRequestDto, GiftFiltersRequestDto, GiftReplyRequestDto } from '../dtos/request';
import { GiftDetailsResponseDto, GiftListResponseDto } from '../dtos/response';
import { GiftsService } from '../services';
@Controller('gift')
@ApiTags('Gifts')
@ApiBearerAuth()
@ApiLangRequestHeader()
export class GiftsController {
constructor(private readonly giftsService: GiftsService) {}
@Post()
@UseGuards(RolesGuard)
@AllowedRoles(Roles.GUARDIAN)
@ApiDataResponse(GiftDetailsResponseDto)
async createGift(@AuthenticatedUser() { sub }: IJwtPayload, @Body() body: CreateGiftRequestDto) {
const gift = await this.giftsService.createGift(sub, body);
return ResponseFactory.data(new GiftDetailsResponseDto(gift));
}
@Get()
@UseGuards(AccessTokenGuard)
@ApiDataPageResponse(GiftListResponseDto)
async findGifts(@AuthenticatedUser() user: IJwtPayload, @Query() filters: GiftFiltersRequestDto) {
const [gifts, itemCount] = await this.giftsService.findGifts(user, filters);
return ResponseFactory.dataPage(
gifts.map((gift) => new GiftListResponseDto(gift)),
{
size: filters.size,
page: filters.page,
itemCount,
},
);
}
@Get(':giftId')
@UseGuards(AccessTokenGuard)
@ApiDataResponse(GiftDetailsResponseDto)
async findGiftById(@AuthenticatedUser() user: IJwtPayload, @Param('giftId') giftId: string) {
const gift = await this.giftsService.findUserGiftById(user, giftId);
return ResponseFactory.data(new GiftDetailsResponseDto(gift));
}
@Post(':giftId/redeem')
@UseGuards(RolesGuard)
@AllowedRoles(Roles.JUNIOR)
@HttpCode(HttpStatus.NO_CONTENT)
redeemGift(@AuthenticatedUser() { sub }: IJwtPayload, @Param('giftId') giftId: string) {
return this.giftsService.redeemGift(sub, giftId);
}
@Post(':giftId/undo-redeem')
@UseGuards(RolesGuard)
@AllowedRoles(Roles.JUNIOR)
@HttpCode(HttpStatus.NO_CONTENT)
UndoGiftRedemption(@AuthenticatedUser() { sub }: IJwtPayload, @Param('giftId') giftId: string) {
return this.giftsService.UndoGiftRedemption(sub, giftId);
}
@Post(':giftId/reply')
@UseGuards(RolesGuard)
@AllowedRoles(Roles.JUNIOR)
@HttpCode(HttpStatus.NO_CONTENT)
replyToGift(
@AuthenticatedUser() { sub }: IJwtPayload,
@Param('giftId') giftId: string,
@Body() body: GiftReplyRequestDto,
) {
return this.giftsService.replyToGift(sub, giftId, body);
}
}