mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
feat: cannot delete a predefined branding template
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import HasTenancyService from '../Tenancy/TenancyService';
|
import HasTenancyService from '../Tenancy/TenancyService';
|
||||||
import UnitOfWork from '../UnitOfWork';
|
import UnitOfWork from '../UnitOfWork';
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import events from '@/subscribers/events';
|
import events from '@/subscribers/events';
|
||||||
|
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||||
|
import { ServiceError } from '@/exceptions';
|
||||||
|
import { ERRORS } from './types';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class DeletePdfTemplate {
|
export class DeletePdfTemplate {
|
||||||
@@ -18,16 +20,26 @@ export class DeletePdfTemplate {
|
|||||||
/**
|
/**
|
||||||
* Deletes a pdf template.
|
* Deletes a pdf template.
|
||||||
* @param {number} tenantId
|
* @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 { 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) => {
|
return this.uow.withTransaction(tenantId, async (trx) => {
|
||||||
// Triggers `onPdfTemplateDeleting` event.
|
// Triggers `onPdfTemplateDeleting` event.
|
||||||
await this.eventPublisher.emitAsync(events.pdfTemplate.onDeleting, {
|
await this.eventPublisher.emitAsync(events.pdfTemplate.onDeleting, {
|
||||||
tenantId,
|
tenantId,
|
||||||
templateId,
|
templateId,
|
||||||
|
oldPdfTemplate,
|
||||||
|
trx,
|
||||||
});
|
});
|
||||||
await PdfTemplate.query(trx).deleteById(templateId);
|
await PdfTemplate.query(trx).deleteById(templateId);
|
||||||
|
|
||||||
@@ -35,6 +47,8 @@ export class DeletePdfTemplate {
|
|||||||
await this.eventPublisher.emitAsync(events.pdfTemplate.onDeleted, {
|
await this.eventPublisher.emitAsync(events.pdfTemplate.onDeleted, {
|
||||||
tenantId,
|
tenantId,
|
||||||
templateId,
|
templateId,
|
||||||
|
oldPdfTemplate,
|
||||||
|
trx,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
export enum ERRORS {
|
||||||
|
CANNOT_DELETE_PREDEFINED_PDF_TEMPLATE = 'CANNOT_DELETE_PREDEFINED_PDF_TEMPLATE',
|
||||||
|
}
|
||||||
export interface ICreateInvoicePdfTemplateDTO {
|
export interface ICreateInvoicePdfTemplateDTO {
|
||||||
// Colors
|
// Colors
|
||||||
primaryColor?: string;
|
primaryColor?: string;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import React from 'react';
|
|||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import { AppToaster } from '@/components';
|
import { AppToaster } from '@/components';
|
||||||
import { Alert, Intent } from '@blueprintjs/core';
|
import { Alert, Intent } from '@blueprintjs/core';
|
||||||
import { useDeletePdfTemplate} from '@/hooks/query/pdf-templates';
|
import { useDeletePdfTemplate } from '@/hooks/query/pdf-templates';
|
||||||
|
|
||||||
import withAlertStoreConnect from '@/containers/Alert/withAlertStoreConnect';
|
import withAlertStoreConnect from '@/containers/Alert/withAlertStoreConnect';
|
||||||
import withAlertActions from '@/containers/Alert/withAlertActions';
|
import withAlertActions from '@/containers/Alert/withAlertActions';
|
||||||
@@ -35,13 +35,30 @@ function DeleteBrandingTemplateAlert({
|
|||||||
});
|
});
|
||||||
closeAlert(name);
|
closeAlert(name);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch(
|
||||||
AppToaster.show({
|
({
|
||||||
message: 'Something went wrong.',
|
response: {
|
||||||
intent: Intent.DANGER,
|
data: { errors },
|
||||||
});
|
},
|
||||||
closeAlert(name);
|
}) => {
|
||||||
});
|
if (
|
||||||
|
errors.find(
|
||||||
|
(error) => error.type === 'CANNOT_DELETE_PREDEFINED_PDF_TEMPLATE',
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
AppToaster.show({
|
||||||
|
message: 'Cannot delete a predefined branding template.',
|
||||||
|
intent: Intent.DANGER,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
AppToaster.show({
|
||||||
|
message: 'Something went wrong.',
|
||||||
|
intent: Intent.DANGER,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
closeAlert(name);
|
||||||
|
},
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
@@ -57,9 +74,7 @@ function DeleteBrandingTemplateAlert({
|
|||||||
onCancel={handleCancel}
|
onCancel={handleCancel}
|
||||||
onConfirm={handleConfirmDelete}
|
onConfirm={handleConfirmDelete}
|
||||||
>
|
>
|
||||||
<p>
|
<p>Are you sure want to delete branding template?</p>
|
||||||
Are you sure want to delete branding template?
|
|
||||||
</p>
|
|
||||||
</Alert>
|
</Alert>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -68,5 +83,3 @@ export default compose(
|
|||||||
withAlertStoreConnect(),
|
withAlertStoreConnect(),
|
||||||
withAlertActions,
|
withAlertActions,
|
||||||
)(DeleteBrandingTemplateAlert);
|
)(DeleteBrandingTemplateAlert);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user