refactor(subaccounts): onboarding code refactor
MODIFY
src/server/rest/v1/router/util/UtilRouter.ts
src/server/rest/v1/router/util/UtilRouter.ts
@@ -88,7 +88,7 @@ export default class UtilRouter {
}
private buildRouteBillingActivateSubAccount(): void {
- this.router.get(`/${RESTServerRoute.REST_BILLING_SUB_ACCOUNT_ACTIVATE}`, (req: Request, res: Response, next: NextFunction) => {
+ this.router.patch(`/${RESTServerRoute.REST_BILLING_SUB_ACCOUNT_ACTIVATE}`, (req: Request, res: Response, next: NextFunction) => {
req.params.ID = req.params.id;
void RouterUtils.handleRestServerAction(BillingService.handleActivateSubAccount.bind(this), ServerAction.BILLING_SUB_ACCOUNT_ACTIVATE, req, res, next);
});

MODIFY
src/server/rest/v1/service/BillingService.ts
src/server/rest/v1/service/BillingService.ts
@@ -503,20 +503,10 @@ export default class BillingService {
}
public static async handleActivateSubAccount(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
- const filteredRequest = BillingValidatorRest.getInstance().validateBillingActivateSubAccountReq({ ...req.params, ...req.query });
+ const filteredRequest = BillingValidatorRest.getInstance().validateBillingActivateSubAccountReq({ ...req.params, ...req.body });
const tenant = await TenantStorage.getTenant(filteredRequest.TenantID);
UtilsService.assertObjectExists(action, tenant, `Tenant ID '${filteredRequest.TenantID}' does not exist`, MODULE_NAME, 'handleActivateSubAccount');
- // Get the billing impl
- const billingImpl = await BillingFactory.getBillingImpl(tenant);
- if (!billingImpl) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'Billing service is not configured',
- module: MODULE_NAME, method: 'handleActivateSubAccount',
- action: action,
- user: req.user
- });
- }
+
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
@@ -539,12 +529,12 @@ export default class BillingService {
user: req.user
});
}
- // Activate and save the sub account
- subAccount.status = BillingAccountStatus.ACTIVE;
- await BillingStorage.saveSubAccount(tenant, subAccount);
// Get the sub account owner
const user = await UserStorage.getUser(tenant, subAccount.userID);
UtilsService.assertObjectExists(action, user, `User ID '${subAccount.userID}' does not exist`, MODULE_NAME, 'handleActivateSubAccount', req.user);
+ // Activate and save the sub account
+ subAccount.status = BillingAccountStatus.ACTIVE;
+ await BillingStorage.saveSubAccount(tenant, subAccount);
// Notify the user
void NotificationHandler.sendBillingSubAccountActivationNotification(
tenant, Utils.generateUUID(), user, { evseDashboardURL: Utils.buildEvseURL(tenant.subdomain), user });
@@ -623,12 +613,12 @@ export default class BillingService {
user: req.user
});
}
- // Activate and save the sub account
- subAccount.status = BillingAccountStatus.PENDING;
- await BillingStorage.saveSubAccount(req.tenant, subAccount);
// 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);
+ // Activate and save the sub account
+ subAccount.status = BillingAccountStatus.PENDING;
+ await BillingStorage.saveSubAccount(req.tenant, subAccount);
// Notify the user
void NotificationHandler.sendBillingSubAccountCreationLink(
req.tenant, Utils.generateUUID(), user, { onboardingLink: subAccount.activationLink, evseDashboardURL: Utils.buildEvseURL(req.tenant.subdomain), user });

MODIFY
test/api/client/BillingApi.ts
test/api/client/BillingApi.ts
@@ -57,7 +57,7 @@ export default class BillingApi extends CrudApi {
}
public async activateSubAccount(params) {
- return super.read(params, super.buildUtilRestEndpointUrl(RESTServerRoute.REST_BILLING_SUB_ACCOUNT_ACTIVATE, { id: params.accountID }));
+ return super.patch(params, super.buildUtilRestEndpointUrl(RESTServerRoute.REST_BILLING_SUB_ACCOUNT_ACTIVATE, { id: params.accountID }));
}
public async readAllSubAccounts(params, paging = TestConstants.DEFAULT_PAGING, ordering = TestConstants.DEFAULT_ORDERING) {

MODIFY
test/api/client/utils/CrudApi.ts
test/api/client/utils/CrudApi.ts
@@ -65,13 +65,11 @@ export default class CrudApi {
});
}
- public async patch(id, path): Promise<any> {
+ public async patch(data, path): Promise<any> {
return this._authenticatedApi.send({
method: 'PATCH',
url: path,
- params: {
- ID: id
- }
+ data
});
}

refactor(subaccounts): onboarding code refactor