chore: clean pdf templates code

This commit is contained in:
Ahmed Bouhuolia
2024-09-15 22:55:39 +02:00
parent ef4beaa564
commit 94c08f0b9e
7 changed files with 106 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import { Knex } from 'knex';
import HasTenancyService from '../Tenancy/TenancyService'; import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork'; import UnitOfWork from '../UnitOfWork';
import { Knex } from 'knex';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
@@ -16,6 +16,13 @@ export class AssignPdfTemplateDefault {
@Inject() @Inject()
private eventPublisher: EventPublisher; private eventPublisher: EventPublisher;
/**
* Assigns a default PDF template for a specific tenant.
* @param {number} tenantId - The ID of the tenant for whom the default template is being assigned.
* @param {number} templateId - The ID of the template to be set as the default.
* @returns {Promise<void>} A promise that resolves when the operation is complete.
* @throws {Error} Throws ddan error if the specified template is not found.
*/
public async assignDefaultTemplate(tenantId: number, templateId: number) { public async assignDefaultTemplate(tenantId: number, templateId: number) {
const { PdfTemplate } = this.tenancy.models(tenantId); const { PdfTemplate } = this.tenancy.models(tenantId);

View File

@@ -1,4 +1,5 @@
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { IEditPdfTemplateDTO } from './types'; import { IEditPdfTemplateDTO } from './types';
import HasTenancyService from '../Tenancy/TenancyService'; import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork'; import UnitOfWork from '../UnitOfWork';
@@ -19,30 +20,39 @@ export class EditPdfTemplate {
/** /**
* Edits an existing pdf template. * Edits an existing pdf template.
* @param {number} tenantId * @param {number} tenantId
* @param {number} templateId * @param {number} templateId - Template id.
* @param {IEditPdfTemplateDTO} editTemplateDTO * @param {IEditPdfTemplateDTO} editTemplateDTO
*/ */
public editPdfTemplate( public async editPdfTemplate(
tenantId: number, tenantId: number,
templateId: number, templateId: number,
editTemplateDTO: IEditPdfTemplateDTO editTemplateDTO: IEditPdfTemplateDTO
) { ) {
const { PdfTemplate } = this.tenancy.models(tenantId); const { PdfTemplate } = this.tenancy.models(tenantId);
return this.uow.withTransaction(tenantId, async (trx) => { const oldPdfTemplate = await PdfTemplate.query()
.findById(templateId)
.throwIfNotFound();
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onPdfTemplateEditing` event.
await this.eventPublisher.emitAsync(events.pdfTemplate.onEditing, { await this.eventPublisher.emitAsync(events.pdfTemplate.onEditing, {
tenantId, tenantId,
templateId, templateId,
}); });
await PdfTemplate.query(trx).where('id', templateId).update({ const pdfTemplate = await PdfTemplate.query(trx)
templateName: editTemplateDTO.templateName, .where('id', templateId)
attributes: editTemplateDTO.attributes, .update({
}); templateName: editTemplateDTO.templateName,
attributes: editTemplateDTO.attributes,
});
// Triggers `onPdfTemplatedEdited` event.
await this.eventPublisher.emitAsync(events.pdfTemplate.onEdited, { await this.eventPublisher.emitAsync(events.pdfTemplate.onEdited, {
tenantId, tenantId,
templateId, templateId,
}); });
return pdfTemplate;
}); });
} }
} }

View File

@@ -18,11 +18,21 @@ export class GetPdfTemplatesTransformer extends Transformer {
return ['createdAtFormatted', 'resourceFormatted']; return ['createdAtFormatted', 'resourceFormatted'];
}; };
private createdAtFormatted = (template) => { /**
* Formats the creation date of the PDF template.
* @param {Object} template
* @returns {string} A formatted string representing the creation date of the template.
*/
protected createdAtFormatted = (template) => {
return this.formatDate(template.createdAt); return this.formatDate(template.createdAt);
}; };
private resourceFormatted = (template) => { /**
* Formats the creation date of the PDF template.
* @param {Object} template -
* @returns {string} A formatted string representing the creation date of the template.
*/
protected resourceFormatted = (template) => {
return getTransactionTypeLabel(template.resource); return getTransactionTypeLabel(template.resource);
}; };
} }

View File

@@ -27,6 +27,14 @@ export class PdfTemplateApplication {
@Inject() @Inject()
private assignPdfTemplateDefaultService: AssignPdfTemplateDefault; private assignPdfTemplateDefaultService: AssignPdfTemplateDefault;
/**
* Creates a new PDF template.
* @param {number} tenantId -
* @param {string} templateName - The name of the PDF template to create.
* @param {string} resource - The resource type associated with the PDF template.
* @param {ICreateInvoicePdfTemplateDTO} invoiceTemplateDTO - The data transfer object containing the details for the new PDF template.
* @returns {Promise<any>}
*/
public async createPdfTemplate( public async createPdfTemplate(
tenantId: number, tenantId: number,
templateName: string, templateName: string,
@@ -41,6 +49,13 @@ export class PdfTemplateApplication {
); );
} }
/**
* Edits an existing PDF template.
* @param {number} tenantId - The ID of the tenant.
* @param {number} templateId - The ID of the PDF template to edit.
* @param {IEditPdfTemplateDTO} editTemplateDTO - The data transfer object containing the updated details for the PDF template.
* @returns {Promise<any>}
*/
public async editPdfTemplate( public async editPdfTemplate(
tenantId: number, tenantId: number,
templateId: number, templateId: number,
@@ -53,6 +68,13 @@ export class PdfTemplateApplication {
); );
} }
/**
* Deletes a PDF template.
* @param {number} tenantId - The ID of the tenant.
* @param {number} templateId - The ID of the PDF template to delete.
* @returns {Promise<any>}
*/
public async deletePdfTemplate(tenantId: number, templateId: number) { public async deletePdfTemplate(tenantId: number, templateId: number) {
return this.deletePdfTemplateService.deletePdfTemplate( return this.deletePdfTemplateService.deletePdfTemplate(
tenantId, tenantId,
@@ -60,10 +82,22 @@ export class PdfTemplateApplication {
); );
} }
/**
* Retrieves a PDF template by its ID for a specified tenant.
* @param {number} tenantId -
* @param {number} templateId - The ID of the PDF template to retrieve.
* @returns {Promise<any>}
*/
public async getPdfTemplate(tenantId: number, templateId: number) { public async getPdfTemplate(tenantId: number, templateId: number) {
return this.getPdfTemplateService.getPdfTemplate(tenantId, templateId); return this.getPdfTemplateService.getPdfTemplate(tenantId, templateId);
} }
/**
* Retrieves a list of PDF templates.
* @param {number} tenantId - The ID of the tenant for which to retrieve templates.
* @param {Object} query
* @returns {Promise<any>}
*/
public async getPdfTemplates( public async getPdfTemplates(
tenantId: number, tenantId: number,
query?: { resource?: string } query?: { resource?: string }
@@ -71,6 +105,12 @@ export class PdfTemplateApplication {
return this.getPdfTemplatesService.getPdfTemplates(tenantId, query); return this.getPdfTemplatesService.getPdfTemplates(tenantId, query);
} }
/**
* Assigns a PDF template as the default template.
* @param {number} tenantId
* @param {number} templateId - The ID of the PDF template to assign as default.
* @returns {Promise<any>}
*/
public async assignPdfTemplateAsDefault( public async assignPdfTemplateAsDefault(
tenantId: number, tenantId: number,
templateId: number templateId: number

View File

@@ -685,6 +685,7 @@ export default {
onImportCommitted: 'onImportFileCommitted', onImportCommitted: 'onImportFileCommitted',
}, },
// Branding templates
pdfTemplate: { pdfTemplate: {
onCreating: 'onPdfTemplateCreating', onCreating: 'onPdfTemplateCreating',
onCreated: 'onPdfTemplateCreated', onCreated: 'onPdfTemplateCreated',
@@ -697,7 +698,5 @@ export default {
onAssignedDefault: 'onPdfTemplateAssignedDefault', onAssignedDefault: 'onPdfTemplateAssignedDefault',
onAssigningDefault: 'onPdfTemplateAssigningDefault', onAssigningDefault: 'onPdfTemplateAssigningDefault',
onInvoiceCreated: 'onInvoicePdfTemplateCreated',
}, },
}; };

View File

@@ -1,15 +1,14 @@
// @ts-nocheck // @ts-nocheck
import * as R from 'ramda'; import * as R from 'ramda';
import { Classes, Tag } from '@blueprintjs/core'; import { DataTable, TableSkeletonRows } from '@/components';
import clsx from 'classnames';
import { DataTable, Group, TableSkeletonRows } from '@/components';
import { useBrandingTemplatesBoot } from './BrandingTemplatesBoot'; import { useBrandingTemplatesBoot } from './BrandingTemplatesBoot';
import { ActionsMenu } from './_components'; import { ActionsMenu } from './_components';
import { DRAWERS } from '@/constants/drawers'; import { DRAWERS } from '@/constants/drawers';
import withAlertActions from '@/containers/Alert/withAlertActions'; import withAlertActions from '@/containers/Alert/withAlertActions';
import withDrawerActions from '@/containers/Drawer/withDrawerActions'; import withDrawerActions from '@/containers/Drawer/withDrawerActions';
import styles from './BrandTemplates.module.scss';
import { getCustomizeDrawerNameFromResource } from './_utils'; import { getCustomizeDrawerNameFromResource } from './_utils';
import { useBrandingTemplatesColumns } from './_hooks';
import styles from './BrandTemplates.module.scss';
interface BrandingTemplatesTableProps {} interface BrandingTemplatesTableProps {}
@@ -72,25 +71,3 @@ export const BrandingTemplatesTable = R.compose(
withAlertActions, withAlertActions,
withDrawerActions, withDrawerActions,
)(BrandingTemplateTableRoot); )(BrandingTemplateTableRoot);
const useBrandingTemplatesColumns = () => {
return [
{
Header: 'Template Name',
accessor: (row) => (
<Group spacing={10}>
{row.template_name} {row.default && <Tag round>Default</Tag>}
</Group>
),
width: 65,
clickable: true,
},
{
Header: 'Created At',
accessor: 'created_at_formatted',
width: 35,
className: clsx(Classes.TEXT_MUTED),
clickable: true,
},
];
};

View File

@@ -0,0 +1,25 @@
import clsx from 'classnames';
import { Classes, Tag } from '@blueprintjs/core';
import { Group } from '@/components';
export const useBrandingTemplatesColumns = () => {
return [
{
Header: 'Template Name',
accessor: (row: any) => (
<Group spacing={10}>
{row.template_name} {row.default && <Tag round>Default</Tag>}
</Group>
),
width: 65,
clickable: true,
},
{
Header: 'Created At',
accessor: 'created_at_formatted',
width: 35,
className: clsx(Classes.TEXT_MUTED),
clickable: true,
},
];
};