import { Injectable } from '@nestjs/common' ; import { ModelType } from '@typegoose/typegoose/lib/types' ; import { InjectModel } from 'nestjs-typegoose' ; import { CreateTopPageDto } from './dto/create-top-page.dto' ; import { TopLevelCategory , TopPageModel } from './top-page.model' ; import { addDays } from 'date-fns' ; import { Types } from 'mongoose' ; @ Injectable ( ) export class TopPageService { constructor ( @ InjectModel ( TopPageModel ) private readonly topPageModel ) { } async create ( dto ) { return this . topPageModel . create ( dto ) ; } async findById ( id ) { return this . topPageModel . findById ( id ) . exec ( ) ; } async findByAlias ( alias ) { return this . topPageModel . findOne ( { alias } ) . exec ( ) ; } async findAll ( ) { return this . topPageModel . find ( { } ) . exec ( ) ; } async findByCategory ( firstCategory ) { return this . topPageModel . aggregate ( ) . match ( { firstCategory } ) . group ( { _id : { secondCategory : '$secondCategory' } , pages : { $push : { alias : '$alias' , title : '$title' , _id : '$_id' , category : '$category' } } } ) . exec ( ) ; } async findByText ( text ) { return this . topPageModel . find ( { $text : { $search : text , $caseSensitive : false } } ) . exec ( ) ; } async deleteById ( id ) { return this . topPageModel . findByIdAndRemove ( id ) . exec ( ) ; } async updateById ( id , dto ) { return this . topPageModel . findByIdAndUpdate ( id , dto , { new : true } ) . exec ( ) ; } async findForHhUpdate ( date ) { return this . topPageModel . find ( { firstCategory : 0 , $or : [ { 'hh.updatedAt' : { $lt : addDays ( date , - 1 ) } } , { 'hh.updatedAt' : { $exists : false } } ] } ) . exec ( ) ; } }