Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable } from '@nestjs/common';
import {
AgentField,
TicketEvents,
TicketStatus,
} from '../../../domain/entities/ticket.entity';
import { ITicketRepository } from '../../../domain/repository/ticket.repository.interface';

export interface TicketHistoryEntryOutput {
event: TicketEvents;
responsibleAgent: string | null;
responsibleAgent: AgentField | null;
status: TicketStatus;
message: string;
solution?: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ describe('GetHistoryFilteredUseCase', () => {
repository.readById.mockResolvedValue(ticket);

const output = await useCase.execute(ticket.id, {
responsibleAgent: agentId,
responsibleAgentId: agentId,
});

expect(output.history.every((e) => e.responsibleAgent === agentId)).toBe(
true,
);
expect(
output.history.every((e) => e.responsibleAgent?.id === agentId),
).toBe(true);
expect(output.history).toHaveLength(1);
});

Expand Down Expand Up @@ -121,17 +121,16 @@ describe('GetHistoryFilteredUseCase', () => {

const output = await useCase.execute(ticket.id, {
status: TicketStatus.IN_PROGRESS,
responsibleAgent: agentId,
responsibleAgentId: agentId,
event: TicketEvents.NEW_AGENT,
fromDate: before,
});

expect(output.history).toHaveLength(1);
expect(output.history[0].status).toBe(TicketStatus.IN_PROGRESS);
expect(output.history[0].responsibleAgent).toBe(agentId);
expect(output.history[0].responsibleAgent?.id).toBe(agentId);
expect(output.history[0].event).toBe(TicketEvents.NEW_AGENT);
});

it('should return empty history when no entries match filters', async () => {
repository.readById.mockResolvedValue(ticket);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable } from '@nestjs/common';
import {
AgentField,
TicketEvents,
TicketStatus,
} from '../../../domain/entities/ticket.entity';
import { ITicketRepository } from '../../../domain/repository/ticket.repository.interface';

export interface TicketHistoryEntryOutput {
event: TicketEvents;
responsibleAgent: string | null;
responsibleAgent: AgentField | null;
status: TicketStatus;
message: string;
solution?: string | null;
Expand All @@ -27,7 +28,7 @@ export class GetHistoryFilteredUseCase {
id: string,
filters: {
status?: TicketStatus;
responsibleAgent?: string;
responsibleAgentId?: string;
event?: TicketEvents;
fromDate?: Date;
},
Expand All @@ -44,9 +45,9 @@ export class GetHistoryFilteredUseCase {
history = history.filter((entry) => entry.status === filters.status);
}

if (filters.responsibleAgent) {
if (filters.responsibleAgentId) {
history = history.filter(
(entry) => entry.responsibleAgent === filters.responsibleAgent,
(entry) => entry.responsibleAgent?.id === filters.responsibleAgentId,
);
}

Expand All @@ -61,7 +62,12 @@ export class GetHistoryFilteredUseCase {

return {
id: foundTicket.id,
history: [...history],
history: history.map((entry) => ({
...entry,
responsibleAgent: entry.responsibleAgent
? { id: entry.responsibleAgent.id, name: entry.responsibleAgent.name }
: null,
})),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import {
AgentField,
Ticket,
TicketPriority,
TicketStatus,
Expand All @@ -15,7 +16,7 @@ export interface ReadAllTicketOutput {
description: string;
clientId: string;
status: TicketStatus;
agentId: string | null;
agent: AgentField | null;
escalationLevel: number;
createdAt: Date;
updatedAt: Date | null;
Expand Down Expand Up @@ -51,7 +52,7 @@ export class ReadAllTicketUseCase {
description: primitive.description,
clientId: primitive.clientId,
status: primitive.status,
agentId: primitive.agentId,
agent: primitive.agent,
escalationLevel: primitive.escalationLevel,
createdAt: primitive.createdAt,
updatedAt: primitive.updatedAt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import {
AgentField,
TicketPriority,
TicketStatus,
} from '../../../domain/entities/ticket.entity';
Expand All @@ -13,7 +14,7 @@ export interface ReadByIdTicketOutput {
description: string;
clientId: string;
status: TicketStatus;
agentId: string | null;
agent: AgentField | null;
groupId: string | null;
escalationLevel: number;
createdAt: Date;
Expand Down Expand Up @@ -42,7 +43,7 @@ export class ReadByIdTicketUseCase {
description: primitive.description,
clientId: primitive.clientId,
status: primitive.status,
agentId: primitive.agentId,
agent: primitive.agent,
groupId: primitive.groupId,
escalationLevel: primitive.escalationLevel,
createdAt: primitive.createdAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ describe('Ticket entity', () => {
expect(ticket.history.length).toBe(2);
expect(ticket.history[1]).toMatchObject({
event: TicketEvents.NEW_AGENT,
responsibleAgent: primitiveTicket.agentId,
responsibleAgent: { id: newAgentId, name: '' },
status: TicketStatus.IN_PROGRESS,
message: TicketEventMessage.NEW_AGENT_MSG,
});

expect(primitiveTicket.agentId).toBe(newAgentId);
expect(ticket.agentId).toBe(newAgentId);
expect(primitiveTicket.status).toBe(TicketStatus.IN_PROGRESS);
expect(primitiveTicket.updatedAt).toBeInstanceOf(Date);
expect(primitiveTicket.updatedAt).not.toBeNull();
Expand All @@ -71,11 +71,12 @@ describe('Ticket entity', () => {
const newGroupId = randomUUID();
ticket.escalate(newGroupId, 'ia');

expect(ticket.agentId).toBe(null);
expect(ticket.status).toBe(TicketStatus.ESCALATED);
expect(ticket.history.length).toBe(3);
expect(ticket.history[2]).toMatchObject({
event: TicketEvents.ESCALATE,
responsibleAgent: newAgentId,
responsibleAgent: null,
status: TicketStatus.ESCALATED,
message: TicketEventMessage.ESCALATE_MSG,
});
Expand All @@ -85,7 +86,6 @@ describe('Ticket entity', () => {
expect(primitiveTicket.groupId).toBe(newGroupId);
expect(primitiveTicket.category).toBe('ia');
expect(primitiveTicket.escalationLevel).toBe(1);
expect(primitiveTicket.agentId).toBeNull();
expect(primitiveTicket.updatedAt).toBeInstanceOf(Date);
expect(primitiveTicket.updatedAt).not.toBeNull();
});
Expand Down
32 changes: 21 additions & 11 deletions backend/src/modules/ticket/domain/entities/ticket.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,25 @@ export enum TicketValidationErrors {

export type TicketHistoryEntry = {
event: TicketEvents;
responsibleAgent: string | null;
responsibleAgent: AgentField | null;
status: TicketStatus;
message: string;
solution?: string | null;
occurredAt: Date;
};

export type AgentField = {
id: string;
name: string;
};

export class Ticket {
// Strutucture definition
private _id: string;

private _status: TicketStatus = TicketStatus.OPEN;
private _agentId: string | null = null;
private _agent: AgentField | null = null;
private _groupId: string | null = null;
private escalationLevel: number = 1;
private attachmentsUrls: string[] = [];
Expand All @@ -76,7 +82,11 @@ export class Ticket {
}

get agentId() {
return this._agentId;
return this._agentId ?? null;
}

get agent() {
return this._agent;
}

get groupId() {
Expand Down Expand Up @@ -135,7 +145,7 @@ export class Ticket {
fileUrls?: string[];
status: TicketStatus;
clientId: string;
agentId?: string;
agent?: AgentField | null;
groupId?: string;
escalationLevel: number;
history: TicketHistoryEntry[];
Expand All @@ -152,7 +162,7 @@ export class Ticket {

ticket._id = props._id;

ticket._agentId = props.agentId ?? null;
ticket._agent = props.agent ?? null;
ticket._groupId = props.groupId ?? null;
ticket.attachmentsUrls = props.fileUrls ?? [];

Expand All @@ -179,7 +189,8 @@ export class Ticket {
clientId: this._clientId,
fileUrls: this.attachmentsUrls,
status: this.status,
agentId: this._agentId,
agentId: this.agentId,
agent: this.agent,
groupId: this._groupId,
escalationLevel: this.escalationLevel,
history: this.history,
Expand All @@ -197,7 +208,7 @@ export class Ticket {
// Appends a new event to the ticket history
private addHistory(props: {
event: TicketEvents;
responsibleAgent: string | null;
responsibleAgent: AgentField | null;
status: TicketStatus;
message: string;
solution?: string | null;
Expand All @@ -217,7 +228,7 @@ export class Ticket {

this.addHistory({
event: TicketEvents.NEW_AGENT,
responsibleAgent: this._agentId,
responsibleAgent: { id: agentId, name: '' },
status: TicketStatus.IN_PROGRESS,
message: TicketEventMessage.NEW_AGENT_MSG,
});
Expand All @@ -243,14 +254,13 @@ export class Ticket {
this.escalationLevel++;
}

const previousAgentId = this._agentId;
this._agentId = null;

this._agent = null;
this._status = TicketStatus.ESCALATED;

this.addHistory({
event: TicketEvents.ESCALATE,
responsibleAgent: previousAgentId,
responsibleAgent: null,
status: TicketStatus.ESCALATED,
message: TicketEventMessage.ESCALATE_MSG,
solution: whatWasDone ?? null,
Expand All @@ -274,7 +284,7 @@ export class Ticket {

this.addHistory({
event: TicketEvents.CLOSE_TICKET,
responsibleAgent: this._agentId,
responsibleAgent: this._agent ?? null,
status: TicketStatus.CLOSED,
message: TicketEventMessage.CLOSE_TICKET_MSG,
solution: solution,
Expand Down
Loading
Loading