feat: enforce singleton active invoice template per language during updates and revisions

This commit is contained in:
seaznCode 2026-03-11 22:32:11 +01:00
parent f544dab9a3
commit 221bac3b2a

View File

@ -135,6 +135,14 @@ class DocumentTemplateService {
}, uow.connection); }, uow.connection);
} }
// Enforce singleton active invoice template per lang
if (state === 'active' && current.type === 'invoice') {
await DocumentTemplateRepository.deactivateOtherActiveInvoices({
excludeId: id,
lang: current.lang,
}, uow.connection);
}
const updated = await DocumentTemplateRepository.findById(id, uow.connection); const updated = await DocumentTemplateRepository.findById(id, uow.connection);
await uow.commit(); await uow.commit();
logger.info('DocumentTemplateService.updateTemplateState:success', { id, state }); logger.info('DocumentTemplateService.updateTemplateState:success', { id, state });
@ -186,6 +194,15 @@ class DocumentTemplateService {
}, uow.connection); }, uow.connection);
} }
// If new template is active and is an invoice template, deactivate other active invoices for same lang
const effectiveType = data.type || previous.type;
if (nextState === 'active' && effectiveType === 'invoice') {
await DocumentTemplateRepository.deactivateOtherActiveInvoices({
excludeId: created.id,
lang: (data.lang || previous.lang),
}, uow.connection);
}
// Deactivate previous (requirement: deactive the previous one) // Deactivate previous (requirement: deactive the previous one)
await DocumentTemplateRepository.updateState(previousId, 'inactive', uow.connection); await DocumentTemplateRepository.updateState(previousId, 'inactive', uow.connection);