From 6674ad4c9dd61b7d5702500eb4083939e7804b94 Mon Sep 17 00:00:00 2001 From: Codie Jones Date: Tue, 17 Aug 2021 12:30:13 -0700 Subject: [PATCH] feat(updatecustomerpowerusers): check plan total license pool and update if flag is passed --- src/index.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2c513d8..040ff09 100644 --- a/src/index.ts +++ b/src/index.ts @@ -315,10 +315,14 @@ class Egnyte { * @param numOfUsers how many licenses to assign to customer * @returns response object */ - async updateCustomerPowerUsers(customerId: string, numOfUsers: number): Promise { + async updateCustomerPowerUsers( + customerId: string, + numOfUsers: number, + autoAddToPool?: boolean, + ): Promise { customerId = customerId.toLowerCase() const customer = await this.getOneCustomer(customerId) - if (customer.powerUsers.available <= 0) + if (customer.powerUsers.available <= 0 && !autoAddToPool) throw new Error('No available licenses on customers reseller plan.') if (numOfUsers < customer.powerUsers.used && this._config.forceLicenseChange !== true) { const response: EgnyteUpdateResponse = { @@ -333,6 +337,18 @@ class Egnyte { } return response } + if (autoAddToPool) { + const plans = await this.getPlans() + const { planId, totalPowerUsers, availablePowerUsers } = plans.find( + (e: any) => e.planId === customer.planId, + ) + const usersNeeded = numOfUsers - customer.powerUsers.total + if (usersNeeded > availablePowerUsers) { + const licensesToAdd = Math.ceil((usersNeeded - availablePowerUsers) / 5) * 5 + const updatedLicenesTotal = licensesToAdd + totalPowerUsers + this.UpdatePowerUserLicensing(planId, updatedLicenesTotal) + } + } const { authCookie, csrfToken } = await this._authenticate() const res = await this._egnyteRequest(`/msp/change_power_users/${this.resellerId}/`, {