102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import type { AuthUser } from '../auth/auth.types';
|
|
import { tenantClient } from '../tenancy/actor';
|
|
import { TenantConnectionService } from '../tenancy/tenant-connection.service';
|
|
import type { UpsertCarrierDto } from './dto/upsert-carrier.dto';
|
|
|
|
@Injectable()
|
|
export class CarriersService {
|
|
constructor(private readonly tenants: TenantConnectionService) {}
|
|
|
|
async list(actor: AuthUser) {
|
|
const db = await tenantClient(this.tenants, actor);
|
|
const rows = await db.carrier.findMany({ orderBy: { name: 'asc' } });
|
|
return rows.map(serializeCarrier);
|
|
}
|
|
|
|
async create(actor: AuthUser, dto: UpsertCarrierDto, ip?: string) {
|
|
const db = await tenantClient(this.tenants, actor);
|
|
const carrier = await db.carrier.create({
|
|
data: {
|
|
name: dto.name,
|
|
document: dto.document,
|
|
phone: dto.phone,
|
|
baseFee: dto.baseFee ?? 0,
|
|
pricePerKg: dto.pricePerKg ?? 0,
|
|
pricePerKm: dto.pricePerKm ?? 0,
|
|
adValoremRate: dto.adValoremRate ?? 0,
|
|
active: dto.active ?? true,
|
|
},
|
|
});
|
|
await db.auditLog.create({
|
|
data: { userId: actor.sub, action: 'carrier.create', entity: 'Carrier', entityId: carrier.id, ip },
|
|
});
|
|
return serializeCarrier(carrier);
|
|
}
|
|
|
|
async update(actor: AuthUser, id: string, dto: UpsertCarrierDto, ip?: string) {
|
|
const db = await tenantClient(this.tenants, actor);
|
|
await this.ensure(db, id);
|
|
const carrier = await db.carrier.update({
|
|
where: { id },
|
|
data: {
|
|
name: dto.name,
|
|
document: dto.document,
|
|
phone: dto.phone,
|
|
baseFee: dto.baseFee,
|
|
pricePerKg: dto.pricePerKg,
|
|
pricePerKm: dto.pricePerKm,
|
|
adValoremRate: dto.adValoremRate,
|
|
active: dto.active,
|
|
},
|
|
});
|
|
await db.auditLog.create({
|
|
data: { userId: actor.sub, action: 'carrier.update', entity: 'Carrier', entityId: id, ip },
|
|
});
|
|
return serializeCarrier(carrier);
|
|
}
|
|
|
|
async remove(actor: AuthUser, id: string, ip?: string) {
|
|
const db = await tenantClient(this.tenants, actor);
|
|
await this.ensure(db, id);
|
|
await db.carrier.delete({ where: { id } });
|
|
await db.auditLog.create({
|
|
data: { userId: actor.sub, action: 'carrier.delete', entity: 'Carrier', entityId: id, ip },
|
|
});
|
|
return { ok: true };
|
|
}
|
|
|
|
private async ensure(db: Awaited<ReturnType<TenantConnectionService['getByTenantId']>>, id: string) {
|
|
const row = await db.carrier.findUnique({ where: { id } });
|
|
if (!row) {
|
|
throw new NotFoundException('Transportadora não encontrada.');
|
|
}
|
|
}
|
|
}
|
|
|
|
function serializeCarrier(row: {
|
|
id: string;
|
|
name: string;
|
|
document: string | null;
|
|
email: string | null;
|
|
phone: string | null;
|
|
baseFee: { toString(): string };
|
|
pricePerKg: { toString(): string };
|
|
pricePerKm: { toString(): string };
|
|
adValoremRate: { toString(): string };
|
|
active: boolean;
|
|
}) {
|
|
return {
|
|
id: row.id,
|
|
name: row.name,
|
|
document: row.document,
|
|
email: row.email,
|
|
phone: row.phone,
|
|
baseFee: Number(row.baseFee),
|
|
pricePerKg: Number(row.pricePerKg),
|
|
pricePerKm: Number(row.pricePerKm),
|
|
adValoremRate: Number(row.adValoremRate),
|
|
active: row.active,
|
|
};
|
|
}
|