import { Body, Controller, Get, HttpCode, HttpStatus, Patch, Query, } from '@nestjs/common'; import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; import moment from 'moment'; import { type PageDto } from '../../common/dto/page.dto'; import { RoleType } from '../../constants'; import { ApiPageOkResponse, Auth, AuthUser, UUIDParam, Whitelist, } from '../../decorators'; import { AccountHasClosedException, IncentiveSettingNotFoundException, } from '../../exceptions'; import { ApiConfigService } from '../../shared/services/api-config.service'; import { UserEntity } from '../user/user.entity'; import { IncentiveRecordPageOptionsDto } from './dtos/get-incentive-record-page.dto'; import { IncentiveRecordDto } from './dtos/incentive-record.dto'; import { UpdateIncentiveRecordDto } from './dtos/update-incentive-record.dto'; import { IncentiveRecordService } from './incentive-record.service'; @Controller('incentive-record') @ApiTags('incentive-record') export class IncentiveRecordController { constructor( private incentiveRecordService: IncentiveRecordService, private configService: ApiConfigService, ) {} @Get() @Whitelist() @Auth([RoleType.ADMIN, RoleType.USER]) @HttpCode(HttpStatus.OK) @ApiPageOkResponse({ type: IncentiveRecordDto, description: 'Get incentive setting list', }) async getIncentiveSettings( @Query() query: IncentiveRecordPageOptionsDto, ): Promise<PageDto<IncentiveRecordDto>> { return this.incentiveRecordService.findMany(query); } @Patch(':id') @Whitelist() @Auth([RoleType.ADMIN]) @HttpCode(HttpStatus.OK) @ApiOkResponse() async updateGame( @AuthUser() user: UserEntity, @UUIDParam('id') id: Uuid, @Body() body: UpdateIncentiveRecordDto, ): Promise<IncentiveRecordDto> { const toPatchEntity = await this.incentiveRecordService.findOne({ id, }); if (!toPatchEntity) { throw new IncentiveSettingNotFoundException(); } const currentDate = moment.utc(); const recordDateMonth = moment.utc(toPatchEntity.date).month(); const currentMonth = moment.utc().month(); if ( this.configService.isProduction && currentMonth - recordDateMonth >= 1 && currentDate.date() > 7 ) { throw new AccountHasClosedException(); } const incentiveRecordEntity = await this.incentiveRecordService.updateIncentiveRecordByAdmin( toPatchEntity, body, user, ); return incentiveRecordEntity.toDto(); } }