Skip to content

Commit

Permalink
send client in queue instead of client
Browse files Browse the repository at this point in the history
  • Loading branch information
hofstede-matheus committed Oct 10, 2023
1 parent 809bd15 commit 8f127fd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -19,7 +18,7 @@ export class TypeOrmDesksRepository implements DeskRepository {
private queueRepository: QueueRepository,
) {}

async getLastClientCalledFromDesk(deskId: string): Promise<ClientEntity> {
async getLastClientCalledFromDesk(deskId: string): Promise<ClientInQueue> {
const servicesFromDesk = await this.desksRepository.query(
`
SELECT services.id as service_id
Expand All @@ -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,
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/modules/desk/domain/entities/Desk.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,11 @@ export interface DeskWithCalledClient {
client: ClientEntity | null;
}

export interface DeskWithCalledClientInQueue {
desk: DeskEntity;
client: ClientInQueue | null;
}

@staticImplements<DomainEntity<DeskEntity>>()
export class DeskEntity {
private constructor(readonly name: string, readonly organizationId: string) {}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/desk/domain/repositories/DeskRepository.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,7 +13,7 @@ export interface DeskRepository {
services: string[],
): Promise<DeskEntity>;
findById(id: string): Promise<DeskEntity>;
getLastClientCalledFromDesk(deskId: string): Promise<ClientEntity>;
getLastClientCalledFromDesk(deskId: string): Promise<ClientInQueue>;
}

export const DeskRepository = Symbol('DeskRepository');
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,7 +19,7 @@ export class FindOneDeskFromOrganizationUsecase implements UseCase {
}: {
organizationId: string;
id?: string;
}): Promise<Either<DomainError, DeskWithCalledClient>> {
}): Promise<Either<DomainError, DeskWithCalledClientInQueue>> {
const validation = Validator.validate({ id: [organizationId] });
if (validation.isLeft()) return left(validation.value);

Expand Down
11 changes: 8 additions & 3 deletions src/modules/desk/presentation/http/controllers/DeskController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -135,7 +138,7 @@ export class DeskController {
}

@Get(':deskId/organizations/:organizationId')
@ApiResponse({ type: Desk })
@ApiResponse({ type: GetDeskResponse })
@ApiBearerAuth()
async getDesk(
@Param('organizationId') organizationId: string,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/desk/presentation/http/dto/GetDesk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export class GetDeskResponse {
@ApiProperty({
type: Client,
})
lastCalledClient: Client;
lastClientCalled: Client;
}

0 comments on commit 8f127fd

Please sign in to comment.