import { BadRequestException , Injectable , Logger , NotFoundException , ForbiddenException , } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import { HelpList } from './help-list.entity' ; import { HelpRequest } from '../helpRequests/help-request.entity' ; import { HelpListCreateDto } from './dto/help-list-create.dto' ; import { HelpListStatus } from './help-list-status' ; import { HelpRequestStatus } from '../helpRequests/help-request-status' ; @ Injectable ( ) export class HelpListsService { static LOGGER = new Logger ( 'HelpLists' , true ) ; constructor ( @ InjectRepository ( HelpList ) private readonly helpListsRepository , @ InjectRepository ( HelpRequest ) private readonly requestRepository ) { } async getById ( userId , helpListId , options = { checkOwner : true } ) { const where = { } ; if ( options . checkOwner ) { where . ownerId = userId ; } const helpLists = await this . helpListsRepository . findOne ( helpListId , { where , relations : [ 'helpRequests' , 'helpRequests.articles' , 'helpRequests.articles.article' , 'helpRequests.requester' , ] , } ) ; if ( ! helpLists ) { throw new NotFoundException ( 'Help List not found' ) ; } return helpLists ; } async create ( userId , createRequestDto ) { const helpList = new HelpList ( ) ; if ( createRequestDto . helpRequestsIds ) { helpList . helpRequests = createRequestDto . helpRequestsIds . map ( h => ( { id : h , status : HelpRequestStatus . ONGOING , } ) ) ; } helpList . ownerId = userId ; helpList . status = HelpListStatus . ACTIVE ; return this . helpListsRepository . save ( helpList ) ; } async getAllByUser ( userId ) { return this . helpListsRepository . find ( { where : { ownerId : userId } , relations : [ 'helpRequests' , 'helpRequests.requester' , 'helpRequests.articles' , 'helpRequests.articles.article' , ] , } ) ; } async update ( userId , helpList , helpListUpdate ) { if ( userId !== helpList . ownerId ) { throw new ForbiddenException ( 'The help list does not belong to you' ) ; } if ( helpListUpdate . status ) { helpList . status = helpListUpdate . status ; if ( helpList . status === HelpListStatus . COMPLETED ) { helpList . helpRequests . forEach ( request => ( request . status = HelpRequestStatus . COMPLETED ) ) ; } } if ( helpListUpdate . helpRequestsIds ) { helpList . helpRequests . forEach ( r => ( r . status = HelpRequestStatus . PENDING ) ) ; await this . helpListsRepository . save ( helpList ) ; helpList . helpRequests = helpListUpdate . helpRequestsIds . map ( id => { const re = new HelpRequest ( ) ; re . id = id ; re . status = HelpRequestStatus . ONGOING ; return re ; } ) ; } return this . helpListsRepository . save ( helpList ) ; } async addRequest ( userId , helpList , helpRequest ) { if ( userId !== helpList . ownerId ) { throw new ForbiddenException ( 'The help list does not belong to you' ) ; } helpRequest . status = HelpRequestStatus . ONGOING ; helpList . helpRequests . push ( helpRequest ) ; return this . helpListsRepository . save ( helpList ) ; } async removeRequest ( userId , helpList , helpRequest ) { if ( userId !== helpList . ownerId ) { throw new ForbiddenException ( 'The help list does not belong to you' ) ; } helpRequest . status = HelpRequestStatus . PENDING ; helpList . helpRequests = helpList . helpRequests . filter ( request => request . id != helpRequest . id ) ; await this . requestRepository . save ( helpRequest ) ; return await this . helpListsRepository . save ( helpList ) ; } async changeArticleDoneForRequest ( userId , helpList , helpRequestId , articleId , articleDone ) { if ( userId !== helpList . ownerId ) { throw new ForbiddenException ( 'The help list does not belong to you' ) ; } const helpRequest = helpList . helpRequests . find ( request => request . id === helpRequestId ) ; if ( ! helpRequest ) { throw new NotFoundException ( 'The help request is not in this help list' ) ; } const article = helpRequest . articles . find ( art => art . articleId === articleId ) ; if ( ! article ) { throw new NotFoundException ( 'Article not found in request' ) ; } article . articleDone = articleDone ; return this . helpListsRepository . save ( helpList ) ; } async changeArticleDoneForAll ( userId , helpList , articleId , articleDone ) { if ( userId !== helpList . ownerId ) { throw new ForbiddenException ( 'The help list does not belong to you' ) ; } helpList . helpRequests . forEach ( request => { request . articles . forEach ( article => { if ( article . articleId === articleId ) { article . articleDone = articleDone ; } } ) ; } ) ; return this . helpListsRepository . save ( helpList ) ; } }