feat: cannot delete a predefined branding template

This commit is contained in:
Ahmed Bouhuolia
2024-09-14 16:26:34 +02:00
parent df0f73f338
commit 28319c2cdc
3 changed files with 46 additions and 16 deletions

View File

@@ -1,8 +1,10 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { ServiceError } from '@/exceptions';
import { ERRORS } from './types';
@Service()
export class DeletePdfTemplate {
@@ -18,16 +20,26 @@ export class DeletePdfTemplate {
/**
* Deletes a pdf template.
* @param {number} tenantId
* @param {number} templateId
* @param {number} templateId - Pdf template id.
*/
public deletePdfTemplate(tenantId: number, templateId: number) {
public async deletePdfTemplate(tenantId: number, templateId: number) {
const { PdfTemplate } = this.tenancy.models(tenantId);
const oldPdfTemplate = await PdfTemplate.query()
.findById(templateId)
.throwIfNotFound();
// Cannot delete the predefined pdf templates.
if (oldPdfTemplate.predefined) {
throw new ServiceError(ERRORS.CANNOT_DELETE_PREDEFINED_PDF_TEMPLATE);
}
return this.uow.withTransaction(tenantId, async (trx) => {
// Triggers `onPdfTemplateDeleting` event.
await this.eventPublisher.emitAsync(events.pdfTemplate.onDeleting, {
tenantId,
templateId,
oldPdfTemplate,
trx,
});
await PdfTemplate.query(trx).deleteById(templateId);
@@ -35,6 +47,8 @@ export class DeletePdfTemplate {
await this.eventPublisher.emitAsync(events.pdfTemplate.onDeleted, {
tenantId,
templateId,
oldPdfTemplate,
trx,
});
});
}

View File

@@ -1,3 +1,6 @@
export enum ERRORS {
CANNOT_DELETE_PREDEFINED_PDF_TEMPLATE = 'CANNOT_DELETE_PREDEFINED_PDF_TEMPLATE',
}
export interface ICreateInvoicePdfTemplateDTO {
// Colors
primaryColor?: string;