| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import {
- Injectable,
- NotFoundException,
- HttpException,
- HttpStatus,
- BadRequestException,
- InternalServerErrorException,
- UnauthorizedException
- } from '@nestjs/common'
- import { Repository, UpdateResult } from 'typeorm'
- import { InjectRepository } from '@nestjs/typeorm'
- import { Moments } from './entities/moments.entity'
- import { paginate, Pagination } from 'nestjs-typeorm-paginate'
- import { PageRequest } from '../common/dto/page-request'
- import { ChatRoleService } from '../chat-role/chat-role.service'
- import { ro } from 'date-fns/locale'
- @Injectable()
- export class MomentsService {
- constructor(
- @InjectRepository(Moments)
- private readonly momentsRepository: Repository<Moments>,
- private readonly chatRoleService: ChatRoleService
- ) { }
- async findAll(req: PageRequest<Moments>): Promise<Pagination<Moments>> {
- const page = await paginate<Moments>(this.momentsRepository, req.page, req.search)
- const ids = page.items.reduce((ids: number[], val: Moments) => {
- if (!ids.includes(val.userId)) {
- ids.push(val.userId);
- }
- return ids;
- }, []);
- const map = await this.chatRoleService.findMapByIds(ids);
- page.items.forEach(c => {
- c.chatRole = map.get(c.userId)
- })
- return page
- }
- async findById(id: number): Promise<Moments> {
- const moments = await this.momentsRepository.findOneBy({ id: +id })
- const chatRole = await this.chatRoleService.findById(moments.userId)
- moments.chatRole = chatRole
- return moments
- }
- }
|