refactor: items services to Nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-15 13:04:41 +02:00
parent 70211980aa
commit 0a112c5655
20 changed files with 897 additions and 18 deletions

View File

@@ -0,0 +1,46 @@
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Inject, Injectable } from '@nestjs/common';
import { Item } from './models/Item';
import { events } from '@/common/events/events';
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
import { ItemTransformer } from './Item.transformer';
@Injectable()
export class GetItemService {
constructor(
@Inject(Item)
private itemModel: typeof Item,
private eventEmitter2: EventEmitter2,
private transformerInjectable: TransformerInjectable,
) {}
/**
* Retrieve the item details of the given id with associated details.
* @param {number} tenantId - The tenant id.
* @param {number} itemId - The item id.
*/
public async getItem(itemId: number): Promise<any> {
const item = await this.itemModel
.query()
.findById(itemId)
.withGraphFetched('sellAccount')
.withGraphFetched('inventoryAccount')
.withGraphFetched('category')
.withGraphFetched('costAccount')
.withGraphFetched('itemWarehouses.warehouse')
.withGraphFetched('sellTaxRate')
.withGraphFetched('purchaseTaxRate')
.throwIfNotFound();
const transformed = await this.transformerInjectable.transform(
item,
new ItemTransformer(),
);
const eventPayload = { itemId };
// Triggers the `onItemViewed` event.
await this.eventEmitter2.emitAsync(events.item.onViewed, eventPayload);
return transformed;
}
}