fix(sorting): add the missing the resources columns.

This commit is contained in:
a.bouhuolia
2021-03-06 14:11:28 +02:00
parent 32e5695950
commit b03a27aefb
9 changed files with 84 additions and 7 deletions

View File

@@ -5,12 +5,16 @@ import asyncMiddleware from 'api/middleware/asyncMiddleware';
import BaseController from './BaseController'; import BaseController from './BaseController';
import { ServiceError } from 'exceptions'; import { ServiceError } from 'exceptions';
import ExchangeRatesService from 'services/ExchangeRates/ExchangeRatesService'; import ExchangeRatesService from 'services/ExchangeRates/ExchangeRatesService';
import DynamicListingService from 'services/DynamicListing/DynamicListService';
@Service() @Service()
export default class ExchangeRatesController extends BaseController { export default class ExchangeRatesController extends BaseController {
@Inject() @Inject()
exchangeRatesService: ExchangeRatesService; exchangeRatesService: ExchangeRatesService;
@Inject()
dynamicListService: DynamicListingService;
/** /**
* Constructor method. * Constructor method.
*/ */
@@ -22,7 +26,8 @@ export default class ExchangeRatesController extends BaseController {
[...this.exchangeRatesListSchema], [...this.exchangeRatesListSchema],
this.validationResult, this.validationResult,
asyncMiddleware(this.exchangeRates.bind(this)), asyncMiddleware(this.exchangeRates.bind(this)),
this.handleServiceError this.dynamicListService.handlerErrorsToResponse,
this.handleServiceError,
); );
router.post( router.post(
'/', '/',
@@ -59,6 +64,9 @@ export default class ExchangeRatesController extends BaseController {
return [ return [
query('page').optional().isNumeric().toInt(), query('page').optional().isNumeric().toInt(),
query('page_size').optional().isNumeric().toInt(), query('page_size').optional().isNumeric().toInt(),
query('column_sort_by').optional(),
query('sort_order').optional().isIn(['desc', 'asc']),
]; ];
} }
@@ -96,8 +104,14 @@ export default class ExchangeRatesController extends BaseController {
const filter = { const filter = {
page: 1, page: 1,
pageSize: 100, pageSize: 100,
...req.query, filterRoles: [],
columnSortBy: 'created_at',
sortOrder: 'asc',
...this.matchedQueryData(req),
}; };
if (filter.stringifiedFilterRoles) {
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
}
try { try {
const exchangeRates = await this.exchangeRatesService.listExchangeRates( const exchangeRates = await this.exchangeRatesService.listExchangeRates(
tenantId, tenantId,

View File

@@ -1,3 +1,4 @@
import { IFilterRole } from './DynamicFilter';
export interface IExchangeRate { export interface IExchangeRate {
id: number, id: number,
@@ -21,6 +22,9 @@ export interface IExchangeRateEditDTO {
export interface IExchangeRateFilter { export interface IExchangeRateFilter {
page: number, page: number,
pageSize: number, pageSize: number,
filterRoles?: IFilterRole[];
columnSortBy: string;
sortOrder: string;
}; };
export interface IExchangeRatesService { export interface IExchangeRatesService {

View File

@@ -1,4 +1,5 @@
import { ISystemUser } from './User'; import { ISystemUser } from './User';
import { IFilterRole } from './DynamicFilter';
export interface IPaginationMeta { export interface IPaginationMeta {
total: number; total: number;
@@ -9,6 +10,9 @@ export interface IPaginationMeta {
export interface IExpensesFilter { export interface IExpensesFilter {
page: number; page: number;
pageSize: number; pageSize: number;
filterRoles?: IFilterRole[];
columnSortBy: string;
sortOrder: string;
} }
export interface IExpense { export interface IExpense {

View File

@@ -16,4 +16,29 @@ export default class ExchangeRate extends TenantModel {
get timestamps() { get timestamps() {
return ['createdAt', 'updatedAt']; return ['createdAt', 'updatedAt'];
} }
/**
* Model defined fields.
*/
static get fields(){
return {
currency_code: {
label: 'Currency',
column: 'currency_code'
},
exchange_rate: {
label: 'Exchange rate',
column: 'exchange_rate',
},
date: {
label: 'Date',
column: 'date',
},
created_at: {
label: "Created at",
column: "created_at",
columnType: "date",
},
}
}
} }

View File

@@ -163,7 +163,7 @@ export default class Expense extends TenantModel {
}, },
published: { published: {
label: "Published", label: "Published",
column: "published", column: "published_at",
}, },
created_at: { created_at: {
label: "Created at", label: "Created at",

View File

@@ -125,6 +125,12 @@ export default class SaleEstimate extends TenantModel {
approved(query) { approved(query) {
query.whereNot('approved_at', null) query.whereNot('approved_at', null)
}, },
/**
* Sorting the estimates orders by delivery status.
*/
orderByDraft(query, order) {
query.orderByRaw(`delivered_at is null ${order}`)
}
}; };
} }
@@ -238,6 +244,9 @@ export default class SaleEstimate extends TenantModel {
query.modify('expired'); break; query.modify('expired'); break;
} }
}, },
sortQuery: (query, role) => {
query.modify('orderByDraft', role.order);
}
}, },
created_at: { created_at: {
label: 'Created at', label: 'Created at',

View File

@@ -60,6 +60,13 @@ export default class SaleReceipt extends TenantModel {
draft(query) { draft(query) {
query.where('closed_at', null); query.where('closed_at', null);
}, },
/**
* Sorting the receipts order by status.
*/
sortByStatus(query, order) {
query.orderByRaw(`CLOSED_AT IS NULL ${order}`);
}
}; };
} }
@@ -196,6 +203,9 @@ export default class SaleReceipt extends TenantModel {
break; break;
} }
}, },
sortQuery(query, role) {
query.modify('sortByStatus', role.order);
}
} }
}; };
} }

View File

@@ -2,6 +2,7 @@ import moment from 'moment';
import { difference } from 'lodash'; import { difference } from 'lodash';
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import { ServiceError } from 'exceptions'; import { ServiceError } from 'exceptions';
import DynamicListingService from 'services/DynamicListing/DynamicListService';
import { import {
EventDispatcher, EventDispatcher,
EventDispatcherInterface, EventDispatcherInterface,
@@ -32,6 +33,9 @@ export default class ExchangeRatesService implements IExchangeRatesService {
@Inject() @Inject()
tenancy: TenancyService; tenancy: TenancyService;
@Inject()
dynamicListService: DynamicListingService;
/** /**
* Creates a new exchange rate. * Creates a new exchange rate.
* @param {number} tenantId * @param {number} tenantId
@@ -116,10 +120,17 @@ export default class ExchangeRatesService implements IExchangeRatesService {
exchangeRateFilter: IExchangeRateFilter exchangeRateFilter: IExchangeRateFilter
): Promise<void> { ): Promise<void> {
const { ExchangeRate } = this.tenancy.models(tenantId); const { ExchangeRate } = this.tenancy.models(tenantId);
const exchangeRates = await ExchangeRate.query().pagination( const dynamicFilter = await this.dynamicListService.dynamicList(
exchangeRateFilter.page - 1, tenantId,
exchangeRateFilter.pageSize ExchangeRate,
exchangeRateFilter
); );
// Retrieve exchange rates by the given query.
const exchangeRates = await ExchangeRate.query()
.onBuild((query) => {
dynamicFilter.buildQuery()(query);
})
.pagination(exchangeRateFilter.page - 1, exchangeRateFilter.pageSize);
return exchangeRates; return exchangeRates;
} }