import { Body , Controller , Delete , Get , HttpCode , Logger , NotFoundException , Param , Patch , Post , UseGuards , UsePipes , ValidationPipe } from '@nestjs/common' ; import { Cron , CronExpression , SchedulerRegistry } from '@nestjs/schedule' ; import { JwtAuthGuard } from 'src/auth/guards/jwt.guard' ; import { HhService } from 'src/hh/hh.service' ; import { IdValidationPipe } from 'src/pipes/ad-validation.pipe' ; import { CreateTopPageDto } from './dto/create-top-page.dto' ; import { FindTopPageDto } from './dto/find-top-page.dto' ; import { NOT_FOUND_TOP_PAGE_ERROR } from './top-page.constants' ; import { TopPageService } from './top-page.service' ; @ Controller ( 'top-page' ) export class TopPageController { constructor ( private readonly topPageService , private readonly hhService , private readonly scheduleRegistry ) { } @ UseGuards ( JwtAuthGuard ) @ UsePipes ( new ValidationPipe ( ) ) @ Post ( 'create' ) async create ( @ Body ( ) dto ) { return this . topPageService . create ( dto ) ; } @ UseGuards ( JwtAuthGuard ) @ Get ( ':id' ) async get ( @ Param ( 'id' , IdValidationPipe ) id ) { const page = await this . topPageService . findById ( id ) ; if ( ! page ) { throw new NotFoundException ( NOT_FOUND_TOP_PAGE_ERROR ) ; } return page ; } @ Get ( 'byAlias/:alias' ) async getByAlias ( @ Param ( 'alias' ) alias ) { const page = await this . topPageService . findByAlias ( alias ) ; if ( ! page ) { throw new NotFoundException ( NOT_FOUND_TOP_PAGE_ERROR ) ; } return page ; } @ UseGuards ( JwtAuthGuard ) @ Delete ( ':id' ) async delete ( @ Param ( 'id' ) id ) { const detetedPage = await this . topPageService . deleteById ( id ) ; if ( ! detetedPage ) { throw new NotFoundException ( NOT_FOUND_TOP_PAGE_ERROR ) ; } } @ UseGuards ( JwtAuthGuard ) @ UsePipes ( new ValidationPipe ( ) ) @ Patch ( ':id' ) async patch ( @ Param ( 'id' ) id , @ Body ( ) dto ) { const updatedPage = await this . topPageService . updateById ( id , dto ) ; if ( ! updatedPage ) { throw new NotFoundException ( NOT_FOUND_TOP_PAGE_ERROR ) ; } return updatedPage ; } @ UsePipes ( new ValidationPipe ( ) ) @ HttpCode ( 200 ) @ Post ( 'find' ) async find ( @ Body ( ) dto ) { return this . topPageService . findByCategory ( dto . firstCategory ) ; } @ Get ( 'textSearch/:text' ) async textSearch ( @ Param ( 'text' ) text ) { return this . topPageService . findByText ( text ) ; } @ Cron ( CronExpression . EVERY_DAY_AT_MIDNIGHT ) async test ( ) { const data = await this . topPageService . findForHhUpdate ( new Date ( ) ) ; for ( let page of data ) { const hhData = await this . hhService . getData ( page . category ) ; page . hh = hhData ; await this . topPageService . updateById ( page . _id , page ) ; } } }