From 8f127fdb57aae0e513e2c533a7ad8fb39236e73f Mon Sep 17 00:00:00 2001 From: hofstede-matheus Date: Tue, 10 Oct 2023 15:10:51 -0300 Subject: [PATCH] send client in queue instead of client --- .../repositories/TypeOrmDesksRepository.ts | 57 +++++++++---------- .../desk/domain/entities/Desk.entity.ts | 6 ++ .../domain/repositories/DeskRepository.ts | 3 +- .../FindOneDeskFromOrganizationUsecase.ts | 4 +- .../http/controllers/DeskController.ts | 11 +++- .../desk/presentation/http/dto/GetDesk.ts | 2 +- 6 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/modules/desk/data/typeorm/repositories/TypeOrmDesksRepository.ts b/src/modules/desk/data/typeorm/repositories/TypeOrmDesksRepository.ts index 5b9eb09..bcdf56a 100644 --- a/src/modules/desk/data/typeorm/repositories/TypeOrmDesksRepository.ts +++ b/src/modules/desk/data/typeorm/repositories/TypeOrmDesksRepository.ts @@ -7,8 +7,7 @@ import { ServiceEntity } from '../../../../services/domain/entities/Service.enti import { isServiceOpen } from '../../../../common/shared/helpers/moment'; import { QueueRepository } from '../../../../queues/domain/repositories/QueueRepository'; import { Inject } from '@nestjs/common'; -import { UserEntity } from '../../../../users/domain/entities/User.entity'; -import { ClientEntity } from '../../../../clients/domain/entities/Client.entity'; +import { ClientInQueue } from '../../../../queues/domain/entities/Queue.entity'; export class TypeOrmDesksRepository implements DeskRepository { constructor( @@ -19,7 +18,7 @@ export class TypeOrmDesksRepository implements DeskRepository { private queueRepository: QueueRepository, ) {} - async getLastClientCalledFromDesk(deskId: string): Promise { + async getLastClientCalledFromDesk(deskId: string): Promise { const servicesFromDesk = await this.desksRepository.query( ` SELECT services.id as service_id @@ -41,41 +40,37 @@ export class TypeOrmDesksRepository implements DeskRepository { ); const lastClientCalled = await this.desksRepository.query( - `SELECT - clients_position_in_queues.client_id as client_id - FROM clients_position_in_queues - WHERE clients_position_in_queues.queue_id = ANY($1) - ORDER BY clients_position_in_queues.created_at DESC - LIMIT 1 + ` + SELECT + clients.id, + clients.name, + clients.organization_id, + clients.registration_id, + clients_position_in_queues.called_at, + clients_position_in_queues.attended_by_user, + clients.created_at, + clients.updated_at + FROM clients + INNER JOIN clients_position_in_queues ON clients.id = clients_position_in_queues.client_id + WHERE clients_position_in_queues.queue_id = ANY($1) + AND clients_position_in_queues.called_at IS NOT NULL + ORDER BY clients_position_in_queues.called_at DESC + LIMIT 1 `, [queuesThatBelongsToServices.map((queue) => queue.queue_id)], ); if (lastClientCalled.length === 0) return undefined; - // clients - const client = await this.desksRepository.query( - ` - SELECT - clients.id as client_id, - clients.name as client_name, - clients.registration_id as client_registration_id, - clients.organization_id as client_organization_id, - clients.created_at as client_created_at, - clients.updated_at as client_updated_at - FROM clients - WHERE clients.id = $1 - `, - [lastClientCalled[0].client_id], - ); - return { - id: client[0].client_id, - name: client[0].client_name, - registrationId: client[0].client_registration_id, - organizationId: client[0].client_organization_id, - createdAt: client[0].client_created_at, - updatedAt: client[0].client_updated_at, + id: lastClientCalled[0].id, + name: lastClientCalled[0].name, + organizationId: lastClientCalled[0].organization_id, + registrationId: lastClientCalled[0].registration_id, + calledDate: lastClientCalled[0].called_at, + attendedByUserId: lastClientCalled[0].attended_by_user, + createdAt: lastClientCalled[0].created_at, + updatedAt: lastClientCalled[0].updated_at, }; } diff --git a/src/modules/desk/domain/entities/Desk.entity.ts b/src/modules/desk/domain/entities/Desk.entity.ts index 99a770c..a58d735 100644 --- a/src/modules/desk/domain/entities/Desk.entity.ts +++ b/src/modules/desk/domain/entities/Desk.entity.ts @@ -11,6 +11,7 @@ import { import { DomainError } from '../../../common/shared/helpers/errors'; import { ServiceEntity } from '../../../services/domain/entities/Service.entity'; import { ClientEntity } from '../../../clients/domain/entities/Client.entity'; +import { ClientInQueue } from '../../../queues/domain/entities/Queue.entity'; export interface DeskEntity { readonly id: string; @@ -29,6 +30,11 @@ export interface DeskWithCalledClient { client: ClientEntity | null; } +export interface DeskWithCalledClientInQueue { + desk: DeskEntity; + client: ClientInQueue | null; +} + @staticImplements>() export class DeskEntity { private constructor(readonly name: string, readonly organizationId: string) {} diff --git a/src/modules/desk/domain/repositories/DeskRepository.ts b/src/modules/desk/domain/repositories/DeskRepository.ts index 358cc49..49c0539 100644 --- a/src/modules/desk/domain/repositories/DeskRepository.ts +++ b/src/modules/desk/domain/repositories/DeskRepository.ts @@ -1,4 +1,5 @@ import { ClientEntity } from '../../../clients/domain/entities/Client.entity'; +import { ClientInQueue } from '../../../queues/domain/entities/Queue.entity'; import { DeskEntity } from '../entities/Desk.entity'; export interface DeskRepository { @@ -12,7 +13,7 @@ export interface DeskRepository { services: string[], ): Promise; findById(id: string): Promise; - getLastClientCalledFromDesk(deskId: string): Promise; + getLastClientCalledFromDesk(deskId: string): Promise; } export const DeskRepository = Symbol('DeskRepository'); diff --git a/src/modules/desk/interactors/usecases/FindOneDeskFromOrganizationUsecase.ts b/src/modules/desk/interactors/usecases/FindOneDeskFromOrganizationUsecase.ts index 5ea48f4..2ce611a 100644 --- a/src/modules/desk/interactors/usecases/FindOneDeskFromOrganizationUsecase.ts +++ b/src/modules/desk/interactors/usecases/FindOneDeskFromOrganizationUsecase.ts @@ -1,7 +1,7 @@ import { Inject, Injectable } from '@nestjs/common'; import { UseCase } from '../../../common/shared/helpers/usecase'; import { DeskRepository } from '../../domain/repositories/DeskRepository'; -import { DeskWithCalledClient } from '../../domain/entities/Desk.entity'; +import { DeskWithCalledClientInQueue } from '../../domain/entities/Desk.entity'; import { Either, left, right } from '../../../common/shared/helpers/either'; import { DomainError } from '../../../common/shared/helpers/errors'; import { Validator } from '../../../common/shared/helpers/validator'; @@ -19,7 +19,7 @@ export class FindOneDeskFromOrganizationUsecase implements UseCase { }: { organizationId: string; id?: string; - }): Promise> { + }): Promise> { const validation = Validator.validate({ id: [organizationId] }); if (validation.isLeft()) return left(validation.value); diff --git a/src/modules/desk/presentation/http/controllers/DeskController.ts b/src/modules/desk/presentation/http/controllers/DeskController.ts index 4f9f2be..c6b5a21 100644 --- a/src/modules/desk/presentation/http/controllers/DeskController.ts +++ b/src/modules/desk/presentation/http/controllers/DeskController.ts @@ -19,7 +19,10 @@ import { UpdateDeskUsecase } from '../../../interactors/usecases/UpdateDeskUseca import { CallNextOnDeskResponse } from '../dto/CallNextOnDesk'; import { CallNextClientOfDeskUsecase } from '../../../interactors/usecases/CallNextClientOfDeskUsecase'; import { Request } from 'express'; -import { DeskEntity } from '../../../domain/entities/Desk.entity'; +import { + DeskEntity, + DeskWithCalledClient, +} from '../../../domain/entities/Desk.entity'; import { FindAllDesksFromOrganizationUsecase } from '../../../interactors/usecases/FindAllDesksFromOrganizationUsecase'; import { FindOneDeskFromOrganizationUsecase } from '../../../interactors/usecases/FindOneDeskFromOrganizationUsecase'; import { GetDeskResponse } from '../dto/GetDesk'; @@ -135,7 +138,7 @@ export class DeskController { } @Get(':deskId/organizations/:organizationId') - @ApiResponse({ type: Desk }) + @ApiResponse({ type: GetDeskResponse }) @ApiBearerAuth() async getDesk( @Param('organizationId') organizationId: string, @@ -149,13 +152,15 @@ export class DeskController { if (result.isLeft()) throw toPresentationError(result.value); return { - lastCalledClient: { + lastClientCalled: { id: result.value.client.id, name: result.value.client.name, organizationId: result.value.client.organizationId, registrationId: result.value.client.registrationId, createdAt: result.value.client.createdAt, updatedAt: result.value.client.updatedAt, + calledDate: result.value.client.calledDate, + attendedByUserId: result.value.client.attendedByUserId, }, desk: { id: result.value.desk.id, diff --git a/src/modules/desk/presentation/http/dto/GetDesk.ts b/src/modules/desk/presentation/http/dto/GetDesk.ts index c9b6408..1ac29fe 100644 --- a/src/modules/desk/presentation/http/dto/GetDesk.ts +++ b/src/modules/desk/presentation/http/dto/GetDesk.ts @@ -10,5 +10,5 @@ export class GetDeskResponse { @ApiProperty({ type: Client, }) - lastCalledClient: Client; + lastClientCalled: Client; }