moments.service.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {
  2. Injectable,
  3. NotFoundException,
  4. HttpException,
  5. HttpStatus,
  6. BadRequestException,
  7. InternalServerErrorException,
  8. UnauthorizedException
  9. } from '@nestjs/common'
  10. import { Repository, UpdateResult } from 'typeorm'
  11. import { InjectRepository } from '@nestjs/typeorm'
  12. import { Moments } from './entities/moments.entity'
  13. import { paginate, Pagination } from 'nestjs-typeorm-paginate'
  14. import { PageRequest } from '../common/dto/page-request'
  15. import { ChatRoleService } from '../chat-role/chat-role.service'
  16. import { ro } from 'date-fns/locale'
  17. @Injectable()
  18. export class MomentsService {
  19. constructor(
  20. @InjectRepository(Moments)
  21. private readonly momentsRepository: Repository<Moments>,
  22. private readonly chatRoleService: ChatRoleService
  23. ) { }
  24. async findAll(req: PageRequest<Moments>): Promise<Pagination<Moments>> {
  25. const page = await paginate<Moments>(this.momentsRepository, req.page, req.search)
  26. const ids = page.items.reduce((ids: number[], val: Moments) => {
  27. if (!ids.includes(val.userId)) {
  28. ids.push(val.userId);
  29. }
  30. return ids;
  31. }, []);
  32. const map = await this.chatRoleService.findMapByIds(ids);
  33. page.items.forEach(c => {
  34. c.chatRole = map.get(c.userId)
  35. })
  36. return page
  37. }
  38. async findById(id: number): Promise<Moments> {
  39. const moments = await this.momentsRepository.findOneBy({ id: +id })
  40. const chatRole = await this.chatRoleService.findById(moments.userId)
  41. moments.chatRole = chatRole
  42. return moments
  43. }
  44. }