Skip to content

Commit

Permalink
fix: filter out failed plan calls
Browse files Browse the repository at this point in the history
  • Loading branch information
santese committed Aug 9, 2024
1 parent f71108c commit 47a9e1e
Showing 1 changed file with 46 additions and 31 deletions.
77 changes: 46 additions & 31 deletions src/lib/plans/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,63 @@ export class Plans extends Base {

/**
* retrieves available global licensing for both user and storage that aren't assigned to customers
* This will filter out any failed plans whose calls to the API return an error
* @returns object containing the available user and storage data
*/
async getPlans(): Promise<any> {
const { authCookie, csrfToken } = await this.authenticate()
const planIds = await this._getAllPlanIds(authCookie)

return Promise.all(
planIds.map(async (id) => {
const [planDataRes, planPowerUserDataRes] = await Promise.all([
this.http.get<[string, UsageStats][]>(
`/msp/usage_stats/${this.resellerId}/${id}/`,
{
headers: { cookie: authCookie, 'X-CSRFToken': csrfToken },
},
),
this.http.get(`/msp/get_plan_pu_data/${this.resellerId}/${id}/`, {
headers: { cookie: authCookie, 'X-CSRFToken': csrfToken },
}),
])
planIds
.map(async (id) => {
let planDataRes: any
let planPowerUserDataRes: any
try {
;[planDataRes, planPowerUserDataRes] = await Promise.all([
this.http.get<[string, UsageStats][]>(
`/msp/usage_stats/${this.resellerId}/${id}/`,
{
headers: {
cookie: authCookie,
'X-CSRFToken': csrfToken,
},
},
),
this.http.get(`/msp/get_plan_pu_data/${this.resellerId}/${id}/`, {
headers: {
cookie: authCookie,
'X-CSRFToken': csrfToken,
},
}),
])
} catch (e) {
return
}

const planData = planDataRes.data
const planPowerUserData = planPowerUserDataRes.data
const planData = planDataRes.data
const planPowerUserData = planPowerUserDataRes.data

let ref = {} as UsageStats
if (planData.length > 0) {
const entries = Object.entries(planData[0] ?? [])
if (entries.length > 0) {
ref = (entries[0] ?? [])[1] as UsageStats
let ref = {} as UsageStats
if (planData.length > 0) {
const entries = Object.entries(planData[0] ?? [])
if (entries.length > 0) {
ref = (entries[0] ?? [])[1] as UsageStats
}
}
}

return {
planId: id,
totalPowerUsers: planPowerUserData?.purchased,
usedPowerUsers: planPowerUserData?.purchased
? planPowerUserData?.purchased - ref.power_user_stats?.Available
: undefined,
availablePowerUsers: ref.power_user_stats?.Available,
availableStorage: ref.storage_stats?.Available,
customers: planData.map((customer: object) => Object.keys(customer)[0]),
}
}),
return {
planId: id,
totalPowerUsers: planPowerUserData?.purchased,
usedPowerUsers: planPowerUserData?.purchased
? planPowerUserData?.purchased - ref.power_user_stats?.Available
: undefined,
availablePowerUsers: ref.power_user_stats?.Available,
availableStorage: ref.storage_stats?.Available,
customers: planData.map((customer: object) => Object.keys(customer)[0]),
}
})
.filter((e) => e),
)
}

Expand Down

0 comments on commit 47a9e1e

Please sign in to comment.