refactor(subaccounts): removed siteID and companyID
MODIFY
src/assets/server/rest/v1/docs/e-mobility-oas.json
src/assets/server/rest/v1/docs/e-mobility-oas.json
"required": [
"userID"
],
- "oneOf": [
- {
- "required": [
- "siteID"
- ]
- },
- {
- "required": [
- "companyID"
- ]
- }
- ],
"properties": {
"userID": {
"type": "string"
- },
- "siteID": {
- "type": "string"
- },
- "companyID": {
- "type": "string"
}
}
}

MODIFY
src/assets/server/rest/v1/schemas/billing/billing-create-sub-account.json
src/assets/server/rest/v1/schemas/billing/billing-create-sub-account.json
"properties": {
"userID": {
"$ref": "common#/definitions/id"
- },
- "siteID": {
- "$ref": "common#/definitions/id"
- },
- "companyID": {
- "$ref": "common#/definitions/id"
}
},
"required": [
"userID"
- ],
- "oneOf": [
- { "required": ["siteID"] },
- { "required": ["companyID"] }
]
}

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -492,25 +492,13 @@ export default class BillingService {
user: req.user
});
}
- let subAccount: BillingAccount = {} as BillingAccount;
-
// Get the user
const user = await UserStorage.getUser(req.tenant, filteredRequest.userID);
UtilsService.assertObjectExists(action, user, `User ID '${filteredRequest.userID}' does not exist`,
MODULE_NAME, 'handleCreateSubAccount', req.user);
- subAccount.userID = user.id;
- // Get the company or the site
- if (filteredRequest.companyID) {
- const company = await CompanyStorage.getCompany(req.tenant, filteredRequest.companyID);
- UtilsService.assertObjectExists(action, company, `Company ID '${filteredRequest.companyID}' does not exist`, MODULE_NAME, 'handleCreateSubAccount', req.user);
- subAccount.companyID = company.id;
- } else if (filteredRequest.siteID) {
- const site = await SiteStorage.getSite(req.tenant, filteredRequest.siteID);
- UtilsService.assertObjectExists(action, site, `Site ID '${filteredRequest.siteID}' does not exist`, MODULE_NAME, 'handleCreateSubAccount', req.user);
- subAccount.siteID = site.id;
- }
// Create the sub account
- subAccount = { ...subAccount, ...await billingImpl.createSubAccount() };
+ const subAccount = await billingImpl.createSubAccount();
+ subAccount.userID = user.id;
// Save the sub account
subAccount.id = await BillingStorage.saveSubAccount(req.tenant, subAccount);
// Notify the user

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -225,9 +225,7 @@ export default class BillingStorage {
_id: subAccount.id ? DatabaseUtils.convertToObjectID(subAccount.id) : new ObjectId(),
accountID: subAccount.accountID,
pending: subAccount.pending,
- userID: DatabaseUtils.convertToObjectID(subAccount.userID),
- siteID: subAccount.siteID ? DatabaseUtils.convertToObjectID(subAccount.siteID) : null,
- companyID: subAccount.companyID ? DatabaseUtils.convertToObjectID(subAccount.companyID) : null
+ userID: DatabaseUtils.convertToObjectID(subAccount.userID)
};
// Modify and return the modified document
await global.database.getCollection<any>(tenant.id, 'billingsubaccounts').findOneAndUpdate(

MODIFY
src/types/Billing.ts
src/types/Billing.ts
@@ -159,7 +159,5 @@ export interface BillingAccount {
activationLink?: string;
userID?: string;
pending: boolean;
- siteID?: string;
- companyID?: string;
}

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
@@ -395,10 +395,9 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
expect(response.data.result.length).to.be.gt(0);
});
- it('should create a sub account for a site', async () => {
+ it('should create a sub account', async () => {
const response = await billingTestHelper.userService.billingApi.createSubAccount({
- userID: billingTestHelper.userContext.id,
- siteID: billingTestHelper.siteContext.getSite().id,
+ userID: billingTestHelper.userContext.id
});
expect(response.status).to.be.eq(StatusCodes.CREATED);
expect(response.data.id).to.not.be.null;
@@ -406,40 +405,6 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
expect(response.data.userID).to.eq(billingTestHelper.userContext.id);
expect(response.data.activationLink).to.not.be.null;
expect(response.data.pending).to.be.true;
- expect(response.data.siteID).to.be.eq(billingTestHelper.siteContext.getSite().id);
- expect(response.data.companyID).to.not.exist;
- });
-
- it('should create a sub account for a company', async () => {
- const company = await billingTestHelper.tenantContext.createCompany(CompanyFactory.build());
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- userID: billingTestHelper.userContext.id,
- companyID: company.id
- });
- expect(response.status).to.be.eq(StatusCodes.CREATED);
- expect(response.data.id).to.not.be.null;
- expect(response.data.accountID).to.not.be.null;
- expect(response.data.userID).to.eq(billingTestHelper.userContext.id);
- expect(response.data.activationLink).to.not.be.null;
- expect(response.data.pending).to.be.true;
- expect(response.data.siteID).to.not.exist;
- expect(response.data.companyID).to.be.eq(company.id);
- });
-
- it('should not create a sub account for a company AND a site', async () => {
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- userID: billingTestHelper.userContext.id,
- companyID: '6283bc3cc746c38fa184d716',
- siteID: '6283bc3cc746c38fa184d715',
- });
- expect(response.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
- });
-
- it('should not create a sub account without company or site', async () => {
- const response = await billingTestHelper.userService.billingApi.createSubAccount({
- userID: billingTestHelper.userContext.id
- });
- expect(response.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
});
});
@@ -503,8 +468,7 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
it('should not be able to create a sub account', async () => {
const response = await billingTestHelper.userService.billingApi.createSubAccount({
- userID: billingTestHelper.userContext.id,
- siteID: '6283bc3cc746c38fa184d716'
+ userID: billingTestHelper.userContext.id
});
expect(response.status).to.be.eq(StatusCodes.FORBIDDEN);
});

refactor(subaccounts): removed siteID and companyID