refactor(subaccounts): requested changes
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -393,7 +393,7 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
{ resource: Entity.BILLING, action: [Action.CHECK_CONNECTION, Action.CLEAR_BILLING_TEST_DATA] },
{
resource: Entity.BILLING_PLATFORM,
- action: [Action.BILLING_CREATE_SUB_ACCOUNT, Action.BILLING_SEND_SUB_ACCOUNT_ONBOARDING, Action.LIST, Action.READ],
+ action: [Action.BILLING_CREATE_SUB_ACCOUNT, Action.BILLING_SUB_ACCOUNT_ONBOARD, Action.LIST, Action.READ],
attributes: [
'id', 'accountID', 'userID', 'user.id', 'user.firstName', 'user.lastName', 'user.email', 'status'
]

MODIFY
src/server/rest/v1/router/api/BillingRouter.ts
src/server/rest/v1/router/api/BillingRouter.ts
@@ -168,7 +168,7 @@ export default class BillingRouter {
private buildRouteBillingSendSubAccountOnboarding(): void {
this.router.patch(`/${RESTServerRoute.REST_BILLING_SUB_ACCOUNT_ONBOARD}`, (req: Request, res: Response, next: NextFunction) => {
req.params.ID = req.params.id;
- void RouterUtils.handleRestServerAction(BillingService.handleSendSubAccountOnboarding.bind(this), ServerAction.BILLING_SUB_ACCOUNT_SEND_ONBOARDING, req, res, next);
+ void RouterUtils.handleRestServerAction(BillingService.handleOnboardAccount.bind(this), ServerAction.BILLING_SUB_ACCOUNT_ONBOARD, req, res, next);
});
}
}

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -510,20 +510,10 @@ export default class BillingService {
const subAccount = await BillingStorage.getSubAccountByID(tenant, filteredRequest.ID);
UtilsService.assertObjectExists(action, subAccount, `Sub account ID '${filteredRequest.ID}' does not exist`, MODULE_NAME, 'handleActivateSubAccount', req.user);
// Check if the sub account onboarding has been sent
- if (subAccount.status === BillingAccountStatus.IDLE) {
+ if (subAccount.status !== BillingAccountStatus.PENDING) {
throw new AppError({
errorCode: HTTPError.GENERAL_ERROR,
- message: 'Sub account onboarding has not been sent',
- module: MODULE_NAME, method: 'handleActivateSubAccount',
- action: action,
- user: req.user
- });
- }
- // Check if the sub account is already activated
- if (subAccount.status === BillingAccountStatus.ACTIVE) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'Sub account is already activated',
+ message: 'Sub-account onboarding aborted - current status should be PENDING',
module: MODULE_NAME, method: 'handleActivateSubAccount',
action: action,
user: req.user
@@ -584,38 +574,28 @@ export default class BillingService {
next();
}
- public static async handleSendSubAccountOnboarding(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ public static async handleOnboardAccount(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.BILLING_PLATFORM,
- Action.BILLING_SEND_SUB_ACCOUNT_ONBOARDING, Entity.BILLING_PLATFORM, MODULE_NAME, 'handleSendSubAccountOnboarding');
+ Action.BILLING_SUB_ACCOUNT_ONBOARD, Entity.BILLING_PLATFORM, MODULE_NAME, 'handleOnboardAccount');
const filteredRequest = BillingValidatorRest.getInstance().validateBillingSubAccountGetReq(req.params);
// Check authorization
- await AuthorizationService.checkAndGetBillingPlatformAuthorizations(req.tenant, req.user, Action.BILLING_SEND_SUB_ACCOUNT_ONBOARDING);
+ await AuthorizationService.checkAndGetBillingPlatformAuthorizations(req.tenant, req.user, Action.BILLING_SUB_ACCOUNT_ONBOARD);
const subAccount = await BillingStorage.getSubAccountByID(req.tenant, filteredRequest.ID);
- UtilsService.assertObjectExists(action, subAccount, `Sub account ID '${filteredRequest.ID}' does not exist`, MODULE_NAME, 'handleSendSubAccountOnboarding', req.user);
+ UtilsService.assertObjectExists(action, subAccount, `Sub account ID '${filteredRequest.ID}' does not exist`, MODULE_NAME, 'handleOnboardAccount', req.user);
// Check if the sub account onboarding is already sent
- if (subAccount.status === BillingAccountStatus.PENDING) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'Sub account onboarding is already sent',
- module: MODULE_NAME, method: 'handleSendSubAccountOnboarding',
- action: action,
- user: req.user
- });
- }
- // Check if the sub account is already activated
- if (subAccount.status === BillingAccountStatus.ACTIVE) {
+ if (subAccount.status !== BillingAccountStatus.IDLE) {
throw new AppError({
errorCode: HTTPError.GENERAL_ERROR,
- message: 'Sub account is already activated',
- module: MODULE_NAME, method: 'handleSendSubAccountOnboarding',
+ message: 'Sub-account onboarding aborted - current status should be IDLE',
+ module: MODULE_NAME, method: 'handleOnboardAccount',
action: action,
user: req.user
});
}
// Get the sub account owner
const user = await UserStorage.getUser(req.tenant, subAccount.userID);
- UtilsService.assertObjectExists(action, user, `User ID '${subAccount.userID}' does not exist`, MODULE_NAME, 'handleSendSubAccountOnboarding', req.user);
+ UtilsService.assertObjectExists(action, user, `User ID '${subAccount.userID}' does not exist`, MODULE_NAME, 'handleOnboardAccount', req.user);
// Activate and save the sub account
subAccount.status = BillingAccountStatus.PENDING;
await BillingStorage.saveSubAccount(req.tenant, subAccount);

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
@@ -137,7 +137,7 @@ export enum Action {
BILLING_CHARGE_INVOICE = 'BillingChargeInvoice',
BILLING_CREATE_SUB_ACCOUNT = 'BillingSubAccountCreate',
BILLING_ACTIVATE_SUB_ACCOUNT = 'BillingSubAccountActivate',
- BILLING_SEND_SUB_ACCOUNT_ONBOARDING = 'BillingSendSubAccountOnboarding',
+ BILLING_SUB_ACCOUNT_ONBOARD = 'BillingSubAccountOnboard',
CHECK_CONNECTION = 'CheckConnection',
CLEAR_BILLING_TEST_DATA = 'ClearBillingTestData',
RETRIEVE_CONSUMPTION = 'RetrieveConsumption',

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -454,7 +454,7 @@ export enum ServerAction {
BILLING_BILL_PENDING_TRANSACTION = 'BillingBillPendingTransaction',
BILLING_SUB_ACCOUNT_CREATE = 'BillingSubAccountCreate',
BILLING_SUB_ACCOUNT_ACTIVATE = 'BillingSubAccountActivate',
- BILLING_SUB_ACCOUNT_SEND_ONBOARDING = 'BillingSubAccountSendOnboarding',
+ BILLING_SUB_ACCOUNT_ONBOARD = 'BillingSubAccountOnboard',
BILLING_SUB_ACCOUNT = 'BillingSubAccount',
BILLING_SUB_ACCOUNTS = 'BillingSubAccounts',

refactor(subaccounts): requested changes