penisularhr / src / modules / incentive / incentive-settings.controller.ts
incentive-settings.controller.ts
Raw
import {
  Body,
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  Patch,
  // Post,
  Query,
} from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';

import { type PageDto } from '../../common/dto/page.dto';
import { RoleType } from '../../constants';
import { ApiPageOkResponse, Auth, AuthUser, Whitelist } from '../../decorators';
import { IncentiveSettingNotFoundException } from '../../exceptions';
import { UserEntity } from '../user/user.entity';
// import { CreateIncentiveSettingDto } from './dtos/create-incentive-settings.dto';
import { IncentiveSettingPageOptionsDto } from './dtos/get-incentive-settings-page.dto';
import { IncentiveSettingDto } from './dtos/incentive-settings.dto';
import { UpdateIncentiveSettingDto } from './dtos/update-incentive-settings.dto';
import { IncentiveSettingService } from './incentive-settings.service';

@Controller('incentive-setting')
@ApiTags('incentive-setting')
export class IncentiveSettingController {
  constructor(private incentiveSettingService: IncentiveSettingService) {}

  @Get()
  @Whitelist()
  @Auth([RoleType.ADMIN, RoleType.USER])
  @HttpCode(HttpStatus.OK)
  @ApiPageOkResponse({
    type: IncentiveSettingDto,
    description: 'Get incentive setting list',
  })
  async getIncentiveSettings(
    @Query() query: IncentiveSettingPageOptionsDto,
  ): Promise<PageDto<IncentiveSettingDto>> {
    return this.incentiveSettingService.findMany(query);
  }

  // @Post()
  // @Auth([RoleType.ADMIN])
  // @HttpCode(HttpStatus.OK)
  // @ApiOkResponse()
  // async createGame(
  //   @Body() body: CreateIncentiveSettingDto,
  // ): Promise<IncentiveSettingDto> {
  //   const incentiveSettingEntity =
  //     await this.incentiveSettingService.createIncentiveSetting(body);

  //   return incentiveSettingEntity.toDto();
  // }

  @Patch(':id')
  @Whitelist()
  @Auth([RoleType.ADMIN])
  @HttpCode(HttpStatus.OK)
  @ApiOkResponse()
  async updateGame(
    @AuthUser() user: UserEntity,
    @Param('id') id: Uuid,
    @Body() body: UpdateIncentiveSettingDto,
  ): Promise<IncentiveSettingDto> {
    const toPatchEntity = await this.incentiveSettingService.findOne({
      id,
    });

    if (!toPatchEntity) {
      throw new IncentiveSettingNotFoundException();
    }

    const incentiveSettingEntity =
      await this.incentiveSettingService.updateincentiveSetting(
        toPatchEntity,
        body,
        user,
      );

    return incentiveSettingEntity.toDto();
  }
}