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,38 @@
import { ContactTransfromer } from "../../Contacts/Contact.transformer";
export class CustomerTransfromer extends ContactTransfromer {
/**
* Include these attributes to expense object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return [
'formattedBalance',
'formattedOpeningBalance',
'formattedOpeningBalanceAt',
'customerType',
'formattedCustomerType',
];
};
/**
* Retrieve customer type.
* @returns {string}
*/
protected customerType = (customer): string => {
return customer.contactType;
};
/**
* Retrieve the formatted customer type.
* @param customer
* @returns {string}
*/
protected formattedCustomerType = (customer): string => {
const keywords = {
individual: 'customer.type.individual',
business: 'customer.type.business',
};
return this.context.i18n.t(keywords[customer.contactType] || '');
};
}

View File

@@ -0,0 +1,30 @@
import { Inject, Injectable } from '@nestjs/common';
import { CustomerTransfromer } from './CustomerTransformer';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
import { Customer } from '../models/Customer';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class GetCustomerService {
constructor(
private transformer: TransformerInjectable,
@Inject(Customer.name)
private customerModel: TenantModelProxy<typeof Customer>,
) {}
/**
* Retrieve the given customer details.
* @param {number} customerId
*/
public async getCustomer(customerId: number) {
// Retrieve the customer model or throw not found error.
const customer = await this.customerModel()
.query()
.findById(customerId)
.throwIfNotFound();
// Retrieves the transformered customers.
return this.transformer.transform(customer, new CustomerTransfromer());
}
}

View File

@@ -0,0 +1,65 @@
import { DynamicListService } from '@/modules/DynamicListing/DynamicList.service';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
import { Inject, Injectable } from '@nestjs/common';
import * as R from 'ramda';
import { Customer } from '../models/Customer';
import { CustomerTransfromer } from './CustomerTransformer';
import {
GetCustomersResponse,
ICustomersFilter,
} from '../types/Customers.types';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class GetCustomers {
constructor(
private dynamicListService: DynamicListService,
private transformer: TransformerInjectable,
@Inject(Customer.name)
private customerModel: TenantModelProxy<typeof Customer>,
) {}
/**
* Parses customers list filter DTO.
* @param filterDTO -
*/
private parseCustomersListFilterDTO(filterDTO) {
return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
}
/**
* Retrieve customers paginated list.
* @param {ICustomersFilter} filter - Cusotmers filter.
* @returns {Promise<GetCustomersResponse>}
*/
public async getCustomersList(
filterDTO: ICustomersFilter,
): Promise<GetCustomersResponse> {
// Parses customers list filter DTO.
const filter = this.parseCustomersListFilterDTO(filterDTO);
const dynamicList = await this.dynamicListService.dynamicList(
this.customerModel(),
filter,
);
const { results, pagination } = await this.customerModel()
.query()
.onBuild((builder) => {
dynamicList.buildQuery()(builder);
builder.modify('inactiveMode', filter.inactiveMode);
})
.pagination(filter.page - 1, filter.pageSize);
// Retrieves the transformed customers.
const customers = await this.transformer.transform(
results,
new CustomerTransfromer(),
);
return {
customers,
pagination,
filterMeta: dynamicList.getResponseMeta(),
};
}
}