egnyte-resellers
Version:
Library for managing things against the undocumented egnyte resellers API.
98 lines (97 loc) • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Plans = void 0;
const base_1 = require("../base/base");
class Plans extends base_1.Base {
constructor(_config) {
super(_config);
}
async getPlans() {
const { authCookie, csrfToken } = await this.authenticate();
const planIds = await this._getAllPlanIds(authCookie);
return Promise.all(planIds
.map(async (id) => {
var _a, _b, _c, _d, _e;
let planDataRes;
let planPowerUserDataRes;
try {
;
[planDataRes, planPowerUserDataRes] = await Promise.all([
this.http.get(`/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;
let ref = {};
if (planData.length > 0) {
const entries = Object.entries((_a = planData[0]) !== null && _a !== void 0 ? _a : []);
if (entries.length > 0) {
ref = ((_b = entries[0]) !== null && _b !== void 0 ? _b : [])[1];
}
}
return {
planId: id,
totalPowerUsers: planPowerUserData === null || planPowerUserData === void 0 ? void 0 : planPowerUserData.purchased,
usedPowerUsers: (planPowerUserData === null || planPowerUserData === void 0 ? void 0 : planPowerUserData.purchased)
? (planPowerUserData === null || planPowerUserData === void 0 ? void 0 : planPowerUserData.purchased) - ((_c = ref.power_user_stats) === null || _c === void 0 ? void 0 : _c.Available)
: undefined,
availablePowerUsers: (_d = ref.power_user_stats) === null || _d === void 0 ? void 0 : _d.Available,
availableStorage: (_e = ref.storage_stats) === null || _e === void 0 ? void 0 : _e.Available,
customers: planData.map((customer) => Object.keys(customer)[0]),
};
})
.filter((e) => e));
}
async getAllProtectPlans() {
const { authCookie, csrfToken } = await this.authenticate();
const { data: protectUsage } = await this.http.get(`/msp/usage_stats/${this.resellerId}/${this.config.protectPlanId}/`, {
headers: { cookie: authCookie, 'X-CSRFToken': csrfToken },
});
return protectUsage;
}
async UpdatePowerUserLicensing(planId, newTotalLicenses) {
const { authCookie, csrfToken } = await this.authenticate();
const { data: res } = await this.http.post(`https://resellers.egnyte.com/msp/change_plan_power_users/${this.resellerId}/`, {
plan_id: planId,
plan_power_users: newTotalLicenses.toString(),
}, {
headers: {
Cookie: `${authCookie}; csrftoken=${csrfToken}`,
'X-Requested-With': 'XMLHttpRequest',
'X-CSRFToken': csrfToken,
Referer: 'https://resellers.egnyte.com',
},
});
if (res.success !== true)
throw new Error(res.msg);
return (await this.getPlans()).filter((e) => e.planId === planId);
}
async _getAllPlanIds(authCookie) {
if (!authCookie)
throw new Error('missing authCookie');
if (!this.resellerId)
await this.setResellerId(authCookie);
const { data: res } = await this.http.get(`/msp/customer_data/${this.resellerId}`, {
headers: { cookie: authCookie },
});
return res
.filter((customer) => customer.status !== 'deleted')
.map((customer) => customer.plan_id.toString())
.filter((v, i, s) => s.indexOf(v) === i);
}
}
exports.Plans = Plans;