import { Args, Mutation, Parent, Query, ResolveField, Resolver, } from '@nestjs/graphql'; import { PaginationArgs } from 'src/core/base/pagination.input'; import { decodeOffsetCursor, encodeCursor, } from 'src/core/base/pagination.util'; import { TourObjectType } from '../app-accounting/tour.object-type'; import { AnomaliesService } from '../feat-anomalies/anomalies.service'; import { AnomalyTypes } from '../feat-anomalies/anomaly-detectors/anomaly-types.enum'; import { AnomalyObjectType, PaginatedAnomalyGroupObjectType, PaginatedAnomalyObjectType, SolutionTargetObjectType, } from './anomaly.object-type'; @Resolver(() => AnomalyObjectType) export class ControlCenterResolver { constructor(private readonly anomaliesService: AnomaliesService) {} @Query(() => AnomalyObjectType) async anomaly(@Args('id') id: string) { return this.anomaliesService.findById(id); } @Query(() => [AnomalyObjectType]) async anomaliesProto() { return this.anomaliesService.findActiveAnomalies(); } @Query(() => PaginatedAnomalyObjectType) async anomalies(@Args() { cursor, take }: PaginationArgs) { const skip = cursor ? decodeOffsetCursor(cursor) : 0; const activeAnomalies = await this.anomaliesService.findActiveAnomalies( skip, take, [AnomalyTypes.WorkingEmployeeNotInDistributionSystem], ); const totalCount = await this.anomaliesService.countActiveAnomalies([ AnomalyTypes.WorkingEmployeeNotInDistributionSystem, ]); const prevPage = skip - take < 0 ? 0 : skip - take; const nextPage = skip + take < totalCount ? skip + take : skip; return { nodes: activeAnomalies, prevCursor: encodeCursor(prevPage), endCursor: encodeCursor(nextPage), hasNextPage: nextPage !== skip, totalCount, }; } @Query(() => PaginatedAnomalyGroupObjectType) async groupedAnomalies(@Args() { cursor, take }: PaginationArgs) { const skip = cursor ? decodeOffsetCursor(cursor) : 0; const { totalCount, anomalies: groupedActiveAnomalies } = await this.anomaliesService.findActiveAnomaliesGroupedByGroupKey( skip, take, ); const prevPage = skip - take < 0 ? 0 : skip - take; const nextPage = skip + take < totalCount ? skip + take : skip; return { nodes: groupedActiveAnomalies, prevCursor: encodeCursor(prevPage), endCursor: encodeCursor(nextPage), hasNextPage: nextPage !== skip, totalCount, }; } @Mutation(() => AnomalyObjectType) async setAnomalyToSleep(@Args({ name: 'id' }) anomalyId: string) { return this.anomaliesService.setAnomalyToSleep(anomalyId); } @Mutation(() => AnomalyObjectType) async acceptAnomaly(@Args({ name: 'id' }) anomalyId: string) { return this.anomaliesService.acceptAnomaly(anomalyId); } @ResolveField(() => TourObjectType, { nullable: true }) tour(@Parent() anomaly: AnomalyObjectType) { const [operationId] = anomaly.groupKey.split('-').reverse(); return this.anomaliesService.findTourByOperationId(operationId); } @ResolveField(() => [SolutionTargetObjectType]) solution( @Parent() anomaly: AnomalyObjectType, ): Promise { return this.anomaliesService.findSolutionTargetsByAnomalyId(anomaly.id); } }