mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { SaleEstimateTransfromer } from './SaleEstimate.transformer';
|
||||
import { SaleEstimateValidators } from '../commands/SaleEstimateValidators.service';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { SaleEstimate } from '../models/SaleEstimate';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class GetSaleEstimate {
|
||||
constructor(
|
||||
@Inject(SaleEstimate.name)
|
||||
private readonly saleEstimateModel: TenantModelProxy<typeof SaleEstimate>,
|
||||
|
||||
private readonly transformer: TransformerInjectable,
|
||||
private readonly validators: SaleEstimateValidators,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the estimate details with associated entries.
|
||||
* @async
|
||||
* @param {Integer} estimateId
|
||||
*/
|
||||
public async getEstimate(estimateId: number) {
|
||||
const estimate = await this.saleEstimateModel()
|
||||
.query()
|
||||
.findById(estimateId)
|
||||
.withGraphFetched('entries.item')
|
||||
.withGraphFetched('customer')
|
||||
.withGraphFetched('branch')
|
||||
.withGraphFetched('attachments');
|
||||
|
||||
// Validates the estimate existance.
|
||||
this.validators.validateEstimateExistance(estimate);
|
||||
|
||||
// Transformes sale estimate model to POJO.
|
||||
const transformed = await this.transformer.transform(
|
||||
estimate,
|
||||
new SaleEstimateTransfromer(),
|
||||
);
|
||||
const eventPayload = { saleEstimateId: estimateId };
|
||||
|
||||
// Triggers `onSaleEstimateViewed` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.saleEstimate.onViewed,
|
||||
eventPayload,
|
||||
);
|
||||
return transformed;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user