feat(nestjs): migrate to NestJS

This commit is contained in:
Ahmed Bouhuolia
2025-04-07 11:51:24 +02:00
parent f068218a16
commit 55fcc908ef
3779 changed files with 631 additions and 195332 deletions

View File

@@ -0,0 +1,78 @@
import { Knex } from 'knex';
import { Inject, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import {
IVendorOpeningBalanceEditDTO,
IVendorOpeningBalanceEditedPayload,
IVendorOpeningBalanceEditingPayload,
} from '../types/Vendors.types';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { Vendor } from '../models/Vendor';
import { events } from '@/common/events/events';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class EditOpeningBalanceVendorService {
/**
* @param {EventEmitter2} eventPublisher - Event emitter service.
* @param {UnitOfWork} uow - Unit of work service.
* @param {typeof Vendor} vendorModel - Vendor model.
*/
constructor(
private readonly eventPublisher: EventEmitter2,
private readonly uow: UnitOfWork,
@Inject(Vendor.name)
private readonly vendorModel: TenantModelProxy<typeof Vendor>,
) {}
/**
* Changes the opening balance of the given customer.
* @param {number} vendorId
* @param {IVendorOpeningBalanceEditDTO} openingBalanceEditDTO
* @returns {Promise<IVendor>}
*/
public async editOpeningBalance(
vendorId: number,
openingBalanceEditDTO: IVendorOpeningBalanceEditDTO,
) {
// Retrieves the old vendor or throw not found error.
const oldVendor = await this.vendorModel()
.query()
.findById(vendorId)
.throwIfNotFound();
// Mutates the customer opening balance under unit-of-work.
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Triggers `onVendorOpeingBalanceChanging` event.
await this.eventPublisher.emitAsync(
events.vendors.onOpeningBalanceChanging,
{
oldVendor,
openingBalanceEditDTO,
trx,
} as IVendorOpeningBalanceEditingPayload,
);
// Mutates the vendor on the storage.
const vendor = await this.vendorModel()
.query()
.patchAndFetchById(vendorId, {
...openingBalanceEditDTO,
});
// Triggers `onVendorOpeingBalanceChanged` event.
await this.eventPublisher.emitAsync(
events.vendors.onOpeningBalanceChanged,
{
vendor,
oldVendor,
openingBalanceEditDTO,
trx,
} as IVendorOpeningBalanceEditedPayload,
);
return vendor;
});
}
}