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,67 @@
import { Injectable, Inject } from '@nestjs/common';
import { Knex } from 'knex';
import { EventEmitter2 } from '@nestjs/event-emitter';
import {
IItemEventDeletedPayload,
IItemEventDeletingPayload,
} from '@/interfaces/Item';
import { events } from '@/common/events/events';
import { Item } from './models/Item';
import { ERRORS } from './Items.constants';
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
import { TenantModelProxy } from '../System/models/TenantBaseModel';
@Injectable()
export class DeleteItemService {
/**
* Constructor for the class.
* @param {EventEmitter2} eventEmitter - Event emitter for publishing item deletion events.
* @param {UnitOfWork} uow - Unit of Work for tenant database transactions.
* @param {typeof Item} itemModel - The Item model class for database operations.
*/
constructor(
private readonly eventEmitter: EventEmitter2,
private readonly uow: UnitOfWork,
@Inject(Item.name)
private readonly itemModel: TenantModelProxy<typeof Item>,
) {}
/**
* Delete the given item from the storage.
* @param {number} itemId - Item id.
* @return {Promise<void>}
*/
public async deleteItem(
itemId: number,
trx?: Knex.Transaction,
): Promise<void> {
// Retrieve the given item or throw not found service error.
const oldItem = await this.itemModel()
.query()
.findById(itemId)
.throwIfNotFound();
// .queryAndThrowIfHasRelations({
// type: ERRORS.ITEM_HAS_ASSOCIATED_TRANSACTIONS,
// });
// Delete item in unit of work.
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Triggers `onItemDeleting` event.
await this.eventEmitter.emitAsync(events.item.onDeleting, {
trx,
oldItem,
} as IItemEventDeletingPayload);
// Deletes the item.
await this.itemModel().query(trx).findById(itemId).delete();
// Triggers `onItemDeleted` event.
await this.eventEmitter.emitAsync(events.item.onDeleted, {
itemId,
oldItem,
trx,
} as IItemEventDeletedPayload);
}, trx);
}
}