fix(orchestration): expose queued Gate1 expiry and audited renewal
Deploy Canina / deploy (push) Successful in 4m51s

This commit is contained in:
devops authored and devops committed 2026-10-24 09:53:07 +00:00
1 parent 6e465663bd
commit 60a4e14b4b
12 files changed
+162 -12

No files matched your search

@@ -261,9 +261,24 @@ export class PrepareWorkflowService {
this.version(t, dto);
if (t.metadata.createdBy === a.id)
throw new ForbiddenException('Independent reviewer required');
if (!['GATE1_PENDING', 'GATE1_HELD'].includes(t.state))
const renewing = t.state === 'READY';
let queue: any;
if (renewing) {
const previous = await tx.orchestrationApprovalDecision.findFirst({
where: { taskId: id, gate: 1 }, orderBy: { createdAt: 'desc' },
});
queue = await tx.prepareWorkflowOutbox.findUnique({ where: { taskId: id } });
if (dto.notBefore !== undefined)
throw new BadRequestException('Renewal preserves the existing schedule');
if (dto.decision !== 'APPROVED' || !previous || previous.decision !== 'APPROVED' ||
previous.actorId === t.metadata.createdBy || previous.expiresAt > new Date() ||
!queue || queue.status !== 'PENDING' || queue.attempts !== 0 || queue.runId || queue.leaseToken)
throw new ConflictException('Only expired independent approval on an unstarted queue can be renewed');
} else if (!['GATE1_PENDING', 'GATE1_HELD'].includes(t.state))
throw new ConflictException('Gate1 pending required');
const now = Date.now();
if (renewing && queue.notBefore && new Date(queue.notBefore).getTime() >= now + 86400000)
throw new ConflictException('Existing schedule is outside the renewal validity window');
const scheduled = dto.notBefore !== undefined;
const notBefore = scheduled ? new Date(dto.notBefore) : null;
if (scheduled && (dto.decision !== 'APPROVED' || typeof dto.notBefore !== 'string' || !Number.isFinite(notBefore!.getTime()) || notBefore!.getTime() <= now || notBefore!.getTime() > now + 30 * 86400000))
@@ -277,7 +292,7 @@ export class PrepareWorkflowService {
throw new BadRequestException('Audit notes must be at most 2000 characters');
if (dto.decision === 'APPROVED') {
await this.allowed(tx, true);
await tx.prepareWorkflowOutbox.create({data:{taskId:id,notBefore}});
if (!renewing) await tx.prepareWorkflowOutbox.create({data:{taskId:id,notBefore}});
}
await tx.orchestrationApprovalDecision.create({
data: {
@@ -296,7 +311,7 @@ export class PrepareWorkflowService {
dto.decision === 'APPROVED' ? 'READY' : 'REJECTED',
a,
dto.idempotencyKey,
'PREPARE_GATE1',
renewing ? 'PREPARE_GATE1_RENEWED' : 'PREPARE_GATE1',
fingerprint,
);
});
@@ -574,7 +589,15 @@ export class PrepareWorkflowService {
(t.metadata?.workflowScope ?? t.metadata?.scope) === 'STAGE_PREPARE';
const owner =
a?.id && (t.metadata?.createdBy === a.id || a.role === 'SUPER_ADMIN');
const latest = [...(t.decisions ?? [])].filter((d: any) => d.gate === 1)
.sort((a: any, b: any) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0];
const expired = safe && t.state === 'READY' && latest?.decision === 'APPROVED' &&
latest.actorId !== t.metadata.createdBy && new Date(latest.expiresAt).getTime() <= Date.now();
const blocked = safe && t.state === 'READY' && (!latest || latest.decision !== 'APPROVED' ||
latest.actorId === t.metadata.createdBy || new Date(latest.expiresAt).getTime() <= Date.now());
const actions: string[] = [];
if (expired && this.reviewer(a) && t.metadata.createdBy !== a.id)
actions.push('APPROVE_GATE1');
if (safe && owner && t.state === 'DRAFT') actions.push('SUBMIT_GATE1');
if (safe && owner && t.state === 'GATE1_APPROVED')
actions.push('QUEUE_PREPARE');
@@ -589,6 +612,7 @@ export class PrepareWorkflowService {
}
return {
...t,
displayState: blocked ? (expired ? 'GATE1_RENEWAL_REQUIRED' : 'BLOCKED') : t.state,
metadata: safe
? {
scope: t.metadata.scope,