mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
chore: clean pdf templates code
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import UnitOfWork from '../UnitOfWork';
|
||||
import { Knex } from 'knex';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import events from '@/subscribers/events';
|
||||
|
||||
@@ -16,6 +16,13 @@ export class AssignPdfTemplateDefault {
|
||||
@Inject()
|
||||
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) {
|
||||
const { PdfTemplate } = this.tenancy.models(tenantId);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import { IEditPdfTemplateDTO } from './types';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import UnitOfWork from '../UnitOfWork';
|
||||
@@ -19,30 +20,39 @@ export class EditPdfTemplate {
|
||||
/**
|
||||
* Edits an existing pdf template.
|
||||
* @param {number} tenantId
|
||||
* @param {number} templateId
|
||||
* @param {number} templateId - Template id.
|
||||
* @param {IEditPdfTemplateDTO} editTemplateDTO
|
||||
*/
|
||||
public editPdfTemplate(
|
||||
public async editPdfTemplate(
|
||||
tenantId: number,
|
||||
templateId: number,
|
||||
editTemplateDTO: IEditPdfTemplateDTO
|
||||
) {
|
||||
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, {
|
||||
tenantId,
|
||||
templateId,
|
||||
});
|
||||
await PdfTemplate.query(trx).where('id', templateId).update({
|
||||
templateName: editTemplateDTO.templateName,
|
||||
attributes: editTemplateDTO.attributes,
|
||||
});
|
||||
const pdfTemplate = await PdfTemplate.query(trx)
|
||||
.where('id', templateId)
|
||||
.update({
|
||||
templateName: editTemplateDTO.templateName,
|
||||
attributes: editTemplateDTO.attributes,
|
||||
});
|
||||
|
||||
// Triggers `onPdfTemplatedEdited` event.
|
||||
await this.eventPublisher.emitAsync(events.pdfTemplate.onEdited, {
|
||||
tenantId,
|
||||
templateId,
|
||||
});
|
||||
return pdfTemplate;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,21 @@ export class GetPdfTemplatesTransformer extends Transformer {
|
||||
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);
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,6 +27,14 @@ export class PdfTemplateApplication {
|
||||
@Inject()
|
||||
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(
|
||||
tenantId: number,
|
||||
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(
|
||||
tenantId: 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) {
|
||||
return this.deletePdfTemplateService.deletePdfTemplate(
|
||||
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) {
|
||||
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(
|
||||
tenantId: number,
|
||||
query?: { resource?: string }
|
||||
@@ -71,6 +105,12 @@ export class PdfTemplateApplication {
|
||||
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(
|
||||
tenantId: number,
|
||||
templateId: number
|
||||
|
||||
Reference in New Issue
Block a user