refactor: wip to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-25 00:43:55 +02:00
parent 336171081e
commit a6932d76f3
249 changed files with 21314 additions and 1616 deletions

View File

@@ -0,0 +1,217 @@
import { Transformer } from '@/modules/Transformer/Transformer';
import { Bill } from '../models/Bill';
import { AttachmentTransformer } from '@/modules/Attachments/Attachment.transformer';
import { ItemEntryTransformer } from '@/modules/TransactionItemEntry/ItemEntry.transformer';
import { SaleInvoiceTaxEntryTransformer } from '@/modules/SaleInvoices/queries/SaleInvoiceTaxEntry.transformer';
export class BillTransformer extends Transformer {
/**
* Include these attributes to sale bill object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return [
'formattedBillDate',
'formattedDueDate',
'formattedCreatedAt',
'formattedAmount',
'formattedPaymentAmount',
'formattedBalance',
'formattedDueAmount',
'formattedExchangeRate',
'subtotalFormatted',
'subtotalLocalFormatted',
'subtotalExcludingTaxFormatted',
'taxAmountWithheldLocalFormatted',
'totalFormatted',
'totalLocalFormatted',
'taxes',
'entries',
'attachments',
];
};
/**
* Excluded attributes.
* @returns {string[]}
*/
public excludeAttributes = (): string[] => {
return ['amount', 'amountLocal', 'localAmount'];
};
/**
* Retrieve formatted bill date.
* @param {IBill} bill
* @returns {String}
*/
protected formattedBillDate = (bill: Bill): string => {
return this.formatDate(bill.billDate);
};
/**
* Retrieve formatted bill date.
* @param {IBill} bill
* @returns {String}
*/
protected formattedDueDate = (bill: Bill): string => {
return this.formatDate(bill.dueDate);
};
/**
* Retrieve the formatted created at date.
* @param {IBill} bill
* @returns {string}
*/
protected formattedCreatedAt = (bill: Bill): string => {
return this.formatDate(bill.createdAt);
};
/**
* Retrieve formatted bill amount.
* @param {IBill} bill
* @returns {string}
*/
protected formattedAmount = (bill: Bill): string => {
return this.formatNumber(bill.amount, { currencyCode: bill.currencyCode });
};
/**
* Retrieve formatted bill amount.
* @param {IBill} bill
* @returns {string}
*/
protected formattedPaymentAmount = (bill: Bill): string => {
return this.formatNumber(bill.paymentAmount, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieve formatted bill amount.
* @param {IBill} bill
* @returns {string}
*/
protected formattedDueAmount = (bill: Bill): string => {
return this.formatNumber(bill.dueAmount, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieve formatted bill balance.
* @param {IBill} bill
* @returns {string}
*/
protected formattedBalance = (bill: Bill): string => {
return this.formatNumber(bill.balance, { currencyCode: bill.currencyCode });
};
/**
* Retrieve the formatted exchange rate.
* @param {IBill} bill
* @returns {string}
*/
protected formattedExchangeRate = (bill: Bill): string => {
return this.formatNumber(bill.exchangeRate, {
currencyCode: this.context.organization.baseCurrency,
});
};
/**
* Retrieves the formatted subtotal.
* @param {IBill} bill
* @returns {string}
*/
protected subtotalFormatted = (bill: Bill): string => {
return this.formatNumber(bill.subtotal, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieves the local subtotal formatted.
* @param {IBill} bill
* @returns {string}
*/
protected subtotalLocalFormatted = (bill: Bill): string => {
return this.formatNumber(bill.subtotalLocal, {
currencyCode: this.context.organization.baseCurrency,
});
};
/**
* Retrieves the formatted subtotal tax excluded.
* @param {IBill} bill
* @returns {string}
*/
protected subtotalExcludingTaxFormatted = (bill: Bill): string => {
return this.formatNumber(bill.subtotalExcludingTax, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieves the local formatted tax amount withheld
* @param {IBill} bill
* @returns {string}
*/
protected taxAmountWithheldLocalFormatted = (bill: Bill): string => {
return this.formatNumber(bill.taxAmountWithheldLocal, {
currencyCode: this.context.organization.baseCurrency,
});
};
/**
* Retrieves the total formatted.
* @param {IBill} bill
* @returns {string}
*/
protected totalFormatted = (bill: Bill): string => {
return this.formatNumber(bill.total, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieves the local total formatted.
* @param {IBill} bill
* @returns {string}
*/
protected totalLocalFormatted = (bill: Bill): string => {
return this.formatNumber(bill.totalLocal, {
currencyCode: this.context.organization.baseCurrency,
});
};
/**
* Retrieve the taxes lines of bill.
* @param {Bill} bill
*/
// protected taxes = (bill: Bill) => {
// return this.item(bill.taxes, new SaleInvoiceTaxEntryTransformer(), {
// subtotal: bill.subtotal,
// isInclusiveTax: bill.isInclusiveTax,
// currencyCode: bill.currencyCode,
// });
// };
/**
* Retrieves the entries of the bill.
* @param {Bill} credit
* @returns {}
*/
// protected entries = (bill: Bill) => {
// return this.item(bill.entries, new ItemEntryTransformer(), {
// currencyCode: bill.currencyCode,
// });
// };
/**
* Retrieves the bill attachments.
* @param {ISaleInvoice} invoice
* @returns
*/
// protected attachments = (bill: Bill) => {
// return this.item(bill.attachments, new AttachmentTransformer());
// };
}

View File

@@ -0,0 +1,38 @@
import { Inject, Injectable } from '@nestjs/common';
import { BillsValidators } from '../commands/BillsValidators.service';
import { BillTransformer } from './Bill.transformer';
import { Bill } from '../models/Bill';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
@Injectable()
export class GetBill {
constructor(
@Inject(Bill.name) private billModel: typeof Bill,
private transformer: TransformerInjectable,
private validators: BillsValidators,
) {}
/**
* Retrieve the given bill details with associated items entries.
* @param {Integer} billId - Specific bill.
* @returns {Promise<IBill>}
*/
public async getBill(billId: number): Promise<Bill> {
const bill = await this.billModel
.query()
.findById(billId)
.withGraphFetched('vendor')
.withGraphFetched('entries.item')
.withGraphFetched('branch')
.withGraphFetched('taxes.taxRate')
.withGraphFetched('attachments');
// Validates the bill existence.
this.validators.validateBillExistance(bill);
return this.transformer.transform(
bill,
new BillTransformer(),
);
}
}

View File

@@ -0,0 +1,32 @@
import { Inject, Injectable } from '@nestjs/common';
import { BillPaymentEntry } from '@/modules/BillPayments/models/BillPaymentEntry';
import { BillPaymentTransactionTransformer } from '@/modules/BillPayments/queries/BillPaymentTransactionTransformer';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
@Injectable()
export class GetBillPayments {
constructor(
@Inject(BillPaymentEntry.name)
private billPaymentEntryModel: typeof BillPaymentEntry,
private transformer: TransformerInjectable,
) {}
/**
* Retrieve the specific bill associated payment transactions.
* @param {number} billId
* @returns {}
*/
public getBillPayments = async (billId: number) => {
const billsEntries = await this.billPaymentEntryModel
.query()
.where('billId', billId)
.withGraphJoined('payment.paymentAccount')
.withGraphJoined('bill')
.orderBy('payment:paymentDate', 'ASC');
return this.transformer.transform(
billsEntries,
new BillPaymentTransactionTransformer(),
);
};
}

View File

@@ -0,0 +1,72 @@
// import { Injectable } from '@nestjs/common';
// import * as R from 'ramda';
// import {
// IBill,
// IBillsFilter,
// IFilterMeta,
// IPaginationMeta,
// } from '@/interfaces';
// import { BillTransformer } from './Bill.transformer';
// import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
// // import { DynamicListingService } from '@/modules/DynamicListing/DynamicListService';
// @Injectable()
// export class GetBills {
// constructor(
// private transformer: TransformerInjectable,
// private dynamicListService: DynamicListingService,
// ) {}
// /**
// * Retrieve bills data table list.
// * @param {number} tenantId -
// * @param {IBillsFilter} billsFilter -
// */
// public async getBills(
// tenantId: number,
// filterDTO: IBillsFilter,
// ): Promise<{
// bills: IBill;
// pagination: IPaginationMeta;
// filterMeta: IFilterMeta;
// }> {
// // Parses bills list filter DTO.
// const filter = this.parseListFilterDTO(filterDTO);
// // Dynamic list service.
// const dynamicFilter = await this.dynamicListService.dynamicList(
// tenantId,
// Bill,
// filter,
// );
// const { results, pagination } = await Bill.query()
// .onBuild((builder) => {
// builder.withGraphFetched('vendor');
// builder.withGraphFetched('entries.item');
// dynamicFilter.buildQuery()(builder);
// // Filter query.
// filterDTO?.filterQuery && filterDTO?.filterQuery(builder);
// })
// .pagination(filter.page - 1, filter.pageSize);
// // Tranform the bills to POJO.
// const bills = await this.transformer.transform(
// results,
// new PurchaseInvoiceTransformer(),
// );
// return {
// bills,
// pagination,
// filterMeta: dynamicFilter.getResponseMeta(),
// };
// }
// /**
// * Parses bills list filter DTO.
// * @param filterDTO -
// */
// private parseListFilterDTO(filterDTO) {
// return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
// }
// }

View File

@@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';
import { Bill } from '../models/Bill';
@Injectable()
export class GetDueBills {
constructor(private billModel: typeof Bill) {}
/**
* Retrieve all due bills or for specific given vendor id.
* @param {number} vendorId -
*/
public async getDueBills(vendorId?: number): Promise<Bill[]> {
const dueBills = await this.billModel.query().onBuild((query) => {
query.orderBy('bill_date', 'DESC');
query.modify('dueBills');
if (vendorId) {
query.where('vendor_id', vendorId);
}
});
return dueBills;
}
}