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

import { RoleType } from '../../constants';
import { ApiPageOkResponse, Auth, AuthUser, Whitelist } from '../../decorators';
import { OtSettingNotFoundException } from '../../exceptions';
import { UserEntity } from '../user/user.entity';
import { OtSettingDto } from './dtos/ot-settings.dto';
import { UpdateOtSettingDto } from './dtos/update-ot-settings.dto';
import { OtSettingService } from './ot-settings.service';

@Controller('ot-setting')
@ApiTags('ot-setting')
export class OtSettingController {
  constructor(private otSettingService: OtSettingService) {}

  @Get()
  @Whitelist()
  @Auth([RoleType.ADMIN, RoleType.USER])
  @HttpCode(HttpStatus.OK)
  @ApiPageOkResponse({ type: OtSettingDto, description: 'Get Ot setting' })
  async getOtSetting(): Promise<OtSettingDto> {
    const otSettingEntity = await this.otSettingService.findOne();

    if (!otSettingEntity) {
      throw new OtSettingNotFoundException();
    }

    return otSettingEntity.toDto();
  }

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

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

    const otSettingEntity = await this.otSettingService.updateOtSetting(
      toPatchEntity,
      body,
      user,
    );

    return otSettingEntity.toDto();
  }
}