refactor(subaccounts: renamed billing object in company/site
ADD
src/assets/schemas/billing/billing-subaccount.json
+{
+ "$id": "billing-subaccount",
+ "definitions": {
+ "accountID": {
+ "$ref": "common#/definitions/id"
+ }
+ }
+}

DELETE
src/assets/schemas/billing/billing.json
-{
- "$id": "billing",
- "definitions": {
- "subAccount": {
- "type": "object",
- "properties": {
- "id": {
- "$ref": "common#/definitions/id"
- }
- }
- }
- }
-}

MODIFY
src/assets/server/rest/v1/schemas/company/company-create.json
src/assets/server/rest/v1/schemas/company/company-create.json
"logo": {
"$ref": "common#/definitions/logo"
},
- "billing": {
- "$ref": "billing#/definitions/subAccount"
+ "billingData": {
+ "type": "object",
+ "properties": {
+ "accountID": {
+ "$ref": "billing-subaccount#/definitions/accountID"
+ }
+ }
}
},
"required": [

MODIFY
src/assets/server/rest/v1/schemas/company/company-update.json
src/assets/server/rest/v1/schemas/company/company-update.json
"logo": {
"$ref": "common#/definitions/logo"
},
- "billing": {
- "$ref": "billing#/definitions/subAccount"
+ "billingData": {
+ "type": "object",
+ "properties": {
+ "accountID": {
+ "$ref": "billing-subaccount#/definitions/accountID"
+ }
+ }
}
},
"required": [

MODIFY
src/assets/server/rest/v1/schemas/site/site-create.json
src/assets/server/rest/v1/schemas/site/site-create.json
"image": {
"$ref": "site#/definitions/image"
},
- "billing": {
- "$ref": "billing#/definitions/subAccount"
+ "billingData": {
+ "type": "object",
+ "properties": {
+ "accountID": {
+ "$ref": "billing-subaccount#/definitions/accountID"
+ }
+ }
}
},
"required": [

MODIFY
src/assets/server/rest/v1/schemas/site/site-update.json
src/assets/server/rest/v1/schemas/site/site-update.json
"image": {
"$ref": "site#/definitions/image"
},
- "billing": {
- "$ref": "billing#/definitions/subAccount"
+ "billingData": {
+ "type": "object",
+ "properties": {
+ "accountID": {
+ "$ref": "billing-subaccount#/definitions/accountID"
+ }
+ }
}
},
"required": [

MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -175,7 +175,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
'id', 'name', 'address.address1', 'address.address2', 'address.postalCode', 'address.city',
'address.department', 'address.region', 'address.country',
'address.coordinates', 'logo', 'issuer', 'distanceMeters', 'createdOn', 'lastChangedOn',
- 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName', 'billing.id'
+ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName', 'billingData.accountID'
]
},
{
@@ -218,7 +218,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
attributes: [
'id', 'name', 'issuer', 'logo',
'address.address1', 'address.address2', 'address.postalCode', 'address.city',
- 'address.department', 'address.region', 'address.country', 'address.coordinates', 'billing.id'
+ 'address.department', 'address.region', 'address.country', 'address.coordinates', 'billingData.accountID'
]
},
{
@@ -241,7 +241,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
'address.department', 'address.region', 'address.country',
'address.coordinates', 'companyID', 'company.name', 'autoUserSiteAssignment', 'issuer',
'autoUserSiteAssignment', 'distanceMeters', 'public', 'createdOn', 'lastChangedOn',
- 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName', 'connectorStats', 'billing.id'
+ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName', 'connectorStats', 'billingData.accountID'
]
},
{
@@ -250,7 +250,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
'id', 'name', 'companyID', 'company.name', 'autoUserSiteAssignment', 'issuer',
'autoUserSiteAssignment', 'distanceMeters', 'public', 'createdOn', 'lastChangedOn', 'tariffID',
'address.address1', 'address.address2', 'address.postalCode', 'address.city',
- 'address.department', 'address.region', 'address.country', 'address.coordinates', 'billing.id'
+ 'address.department', 'address.region', 'address.country', 'address.coordinates', 'billingData.accountID'
]
},
{

MODIFY
src/server/rest/v1/service/CompanyService.ts
src/server/rest/v1/service/CompanyService.ts
@@ -147,12 +147,12 @@ export default class CompanyService {
createdOn: new Date()
} as Company;
// If the company is assigned to a billing sub-account, check if the billing is active
- if (filteredRequest.billing) {
+ if (filteredRequest.billingData) {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.BILLING_PLATFORM,
Action.CREATE, Entity.COMPANY, MODULE_NAME, 'handleCreateCompany');
- const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billing.id);
- UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billing.id}' does not exist`, MODULE_NAME, 'handleCreateCompany', req.user);
- newCompany.billing = { id: billingSubAccount.id };
+ const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billingData.accountID);
+ UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billingData.accountID}' does not exist`, MODULE_NAME, 'handleCreateCompany', req.user);
+ newCompany.billingData = { accountID: billingSubAccount.id };
}
// Save
newCompany.id = await CompanyStorage.saveCompany(req.tenant, newCompany);
@@ -185,12 +185,12 @@ export default class CompanyService {
company.lastChangedBy = { 'id': req.user.id };
company.lastChangedOn = new Date();
// If the company is assigned to a billing sub-account, check if the billing is active
- if (filteredRequest.billing) {
+ if (filteredRequest.billingData.accountID) {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.BILLING_PLATFORM,
Action.CREATE, Entity.COMPANY, MODULE_NAME, 'handleUpdateCompany');
- const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billing.id);
- UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billing.id}' does not exist`, MODULE_NAME, 'handleUpdateCompany', req.user);
- company.billing = { id: billingSubAccount.id };
+ const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billingData.accountID);
+ UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billingData.accountID}' does not exist`, MODULE_NAME, 'handleUpdateCompany', req.user);
+ company.billingData = { accountID: billingSubAccount.id };
}
// Update Company
await CompanyStorage.saveCompany(req.tenant, company, Utils.objectHasProperty(filteredRequest, 'logo') ? true : false);

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -307,12 +307,12 @@ export default class SiteService {
createdOn: new Date()
} as Site;
// If the site is assigned to a billing sub-account, check if the billing is active
- if (filteredRequest.billing) {
+ if (filteredRequest.billingData) {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.BILLING_PLATFORM,
Action.CREATE, Entity.SITE, MODULE_NAME, 'handleCreateSite');
- const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billing.id);
- UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billing.id}' does not exist`, MODULE_NAME, 'handleCreateSite', req.user);
- site.billing = { id: billingSubAccount.id };
+ const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billingData.accountID);
+ UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billingData.accountID}' does not exist`, MODULE_NAME, 'handleCreateSite', req.user);
+ site.billingData = { accountID: billingSubAccount.id };
}
// Save
site.id = await SiteStorage.saveSite(req.tenant, site, Utils.objectHasProperty(filteredRequest, 'image'));
@@ -379,12 +379,12 @@ export default class SiteService {
site.lastChangedBy = { 'id': req.user.id };
site.lastChangedOn = new Date();
// If the site is assigned to a billing sub-account, check if the billing is active
- if (filteredRequest.billing) {
+ if (filteredRequest.billingData) {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.BILLING_PLATFORM,
Action.CREATE, Entity.SITE, MODULE_NAME, 'handleUpdateSite');
- const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billing.id);
- UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billing.id}' does not exist`, MODULE_NAME, 'handleUpdateSite', req.user);
- site.billing = { id: billingSubAccount.id };
+ const billingSubAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.billingData.accountID);
+ UtilsService.assertObjectExists(action, billingSubAccount, `Billing Sub-Account ID '${filteredRequest.billingData.accountID}' does not exist`, MODULE_NAME, 'handleUpdateSite', req.user);
+ site.billingData = { accountID: billingSubAccount.id };
}
// Save
await SiteStorage.saveSite(req.tenant, site, Utils.objectHasProperty(filteredRequest, 'image'));

MODIFY
src/storage/mongodb/CompanyStorage.ts
src/storage/mongodb/CompanyStorage.ts
@@ -61,9 +61,9 @@ export default class CompanyStorage {
(coordinate) => Utils.convertToFloat(coordinate)) : [],
};
}
- if (companyToSave.billing) {
- companyMDB.billing = {
- id: DatabaseUtils.convertToObjectID(companyToSave.billing.id),
+ if (companyToSave.billingData) {
+ companyMDB.billingData = {
+ accountID: DatabaseUtils.convertToObjectID(companyToSave.billingData.accountID),
};
}
// Add Last Changed/Created props

MODIFY
src/storage/mongodb/SiteStorage.ts
src/storage/mongodb/SiteStorage.ts
@@ -271,9 +271,9 @@ export default class SiteStorage {
(coordinate) => Utils.convertToFloat(coordinate)) : [],
};
}
- if (siteToSave.billing) {
- siteMDB.billing = {
- id: DatabaseUtils.convertToObjectID(siteToSave.billing.id)
+ if (siteToSave.billingData) {
+ siteMDB.billingData = {
+ accountID: DatabaseUtils.convertToObjectID(siteToSave.billingData.accountID)
};
}
// Add Last Changed/Created props

MODIFY
src/types/Billing.ts
src/types/Billing.ts
@@ -162,6 +162,6 @@ export interface BillingAccount {
}
export interface BillingAccountSetting {
- id: string;
+ accountID: string;
}

MODIFY
src/types/Company.ts
src/types/Company.ts
@@ -14,5 +14,5 @@ export default interface Company extends CreatedUpdatedProps, AuthorizationActio
sites?: Site[];
distanceMeters?: number;
openingTimes?: OpeningTimes;
- billing?: BillingAccountSetting;
+ billingData?: BillingAccountSetting;
}

MODIFY
src/types/Site.ts
src/types/Site.ts
@@ -23,7 +23,7 @@ export default interface Site extends CreatedUpdatedProps, SiteAuthorizationActi
public?: boolean;
openingTimes?: OpeningTimes;
tariffID?: string;
- billing?: BillingAccountSetting;
+ billingData?: BillingAccountSetting;
}
export interface SiteUser {

MODIFY
src/validator/SchemaValidator.ts
src/validator/SchemaValidator.ts
@@ -35,7 +35,7 @@ export default class SchemaValidator {
private static registrationTokenSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/registration-token/registration-token.json`, 'utf8'));
private static siteAreasSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/site-area/site-area.json`, 'utf8'));
private static siteSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/site/site.json`, 'utf8'));
- private static billingSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/billing/billing.json`, 'utf8'));
+ private static billingSubaccountSchema: Schema = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/schemas/billing/billing-subaccount.json`, 'utf8'));
protected moduleName: string;
private readonly ajv: Ajv;
@@ -80,7 +80,7 @@ export default class SchemaValidator {
SchemaValidator.registrationTokenSchema,
SchemaValidator.siteAreasSchema,
SchemaValidator.siteSchema,
- SchemaValidator.billingSchema
+ SchemaValidator.billingSubaccountSchema
]);
}

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -1025,13 +1025,13 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
let companyResponse = await billingTestHelper.userService.companyApi.create({
...CompanyFactory.build(),
- billing: {
- id: subAccountResponse.data.id
+ billingData: {
+ accountID: subAccountResponse.data.id
}
});
expect(companyResponse.status).to.be.eq(StatusCodes.OK);
companyResponse = await billingTestHelper.userService.companyApi.readById(companyResponse.data.id);
- expect(companyResponse.data.billing.id).to.eq(subAccountResponse.data.id);
+ expect(companyResponse.data.billingData.accountID).to.eq(subAccountResponse.data.id);
});
it('should update a company to assign a sub-account', async () => {
@@ -1043,11 +1043,15 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
let companyResponse = await billingTestHelper.userService.companyApi.create(CompanyFactory.build());
expect(companyResponse.status).to.be.eq(StatusCodes.OK);
const companyID = companyResponse.data.id;
- companyResponse = await billingTestHelper.userService.companyApi.update({ id: companyID, ...CompanyFactory.build(), billing: { id: subAccountResponse.data.id } });
+ companyResponse = await billingTestHelper.userService.companyApi.update({
+ id: companyID,
+ ...CompanyFactory.build(),
+ billingData: { accountID: subAccountResponse.data.id }
+ });
expect(companyResponse.status).to.be.eq(StatusCodes.OK);
companyResponse = await billingTestHelper.userService.companyApi.readById(companyID);
- expect(companyResponse.data.billing.id).to.eq(subAccountResponse.data.id);
+ expect(companyResponse.data.billingData.accountID).to.eq(subAccountResponse.data.id);
});
it('should create a site assigned to a sub-account', async () => {
@@ -1063,13 +1067,13 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
let siteResponse = await billingTestHelper.userService.siteApi.create({
...SiteFactory.build(),
companyID: companyResponse.data.id,
- billing: {
- id: subAccountResponse.data.id
+ billingData: {
+ accountID: subAccountResponse.data.id
}
});
expect(siteResponse.status).to.be.eq(StatusCodes.OK);
siteResponse = await billingTestHelper.userService.siteApi.readById(siteResponse.data.id);
- expect(siteResponse.data.billing.id).to.eq(subAccountResponse.data.id);
+ expect(siteResponse.data.billingData.accountID).to.eq(subAccountResponse.data.id);
});
it('should update a site to assign a sub-account', async () => {
@@ -1093,12 +1097,12 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
id: siteID,
...SiteFactory.build(),
companyID: companyResponse.data.id,
- billing: { id: subAccountResponse.data.id }
+ billingData: { accountID: subAccountResponse.data.id }
});
expect(siteResponse.status).to.be.eq(StatusCodes.OK);
siteResponse = await billingTestHelper.userService.siteApi.readById(siteID);
- expect(siteResponse.data.billing.id).to.eq(subAccountResponse.data.id);
+ expect(siteResponse.data.billingData.accountID).to.eq(subAccountResponse.data.id);
});
});
});

refactor(subaccounts: renamed billing object in company/site