mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
67
packages/server/src/modules/Items/GetItems.service.ts
Normal file
67
packages/server/src/modules/Items/GetItems.service.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import * as R from 'ramda';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
import { DynamicListService } from '../DynamicListing/DynamicList.service';
|
||||
import { Item } from './models/Item';
|
||||
import { IItemsFilter } from './types/Items.types';
|
||||
import { ItemTransformer } from './Item.transformer';
|
||||
import { TenantModelProxy } from '../System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class GetItemsService {
|
||||
constructor(
|
||||
private readonly dynamicListService: DynamicListService,
|
||||
private readonly transformer: TransformerInjectable,
|
||||
@Inject(Item.name)
|
||||
private readonly itemModel: TenantModelProxy<typeof Item>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Parses items list filter DTO.
|
||||
* @param {} filterDTO - Filter DTO.
|
||||
*/
|
||||
private parseItemsListFilterDTO(filterDTO: IItemsFilter) {
|
||||
return R.compose(
|
||||
this.dynamicListService.parseStringifiedFilter<IItemsFilter>,
|
||||
)(filterDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves items datatable list.
|
||||
* @param {IItemsFilter} itemsFilter - Items filter.
|
||||
*/
|
||||
public async getItems(filterDTO: IItemsFilter) {
|
||||
// Parses items list filter DTO.
|
||||
const filter = this.parseItemsListFilterDTO(filterDTO);
|
||||
|
||||
// Dynamic list service.
|
||||
const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
Item,
|
||||
filter,
|
||||
);
|
||||
const { results: items, pagination } = await this.itemModel()
|
||||
.query()
|
||||
.onBuild((builder) => {
|
||||
builder.modify('inactiveMode', filter.inactiveMode);
|
||||
|
||||
builder.withGraphFetched('inventoryAccount');
|
||||
builder.withGraphFetched('sellAccount');
|
||||
builder.withGraphFetched('costAccount');
|
||||
builder.withGraphFetched('category');
|
||||
|
||||
dynamicFilter.buildQuery()(builder);
|
||||
})
|
||||
.pagination(filter.page - 1, filter.pageSize);
|
||||
|
||||
// Retrieves the transformed items.
|
||||
const transformedItems = await this.transformer.transform(
|
||||
items,
|
||||
new ItemTransformer(),
|
||||
);
|
||||
return {
|
||||
items: transformedItems,
|
||||
pagination,
|
||||
filterMeta: dynamicFilter.getResponseMeta(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user