diff --git a/backend/src/services/certificate-template/certificate-template-dal.ts b/backend/src/services/certificate-template/certificate-template-dal.ts index a30c4890d..4db383951 100644 --- a/backend/src/services/certificate-template/certificate-template-dal.ts +++ b/backend/src/services/certificate-template/certificate-template-dal.ts @@ -1,3 +1,5 @@ +import { Knex } from "knex"; + import { TDbClient } from "@app/db"; import { TableName } from "@app/db/schemas"; import { DatabaseError } from "@app/lib/errors"; @@ -30,10 +32,9 @@ export const certificateTemplateDALFactory = (db: TDbClient) => { } }; - const getById = async (id: string) => { + const getById = async (id: string, tx?: Knex) => { try { - const certTemplate = await db - .replicaNode()(TableName.CertificateTemplate) + const certTemplate = await (tx || db.replicaNode())(TableName.CertificateTemplate) .join( TableName.CertificateAuthority, `${TableName.CertificateAuthority}.id`, diff --git a/backend/src/services/certificate-template/certificate-template-service.ts b/backend/src/services/certificate-template/certificate-template-service.ts index 31745e752..04d5e98eb 100644 --- a/backend/src/services/certificate-template/certificate-template-service.ts +++ b/backend/src/services/certificate-template/certificate-template-service.ts @@ -75,23 +75,28 @@ export const certificateTemplateServiceFactory = ({ ProjectPermissionSub.CertificateTemplates ); - const { id } = await certificateTemplateDAL.create({ - caId, - pkiCollectionId, - name, - commonName, - subjectAlternativeName, - ttl + return certificateTemplateDAL.transaction(async (tx) => { + const { id } = await certificateTemplateDAL.create( + { + caId, + pkiCollectionId, + name, + commonName, + subjectAlternativeName, + ttl + }, + tx + ); + + const certificateTemplate = await certificateTemplateDAL.getById(id, tx); + if (!certificateTemplate) { + throw new NotFoundError({ + message: "Certificate template not found" + }); + } + + return certificateTemplate; }); - - const certificateTemplate = await certificateTemplateDAL.getById(id); - if (!certificateTemplate) { - throw new NotFoundError({ - message: "Certificate template not found" - }); - } - - return certificateTemplate; }; const updateCertTemplate = async ({ @@ -136,23 +141,29 @@ export const certificateTemplateServiceFactory = ({ } } - await certificateTemplateDAL.updateById(certTemplate.id, { - caId, - pkiCollectionId, - commonName, - subjectAlternativeName, - name, - ttl + return certificateTemplateDAL.transaction(async (tx) => { + await certificateTemplateDAL.updateById( + certTemplate.id, + { + caId, + pkiCollectionId, + commonName, + subjectAlternativeName, + name, + ttl + }, + tx + ); + + const updatedTemplate = await certificateTemplateDAL.getById(id, tx); + if (!updatedTemplate) { + throw new NotFoundError({ + message: "Certificate template not found" + }); + } + + return updatedTemplate; }); - - const updatedTemplate = await certificateTemplateDAL.getById(id); - if (!updatedTemplate) { - throw new NotFoundError({ - message: "Certificate template not found" - }); - } - - return updatedTemplate; }; const deleteCertTemplate = async ({ id, actorId, actorAuthMethod, actor, actorOrgId }: TDeleteCertTemplateDTO) => {