Skip to content
Merged
62 changes: 60 additions & 2 deletions backend/src/features/dashboard/dashboard.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { DashboardService } from './dashboard.service';
function mkPrisma() {
return {
company: { count: jest.fn().mockResolvedValue(0) },
process: { count: jest.fn().mockResolvedValue(0) },
process: {
count: jest.fn().mockResolvedValue(0),
groupBy: jest.fn().mockResolvedValue([]),
},
user: {
count: jest.fn().mockResolvedValue(0),
findUnique: jest.fn().mockResolvedValue(null),
findMany: jest.fn().mockResolvedValue([]),
},
car: { count: jest.fn().mockResolvedValue(0) },
Expand All @@ -23,13 +27,67 @@ function mkAdminDatabaseService(counts: any[] = []) {
}

function mkSvc(sales: any[] = [], counts: any[] = []) {
return mkSvcWithPrisma(mkPrisma(), sales, counts);
}

function mkSvcWithPrisma(
prisma: ReturnType<typeof mkPrisma>,
sales: any[] = [],
counts: any[] = [],
) {
return new DashboardService(
mkPrisma(),
prisma,
mkCommissionsService(sales),
mkAdminDatabaseService(counts),
);
}

describe('DashboardService — produtos ativos e totais', () => {
it('retorna ativos separados do total no dashboard administrativo', async () => {
const prisma = mkPrisma();
prisma.car.count.mockImplementation(({ where }: any = {}) =>
Promise.resolve(where?.is_active ? 2 : 3),
);
prisma.boat.count.mockImplementation(({ where }: any = {}) =>
Promise.resolve(where?.is_active ? 1 : 4),
);
prisma.aircraft.count.mockImplementation(({ where }: any = {}) =>
Promise.resolve(where?.is_active ? 5 : 6),
);

const result = await mkSvcWithPrisma(prisma).getAdminStats();

expect(result.totalProducts).toBe(13);
expect(result.activeProducts).toBe(8);
expect(prisma.car.count).toHaveBeenCalledWith({
where: { is_active: true },
});
expect(prisma.boat.count).toHaveBeenCalledWith({
where: { is_active: true },
});
expect(prisma.aircraft.count).toHaveBeenCalledWith({
where: { is_active: true },
});
});

it('retorna ativos separados do total para o especialista', async () => {
const prisma = mkPrisma();
prisma.user.findUnique.mockResolvedValue({ speciality: 'CAR' });
prisma.car.count.mockImplementation(({ where }: any) =>
Promise.resolve(where.is_active ? 2 : 5),
);

const result =
await mkSvcWithPrisma(prisma).getSpecialistStats('specialist-1');

expect(result.productsListed).toBe(5);
expect(result.activeProducts).toBe(2);
expect(prisma.car.count).toHaveBeenCalledWith({
where: { specialist_id: 'specialist-1', is_active: true },
});
});
});

describe('DashboardService — getAdminStats sem conversionRate', () => {
it('não retorna mais o campo conversionRate', async () => {
const result = await mkSvc().getAdminStats();
Expand Down
26 changes: 26 additions & 0 deletions backend/src/features/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export class DashboardService {
totalCars,
totalBoats,
totalAircrafts,
activeCars,
activeBoats,
activeAircrafts,
salesByMonth,
consultantsPerformance,
sales,
Expand All @@ -39,20 +42,25 @@ export class DashboardService {
this.prisma.car.count(),
this.prisma.boat.count(),
this.prisma.aircraft.count(),
this.prisma.car.count({ where: { is_active: true } }),
this.prisma.boat.count({ where: { is_active: true } }),
this.prisma.aircraft.count({ where: { is_active: true } }),
this.buildMonthlySalesData({}),
this.getConsultantsPerformance(),
this.commissionsService.listSales(),
this.adminDatabaseService.countAll(),
]);

const totalProducts = totalCars + totalBoats + totalAircrafts;
const activeProducts = activeCars + activeBoats + activeAircrafts;

return {
activeProcesses,
activeCompanies,
totalClients,
specialistsCount,
totalProducts,
activeProducts,
productsByType: {
cars: totalCars,
boats: totalBoats,
Expand Down Expand Up @@ -110,15 +118,32 @@ export class DashboardService {
})
: Promise.resolve(0);

const activeProductsPromise =
specialist?.speciality === 'CAR'
? this.prisma.car.count({
where: { specialist_id: specialistId, is_active: true },
})
: specialist?.speciality === 'BOAT'
? this.prisma.boat.count({
where: { specialist_id: specialistId, is_active: true },
})
: specialist?.speciality === 'AIRCRAFT'
? this.prisma.aircraft.count({
where: { specialist_id: specialistId, is_active: true },
})
: Promise.resolve(0);

const [
productsListed,
activeProducts,
activeProcesses,
completedSales,
totalProcesses,
monthsData,
processesByStatus,
] = await Promise.all([
productsListedPromise,
activeProductsPromise,
this.prisma.process.count({
where: {
specialist_id: specialistId,
Expand Down Expand Up @@ -149,6 +174,7 @@ export class DashboardService {

return {
productsListed,
activeProducts,
activeProcesses,
completedSales,
conversionRate,
Expand Down
Binary file added docs/e2e-evidence/commission-after.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/e2e-evidence/commission-before.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions docs/e2e-evidence/commission-total-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Evidência E2E — valor total da comissão

Data: 14 de outubro de 2026

Fluxo validado na rota real `/specialist/contracts/new`, com autenticação e dados de prefill fornecidos por uma API local controlada.

## 1. Estado inicial

Com a porcentagem em `0`, a prévia financeira permanece oculta.

![Etapa de comissão antes do preenchimento](./commission-before.jpg)

## 2. Resultado após o preenchimento

Ao informar `10%` sobre uma venda de `R$ 100.000,00`, a mesma seção exibe:

- Valor total da comissão: `R$ 10.000,00`
- Seu ganho estimado: `R$ 7.000,00`

![Etapa de comissão mostrando o total e o ganho estimado](./commission-after.jpg)

O DOM foi verificado pelos rótulos e valores exatos, e o console do navegador não registrou erros nem avisos durante o fluxo.

Loading
Loading