F1.1 · Inspección multi-Acta y firma diferida
Implementa el nuevo núcleo operativo: múltiples Actas por Inspección, un Informe por Acta, cierre de campo independiente y firma de empresa diferida con conformidad/disidencia.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { Transform } from 'class-transformer';
|
||||
import { IsEnum, IsOptional, IsString, MaxLength, MinLength, ValidateIf } from 'class-validator';
|
||||
import { InspectionCompanySignatureManifestation } from '../../database/entities';
|
||||
import { CreateInspectionSignatureDto } from './create-inspection-signature.dto';
|
||||
|
||||
export class CreateCompanySignatureDto extends CreateInspectionSignatureDto {
|
||||
@IsOptional()
|
||||
@IsEnum(InspectionCompanySignatureManifestation)
|
||||
manifestation?: InspectionCompanySignatureManifestation;
|
||||
|
||||
@ValidateIf((dto: CreateCompanySignatureDto) =>
|
||||
dto.manifestation === InspectionCompanySignatureManifestation.DISSENT,
|
||||
)
|
||||
@Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
|
||||
@IsString()
|
||||
@MinLength(10)
|
||||
@MaxLength(4000)
|
||||
statement?: string;
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import type { AuthPrincipal, RequestWithContext } from '../common/http/request-c
|
||||
import { CloseInspectionActDto } from './dto/close-inspection-act.dto';
|
||||
import { CreateCompanyOutcomeDto } from './dto/create-company-outcome.dto';
|
||||
import { CreateInspectionSignatureDto } from './dto/create-inspection-signature.dto';
|
||||
import { CreateCompanySignatureDto } from './dto/create-company-signature.dto';
|
||||
import { UpsertInspectionResponsibleDto } from './dto/upsert-inspection-responsible.dto';
|
||||
import { InspectionClosingService } from './inspection-closing.service';
|
||||
import {
|
||||
@@ -89,7 +90,7 @@ export class InspectionClosingController {
|
||||
}))
|
||||
companySignature(
|
||||
@Param('actId', new ParseUUIDPipe({ version: '4' })) actId: string,
|
||||
@Body() dto: CreateInspectionSignatureDto,
|
||||
@Body() dto: CreateCompanySignatureDto,
|
||||
@UploadedFile() file: UploadedInspectionSignatureFile | undefined,
|
||||
@CurrentAuth() principal: AuthPrincipal,
|
||||
@Req() request: RequestWithContext,
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
InspectionActSignatureSource,
|
||||
InspectionActSignatureStatus,
|
||||
InspectionActSignerType,
|
||||
InspectionCompanySignatureManifestation,
|
||||
InspectionActStatus,
|
||||
InspectionActUploadMode,
|
||||
InspectionActVersionEvent,
|
||||
@@ -33,6 +34,7 @@ import type { CloseInspectionActDto } from './dto/close-inspection-act.dto';
|
||||
import { InspectionReportsService } from '../inspection-reports/inspection-reports.service';
|
||||
import type { CreateCompanyOutcomeDto } from './dto/create-company-outcome.dto';
|
||||
import type { CreateInspectionSignatureDto } from './dto/create-inspection-signature.dto';
|
||||
import type { CreateCompanySignatureDto } from './dto/create-company-signature.dto';
|
||||
import type { UpsertInspectionResponsibleDto } from './dto/upsert-inspection-responsible.dto';
|
||||
import {
|
||||
inspectInspectionSignatureFile,
|
||||
@@ -70,6 +72,8 @@ interface SignatureView {
|
||||
position: string | null;
|
||||
status: InspectionActSignatureStatus;
|
||||
reason: string | null;
|
||||
companyManifestation: InspectionCompanySignatureManifestation | null;
|
||||
companyStatement: string | null;
|
||||
mimeType: string | null;
|
||||
sizeBytes: number | null;
|
||||
imageSha256: string | null;
|
||||
@@ -429,7 +433,7 @@ export class InspectionClosingService {
|
||||
|
||||
async signCompany(
|
||||
actId: string,
|
||||
dto: CreateInspectionSignatureDto,
|
||||
dto: CreateCompanySignatureDto,
|
||||
file: UploadedInspectionSignatureFile | undefined,
|
||||
principal: AuthPrincipal,
|
||||
request: RequestWithContext,
|
||||
@@ -447,7 +451,7 @@ export class InspectionClosingService {
|
||||
assertMobileInspector(principal);
|
||||
return this.dataSource.transaction(async (manager) => {
|
||||
const { act, visit } = await this.lockContext(manager, actId);
|
||||
this.assertReadyInProgress(act, visit);
|
||||
this.assertReadyForCompanyOutcome(act, visit);
|
||||
await this.assertActorAssigned(manager, visit.id, principal, true);
|
||||
const responsible = await this.requireResponsible(manager, actId);
|
||||
if (
|
||||
@@ -540,7 +544,8 @@ export class InspectionClosingService {
|
||||
assertMobileInspector(principal);
|
||||
const closed = await this.dataSource.transaction(async (manager) => {
|
||||
const { act, visit } = await this.lockContext(manager, actId);
|
||||
this.assertReadyInProgress(act, visit);
|
||||
this.assertReadyForFinalClosure(act, visit);
|
||||
await this.assertActorAssigned(manager, visit.id, principal, true);
|
||||
const closure = await this.requireClosure(manager, actId);
|
||||
const signatures = await this.loadSignatures(manager, actId);
|
||||
if (!signatures.some((item) => (
|
||||
@@ -576,6 +581,8 @@ export class InspectionClosingService {
|
||||
position: item.position,
|
||||
status: item.status,
|
||||
reason: item.reason,
|
||||
companyManifestation: item.companyManifestation,
|
||||
companyStatement: item.companyStatement,
|
||||
imageSha256: item.imageSha256,
|
||||
consentText: item.consentText,
|
||||
consentVersion: item.consentVersion,
|
||||
@@ -636,14 +643,6 @@ export class InspectionClosingService {
|
||||
WHERE id = $1
|
||||
RETURNING current_version AS "versionNumber"
|
||||
`, [actId, serverClosedAt, principal.userId, finalSha256])) as Array<{ versionNumber: number }>;
|
||||
await manager.query(`
|
||||
UPDATE inspection_visits
|
||||
SET status = 'CLOSED',
|
||||
actual_closed_at = $2,
|
||||
updated_by = $3,
|
||||
updated_at = $2
|
||||
WHERE id = $1
|
||||
`, [visit.id, serverClosedAt, principal.userId]);
|
||||
await manager.query(`
|
||||
INSERT INTO inspection_act_versions (
|
||||
act_id, version_number, event, snapshot, actor_user_id, actor_username
|
||||
@@ -672,6 +671,7 @@ export class InspectionClosingService {
|
||||
visitId: visit.id,
|
||||
versionNumber: Number(updated.versionNumber),
|
||||
findingsRemainOpen: true,
|
||||
visitRemainsIndependent: true,
|
||||
},
|
||||
}, manager);
|
||||
await this.reports.ensureFrozenReport(manager, actId, principal, request);
|
||||
@@ -710,7 +710,7 @@ export class InspectionClosingService {
|
||||
|
||||
private async createSignedSignature(
|
||||
actId: string,
|
||||
dto: CreateInspectionSignatureDto,
|
||||
dto: CreateInspectionSignatureDto | CreateCompanySignatureDto,
|
||||
file: UploadedInspectionSignatureFile | undefined,
|
||||
fixedIdentity: null | {
|
||||
signerType: InspectionActSignerType;
|
||||
@@ -737,7 +737,8 @@ export class InspectionClosingService {
|
||||
try {
|
||||
return await this.dataSource.transaction(async (manager) => {
|
||||
const { act, visit } = await this.lockContext(manager, actId);
|
||||
this.assertReadyInProgress(act, visit);
|
||||
if (fixedIdentity) this.assertReadyInProgress(act, visit);
|
||||
else this.assertReadyForCompanyOutcome(act, visit);
|
||||
await this.assertActorAssigned(manager, visit.id, principal, !requireSelfAssignment);
|
||||
const closure = await this.requireClosure(manager, actId);
|
||||
const responsible = fixedIdentity ? null : await this.requireResponsible(manager, actId);
|
||||
@@ -778,6 +779,13 @@ export class InspectionClosingService {
|
||||
const source = this.signatureSource(principal);
|
||||
const signedAt = new Date();
|
||||
const clientSignedAt = dto.clientSignedAt ? new Date(dto.clientSignedAt) : null;
|
||||
const companyDto = fixedIdentity ? null : dto as CreateCompanySignatureDto;
|
||||
const companyManifestation = fixedIdentity
|
||||
? null
|
||||
: companyDto?.manifestation ?? InspectionCompanySignatureManifestation.CONFORMITY;
|
||||
const companyStatement = companyManifestation === InspectionCompanySignatureManifestation.DISSENT
|
||||
? companyDto?.statement?.trim() ?? null
|
||||
: null;
|
||||
const payload = {
|
||||
actId,
|
||||
preparedSha256: closure.preparedSha256,
|
||||
@@ -788,6 +796,8 @@ export class InspectionClosingService {
|
||||
documentNumber: identity.documentNumber,
|
||||
position: identity.position,
|
||||
status: InspectionActSignatureStatus.SIGNED,
|
||||
companyManifestation,
|
||||
companyStatement,
|
||||
imageSha256,
|
||||
consentText: identity.consentText,
|
||||
consentVersion: CONSENT_VERSION,
|
||||
@@ -806,6 +816,7 @@ export class InspectionClosingService {
|
||||
INSERT INTO inspection_act_signatures (
|
||||
id, act_id, signer_type, signer_user_id, signer_name,
|
||||
document_type, document_number, position, status,
|
||||
company_manifestation, company_statement,
|
||||
original_name, stored_name, mime_type, size_bytes, image_sha256,
|
||||
consent_text, consent_version, consent_accepted_at,
|
||||
client_signed_at, signed_at, latitude, longitude, accuracy_m,
|
||||
@@ -814,11 +825,12 @@ export class InspectionClosingService {
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, 'SIGNED',
|
||||
$9, $10, $11, $12, $13,
|
||||
$14, $15, $16,
|
||||
$17, $18, $19, $20, $21,
|
||||
$22, $23, $24, $25,
|
||||
$26, $18
|
||||
$9, $10,
|
||||
$11, $12, $13, $14, $15,
|
||||
$16, $17, $18,
|
||||
$19, $20, $21, $22, $23,
|
||||
$24, $25, $26, $27,
|
||||
$28, $20
|
||||
)
|
||||
`, [
|
||||
id,
|
||||
@@ -829,6 +841,8 @@ export class InspectionClosingService {
|
||||
identity.documentType,
|
||||
identity.documentNumber,
|
||||
identity.position,
|
||||
companyManifestation,
|
||||
companyStatement,
|
||||
inspected.originalName,
|
||||
storedName,
|
||||
inspected.mimeType,
|
||||
@@ -1266,6 +1280,8 @@ export class InspectionClosingService {
|
||||
signature.position,
|
||||
signature.status,
|
||||
signature.reason,
|
||||
signature.company_manifestation AS "companyManifestation",
|
||||
signature.company_statement AS "companyStatement",
|
||||
signature.original_name AS "originalName",
|
||||
signature.stored_name AS "storedName",
|
||||
signature.mime_type AS "mimeType",
|
||||
@@ -1365,6 +1381,26 @@ export class InspectionClosingService {
|
||||
}
|
||||
}
|
||||
|
||||
private assertReadyForCompanyOutcome(act: InspectionAct, visit: InspectionVisit): void {
|
||||
if (act.status !== InspectionActStatus.READY
|
||||
|| ![InspectionVisitStatus.IN_PROGRESS, InspectionVisitStatus.CLOSED].includes(visit.status)) {
|
||||
throw new ConflictException({
|
||||
code: 'INSPECTION_ACT_NOT_READY_FOR_COMPANY',
|
||||
message: 'El acta debe estar preparada; la firma de empresa puede completarse durante la inspección o después de su cierre',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private assertReadyForFinalClosure(act: InspectionAct, visit: InspectionVisit): void {
|
||||
if (act.status !== InspectionActStatus.READY
|
||||
|| ![InspectionVisitStatus.IN_PROGRESS, InspectionVisitStatus.CLOSED].includes(visit.status)) {
|
||||
throw new ConflictException({
|
||||
code: 'INSPECTION_ACT_NOT_READY',
|
||||
message: 'El acta debe estar preparada para completar su cierre definitivo',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async assertActorAssigned(
|
||||
manager: EntityManager,
|
||||
visitId: string,
|
||||
|
||||
Reference in New Issue
Block a user