Optimized Billing
MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -271,10 +271,8 @@ export default class BillingService {
}
// Get taxes
const taxes = await billingImpl.getTaxes();
- // Filter
- const filteredTaxes = BillingSecurity.filterTaxesResponse(taxes, req.user);
// Return
- res.json(filteredTaxes);
+ res.json(taxes);
next();
}
@@ -290,6 +288,11 @@ export default class BillingService {
module: MODULE_NAME, method: 'handleGetInvoices',
});
}
+ // Check Users
+ let userProject: string[] = [];
+ if (Authorizations.canListUsers(req.user)) {
+ userProject = [ 'userID', 'user.id', 'user.name', 'user.firstName', 'user.email' ];
+ }
// Filter
const filteredRequest = BillingSecurity.filterGetUserInvoicesRequest(req.query);
// Get invoices
@@ -306,9 +309,11 @@ export default class BillingService {
skip: filteredRequest.Skip,
sort: filteredRequest.Sort,
onlyRecordCount: filteredRequest.OnlyRecordCount
- });
- // Filter
- BillingSecurity.filterInvoicesResponse(invoices, req.user);
+ },
+ [
+ 'id', 'number', 'status', 'amount', 'createdOn', 'nbrOfItems', 'currency', 'downloadable',
+ ...userProject
+ ]);
// Return
res.json(invoices);
next();

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -640,7 +640,7 @@ export default class TransactionService {
// Check Users
let userProject: string[] = [];
if (Authorizations.canListUsers(req.user)) {
- userProject = ['userID', 'user.id', 'user.name', 'user.firstName', 'user.email',];
+ userProject = [ 'userID', 'user.id', 'user.name', 'user.firstName', 'user.email' ];
}
const filter: any = { stop: { $exists: true } };
// Filter

MODIFY
src/server/rest/v1/service/security/BillingSecurity.ts
src/server/rest/v1/service/security/BillingSecurity.ts
-import { BillingInvoice, BillingTax } from '../../../../../types/Billing';
import { HttpCreateTransactionInvoiceRequest, HttpForceSynchronizeUserInvoicesRequest, HttpSynchronizeUserRequest } from '../../../../../types/requests/HttpUserRequest';
-import Authorizations from '../../../../../authorization/Authorizations';
-import { DataResult } from '../../../../../types/DataResult';
import { HttpBillingInvoiceRequest } from '../../../../../types/requests/HttpBillingRequest';
import HttpByIDRequest from '../../../../../types/requests/HttpByIDRequest';
-import UserSecurity from './UserSecurity';
-import UserToken from '../../../../../types/UserToken';
import UtilsSecurity from './UtilsSecurity';
import sanitize from 'mongo-sanitize';
export default class BillingSecurity {
- static filterTaxesResponse(taxes: BillingTax[], loggedUser: UserToken): BillingTax[] {
- const filteredTaxes: BillingTax[] = [];
- if (!taxes) {
- return null;
- }
- for (const tax of taxes) {
- // Filter
- const filteredTax = BillingSecurity.filterTaxResponse(tax, loggedUser);
- if (filteredTax) {
- filteredTaxes.push(filteredTax);
- }
- }
- return filteredTaxes;
- }
-
- static filterTaxResponse(tax: BillingTax, loggedUser: UserToken): BillingTax {
- const filteredTax = {} as BillingTax;
- if (!tax) {
- return null;
- }
- // Check auth
- if (Authorizations.canReadTaxesBilling(loggedUser)) {
- // Set only necessary info
- filteredTax.id = tax.id;
- filteredTax.description = tax.description;
- filteredTax.displayName = tax.displayName;
- filteredTax.percentage = tax.percentage;
- }
- return filteredTax;
- }
-
- static filterInvoicesResponse(invoices: DataResult<BillingInvoice>, loggedUser: UserToken): void {
- const filteredInvoices: BillingInvoice[] = [];
- if (!invoices) {
- return null;
- }
- for (const invoice of invoices.result) {
- // Filter
- const filteredInvoice = BillingSecurity.filterInvoiceResponse(invoice, loggedUser);
- if (filteredInvoices) {
- filteredInvoices.push(filteredInvoice);
- }
- }
- invoices.result = filteredInvoices;
- }
-
- static filterInvoiceResponse(invoice: BillingInvoice, loggedUser: UserToken): BillingInvoice {
- const filteredInvoice = {} as BillingInvoice;
- if (!invoice) {
- return null;
- }
- // Check auth
- if (Authorizations.canListInvoicesBilling(loggedUser)) {
- // Set only necessary info
- filteredInvoice.id = invoice.id;
- filteredInvoice.userID = invoice.userID;
- filteredInvoice.number = invoice.number;
- filteredInvoice.status = invoice.status;
- filteredInvoice.amount = invoice.amount;
- filteredInvoice.createdOn = invoice.createdOn;
- filteredInvoice.nbrOfItems = invoice.nbrOfItems;
- filteredInvoice.currency = invoice.currency;
- filteredInvoice.downloadable = invoice.downloadable;
- filteredInvoice.user = UserSecurity.filterMinimalUserResponse(invoice.user, loggedUser);
- }
- return filteredInvoice;
- }
-
static filterSynchronizeUserRequest(request: any): HttpSynchronizeUserRequest {
const filteredUser: HttpSynchronizeUserRequest = {} as HttpSynchronizeUserRequest;
if (request.id) {

Optimized Billing