mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: listing vendors and customers.
feat: items service events.
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
import { Request, Response, Router, NextFunction } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { check } from 'express-validator';
|
||||
import { check, query } from 'express-validator';
|
||||
import ContactsController from 'api/controllers/Contacts/Contacts';
|
||||
import CustomersService from 'services/Contacts/CustomersService';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import { ICustomerNewDTO, ICustomerEditDTO } from 'interfaces';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
|
||||
@Service()
|
||||
export default class CustomersController extends ContactsController {
|
||||
@Inject()
|
||||
customersService: CustomersService;
|
||||
|
||||
@Inject()
|
||||
dynamicListService: DynamicListingService;
|
||||
|
||||
/**
|
||||
* Express router.
|
||||
*/
|
||||
@@ -51,10 +55,11 @@ export default class CustomersController extends ContactsController {
|
||||
this.handlerServiceErrors,
|
||||
);
|
||||
router.get('/', [
|
||||
|
||||
...this.validateListQuerySchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getCustomersList.bind(this))
|
||||
asyncMiddleware(this.getCustomersList.bind(this)),
|
||||
this.dynamicListService.handlerErrorsToResponse,
|
||||
);
|
||||
router.get('/:id', [
|
||||
...this.specificContactSchema,
|
||||
@@ -76,6 +81,19 @@ export default class CustomersController extends ContactsController {
|
||||
];
|
||||
}
|
||||
|
||||
get validateListQuerySchema() {
|
||||
return [
|
||||
query('column_sort_by').optional().trim().escape(),
|
||||
query('sort_order').optional().isIn(['desc', 'asc']),
|
||||
|
||||
query('page').optional().isNumeric().toInt(),
|
||||
query('page_size').optional().isNumeric().toInt(),
|
||||
|
||||
query('custom_view_id').optional().isNumeric().toInt(),
|
||||
query('stringified_filter_roles').optional().isJSON(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new customer.
|
||||
* @param {Request} req
|
||||
@@ -167,12 +185,34 @@ export default class CustomersController extends ContactsController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve customers paginated and filterable list.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async getCustomersList(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const filter = {
|
||||
filterRoles: [],
|
||||
sortOrder: 'asc',
|
||||
columnSortBy: 'created_at',
|
||||
page: 1,
|
||||
pageSize: 12,
|
||||
...this.matchedQueryData(req),
|
||||
};
|
||||
if (filter.stringifiedFilterRoles) {
|
||||
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||
}
|
||||
|
||||
try {
|
||||
await this.customersService.getCustomersList(tenantId)
|
||||
const { customers, pagination, filterMeta } = await this.customersService.getCustomersList(tenantId, filter);
|
||||
|
||||
return res.status(200).send({
|
||||
customers,
|
||||
pagination: this.transfromToResponse(pagination),
|
||||
filter_meta: this.transfromToResponse(filterMeta),
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -196,9 +196,15 @@ export default class VendorsController extends ContactsController {
|
||||
filterRoles: [],
|
||||
...this.matchedBodyData(req),
|
||||
};
|
||||
|
||||
try {
|
||||
const vendors = await this.vendorsService.getVendorsList(tenantId, vendorsFilter);
|
||||
return res.status(200).send({ vendors });
|
||||
const { vendors, pagination, filterMeta } = await this.vendorsService.getVendorsList(tenantId, vendorsFilter);
|
||||
|
||||
return res.status(200).send({
|
||||
vendors,
|
||||
pagination: this.transfromToResponse(pagination),
|
||||
filter_meta: this.transfromToResponse(filterMeta),
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user