Refactor Company canCreate.
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -6,7 +6,7 @@ import User, { UserRole } from '../../../../types/User';
import AppAuthError from '../../../../exception/AppAuthError';
import Authorizations from '../../../../authorization/Authorizations';
-import Company from '../../../../types/Company';
+import { CompanyDataResult } from '../../../../types/DataResult';
import Constants from '../../../../utils/Constants';
import { HTTPAuthError } from '../../../../types/HTTPError';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
@@ -380,15 +380,17 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async addCompaniesAuthorizations(tenant: Tenant, userToken: UserToken, companies: Company[]): Promise<void> {
- // Get Site Admins - needed?
+ public static async addCompaniesAuthorizations(tenant: Tenant, userToken: UserToken, companies: CompanyDataResult): Promise<void> {
+ // Get Site Admins
const { siteAdminIDs, siteOwnerIDs } = await AuthorizationService.getSiteAdminOwnerIDs(tenant, userToken);
// Set to user
userToken.sitesAdmin = siteAdminIDs;
userToken.sitesOwner = siteOwnerIDs;
+ // Add canCreate flag to root
+ companies.canCreate = Authorizations.canCreateCompany(userToken);
const assignedCompanies = await AuthorizationService.getAssignedSitesCompanyIDs(tenant.id, userToken);
// Enrich
- for (const company of companies) {
+ for (const company of companies.result) {
if (userToken.role === UserRole.ADMIN) {
company.canRead = true;
company.canUpdate = true;

MODIFY
src/server/rest/v1/service/CompanyService.ts
src/server/rest/v1/service/CompanyService.ts
@@ -7,6 +7,7 @@ import AppError from '../../../../exception/AppError';
import AuthorizationService from './AuthorizationService';
import Authorizations from '../../../../authorization/Authorizations';
import Company from '../../../../types/Company';
+import { CompanyDataResult } from '../../../../types/DataResult';
import CompanySecurity from './security/CompanySecurity';
import CompanyStorage from '../../../../storage/mongodb/CompanyStorage';
import Constants from '../../../../utils/Constants';
@@ -169,8 +170,7 @@ export default class CompanyService {
authorizationCompaniesFilter.projectFields
);
// Add Auth flags
- await AuthorizationService.addCompaniesAuthorizations(req.tenant, req.user, companies.result);
- companies.canCreate = Authorizations.canCreateCompany(req.user);
+ await AuthorizationService.addCompaniesAuthorizations(req.tenant, req.user, companies as CompanyDataResult);
// Return
res.json(companies);
next();

MODIFY
src/types/DataResult.ts
src/types/DataResult.ts
+import Company from './Company';
import { Transaction } from '@google-cloud/firestore';
export interface DataResult<T> {
count: number;
- canCreate?: boolean;
result: T[];
}
+export interface CompanyDataResult extends DataResult<Company>{
+ canCreate: boolean;
+}
+
export interface TransactionDataResult {
count: number;
result: Transaction[];

Refactor Company canCreate.