From 221bac3b2a27b1bff2a4cb3f27d92661281e6d2e Mon Sep 17 00:00:00 2001 From: seaznCode Date: Wed, 11 Mar 2026 22:32:11 +0100 Subject: [PATCH] feat: enforce singleton active invoice template per language during updates and revisions --- services/template/DocumentTemplateService.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/services/template/DocumentTemplateService.js b/services/template/DocumentTemplateService.js index 351066b..ff94aa5 100644 --- a/services/template/DocumentTemplateService.js +++ b/services/template/DocumentTemplateService.js @@ -135,6 +135,14 @@ class DocumentTemplateService { }, 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); await uow.commit(); logger.info('DocumentTemplateService.updateTemplateState:success', { id, state }); @@ -186,6 +194,15 @@ class DocumentTemplateService { }, 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) await DocumentTemplateRepository.updateState(previousId, 'inactive', uow.connection);