mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 14:50:32 +00:00
feat(items): filter auto-complete results.
feat(contacts): filter auto-complete results.
This commit is contained in:
@@ -1,34 +1,38 @@
|
|||||||
import { check, param, query, body, ValidationChain } from 'express-validator';
|
import { check, param, query, body, ValidationChain } from 'express-validator';
|
||||||
import { Router, Request, Response, NextFunction } from 'express';
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
import { Inject } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import BaseController from 'api/controllers/BaseController';
|
import BaseController from 'api/controllers/BaseController';
|
||||||
import ContactsService from 'services/Contacts/ContactsService';
|
import ContactsService from 'services/Contacts/ContactsService';
|
||||||
|
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||||
import { DATATYPES_LENGTH } from 'data/DataTypes';
|
import { DATATYPES_LENGTH } from 'data/DataTypes';
|
||||||
import { Service } from 'typedi';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class ContactsController extends BaseController {
|
export default class ContactsController extends BaseController {
|
||||||
@Inject()
|
@Inject()
|
||||||
contactsService: ContactsService;
|
contactsService: ContactsService;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
dynamicListService: DynamicListingService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Express router.
|
* Express router.
|
||||||
*/
|
*/
|
||||||
router() {
|
router() {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
'/auto-complete',
|
||||||
|
[...this.autocompleteQuerySchema],
|
||||||
|
this.validationResult,
|
||||||
|
this.asyncMiddleware(this.autocompleteContacts.bind(this)),
|
||||||
|
this.dynamicListService.handlerErrorsToResponse
|
||||||
|
);
|
||||||
router.get(
|
router.get(
|
||||||
'/:id',
|
'/:id',
|
||||||
[param('id').exists().isNumeric().toInt()],
|
[param('id').exists().isNumeric().toInt()],
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
this.asyncMiddleware(this.getContact.bind(this))
|
this.asyncMiddleware(this.getContact.bind(this))
|
||||||
);
|
);
|
||||||
router.get(
|
|
||||||
'/auto-complete',
|
|
||||||
[...this.autocompleteQuerySchema],
|
|
||||||
this.validationResult,
|
|
||||||
this.asyncMiddleware(this.autocompleteContacts.bind(this))
|
|
||||||
);
|
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,11 +83,13 @@ export default class ContactsController extends BaseController {
|
|||||||
const filter = {
|
const filter = {
|
||||||
filterRoles: [],
|
filterRoles: [],
|
||||||
sortOrder: 'asc',
|
sortOrder: 'asc',
|
||||||
columnSortBy: 'created_at',
|
columnSortBy: 'display_name',
|
||||||
limit: 10,
|
limit: 10,
|
||||||
...this.matchedQueryData(req),
|
...this.matchedQueryData(req),
|
||||||
};
|
};
|
||||||
|
if (filter.stringifiedFilterRoles) {
|
||||||
|
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const contacts = await this.contactsService.autocompleteContacts(
|
const contacts = await this.contactsService.autocompleteContacts(
|
||||||
tenantId,
|
tenantId,
|
||||||
|
|||||||
@@ -216,38 +216,11 @@ export default class ItemsController extends BaseController {
|
|||||||
|
|
||||||
query('stringified_filter_roles').optional().isJSON(),
|
query('stringified_filter_roles').optional().isJSON(),
|
||||||
query('limit').optional().isNumeric().toInt(),
|
query('limit').optional().isNumeric().toInt(),
|
||||||
|
|
||||||
|
query('keyword').optional().isString().trim().escape(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Auto-complete list.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
async autocompleteList(req: Request, res: Response, next: NextFunction) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const filter = {
|
|
||||||
filterRoles: [],
|
|
||||||
sortOrder: 'asc',
|
|
||||||
columnSortBy: 'created_at',
|
|
||||||
limit: 10,
|
|
||||||
...this.matchedQueryData(req),
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const items = await this.itemsService.autocompleteItems(
|
|
||||||
tenantId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
items,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores the given item details to the storage.
|
* Stores the given item details to the storage.
|
||||||
* @param {Request} req
|
* @param {Request} req
|
||||||
@@ -410,6 +383,38 @@ export default class ItemsController extends BaseController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-complete list.
|
||||||
|
* @param {Request} req
|
||||||
|
* @param {Response} res
|
||||||
|
* @param {NextFunction} next
|
||||||
|
*/
|
||||||
|
async autocompleteList(req: Request, res: Response, next: NextFunction) {
|
||||||
|
const { tenantId } = req;
|
||||||
|
const filter = {
|
||||||
|
filterRoles: [],
|
||||||
|
sortOrder: 'asc',
|
||||||
|
columnSortBy: 'name',
|
||||||
|
limit: 10,
|
||||||
|
keyword: '',
|
||||||
|
...this.matchedQueryData(req),
|
||||||
|
};
|
||||||
|
if (filter.stringifiedFilterRoles) {
|
||||||
|
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const items = await this.itemsService.autocompleteItems(
|
||||||
|
tenantId,
|
||||||
|
filter
|
||||||
|
);
|
||||||
|
return res.status(200).send({
|
||||||
|
items,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes items in bulk.
|
* Deletes items in bulk.
|
||||||
* @param {Request} req
|
* @param {Request} req
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
|
|
||||||
// Contact Interfaces.
|
import { IFilterRole } from "./DynamicFilter";
|
||||||
|
|
||||||
import { IDynamicListFilter } from "./DynamicFilter";
|
|
||||||
|
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
export interface IContactAddress {
|
export interface IContactAddress {
|
||||||
@@ -208,6 +206,9 @@ export interface ICustomersFilter extends IDynamicListFilter {
|
|||||||
export interface IContactsAutoCompleteFilter {
|
export interface IContactsAutoCompleteFilter {
|
||||||
limit: number,
|
limit: number,
|
||||||
keyword: string,
|
keyword: string,
|
||||||
|
filterRoles?: IFilterRole[];
|
||||||
|
columnSortBy: string;
|
||||||
|
sortOrder: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IContactAutoCompleteItem {
|
export interface IContactAutoCompleteItem {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IDynamicListFilter } from 'interfaces/DynamicFilter';
|
import { IFilterRole } from 'interfaces/DynamicFilter';
|
||||||
|
|
||||||
export interface IItem{
|
export interface IItem{
|
||||||
id: number,
|
id: number,
|
||||||
@@ -72,7 +72,7 @@ export interface IItemsService {
|
|||||||
itemsList(tenantId: number, itemsFilter: IItemsFilter): Promise<{items: IItem[]}>;
|
itemsList(tenantId: number, itemsFilter: IItemsFilter): Promise<{items: IItem[]}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IItemsFilter extends IDynamicListFilter {
|
export interface IItemsFilter extends IDynamicListFilterDTO {
|
||||||
stringifiedFilterRoles?: string,
|
stringifiedFilterRoles?: string,
|
||||||
page: number,
|
page: number,
|
||||||
pageSize: number,
|
pageSize: number,
|
||||||
@@ -81,5 +81,7 @@ export interface IItemsFilter extends IDynamicListFilter {
|
|||||||
export interface IItemsAutoCompleteFilter {
|
export interface IItemsAutoCompleteFilter {
|
||||||
limit: number,
|
limit: number,
|
||||||
keyword: string,
|
keyword: string,
|
||||||
|
filterRoles?: IFilterRole[];
|
||||||
|
columnSortBy: string;
|
||||||
|
sortOrder: string;
|
||||||
}
|
}
|
||||||
@@ -97,6 +97,12 @@ export default class Contact extends TenantModel {
|
|||||||
|
|
||||||
static get fields() {
|
static get fields() {
|
||||||
return {
|
return {
|
||||||
|
contact_service: {
|
||||||
|
column: 'contact_service',
|
||||||
|
},
|
||||||
|
display_name: {
|
||||||
|
column: 'display_name',
|
||||||
|
},
|
||||||
created_at: {
|
created_at: {
|
||||||
column: 'created_at',
|
column: 'created_at',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ export default class ContactsService {
|
|||||||
// Retrieve contacts list by the given query.
|
// Retrieve contacts list by the given query.
|
||||||
const contacts = await Contact.query().onBuild((builder) => {
|
const contacts = await Contact.query().onBuild((builder) => {
|
||||||
if (contactsFilter.keyword) {
|
if (contactsFilter.keyword) {
|
||||||
builder.where('display_name', 'LIKE', contactsFilter.keyword);
|
builder.where('display_name', 'LIKE', `%${contactsFilter.keyword}%`);
|
||||||
}
|
}
|
||||||
dynamicList.buildQuery()(builder);
|
dynamicList.buildQuery()(builder);
|
||||||
builder.limit(contactsFilter.limit);
|
builder.limit(contactsFilter.limit);
|
||||||
|
|||||||
@@ -523,14 +523,14 @@ export default class ItemsService implements IItemsService {
|
|||||||
|
|
||||||
dynamicFilter.buildQuery()(builder);
|
dynamicFilter.buildQuery()(builder);
|
||||||
builder.limit(itemsFilter.limit);
|
builder.limit(itemsFilter.limit);
|
||||||
});
|
|
||||||
|
|
||||||
// const autocompleteItems = this.transformAutoCompleteItems(items);
|
if (itemsFilter.keyword) {
|
||||||
|
builder.where('name', 'LIKE', `%${itemsFilter.keyword}%`);
|
||||||
|
}
|
||||||
|
});
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
// transformAutoCompleteItems(item)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the given item or items have no associated invoices or bills.
|
* Validates the given item or items have no associated invoices or bills.
|
||||||
* @param {number} tenantId - Tenant id.
|
* @param {number} tenantId - Tenant id.
|
||||||
|
|||||||
Reference in New Issue
Block a user