Skip to content

Commit

Permalink
Added getApplication specific
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinc committed Sep 5, 2024
1 parent c729c8b commit 88bdc26
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 46 deletions.
84 changes: 50 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/plugins/service-iq-enterprise/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ export class IQEnterprise<
);
}

public async getApplication(
uid: string,
hostname?: string,
username?: string,
password?: string,
): Promise<APIApplicationResponse<Meta>> {
return await this.events.emitEventAndReturn(
"getApplication",
30,
uid,
hostname ?? this.customConfig.hostname,
username ?? this.customConfig.username,
password ?? this.customConfig.password,
);
}

public async getServiceUsageBySubAccount(
subAccountId: string,
month: number,
Expand Down
54 changes: 42 additions & 12 deletions src/plugins/service-iq-enterprise/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export interface Events<Meta extends object>
username?: string,
password?: string,
): Promise<Array<APIApplicationResponse<Meta>>>;
getApplication(
uid: string,
hostname?: string,
username?: string,
password?: string,
): Promise<APIApplicationResponse<Meta>>;
updateApplication(
applicationId: number,
data: NewAPIApplication<Meta>,
Expand Down Expand Up @@ -496,14 +502,14 @@ export class Plugin<Meta extends object = any>
password?: string,
) => {
const axios: Axios = await this.getAxios(hostname, username, password);
const resp = await axios.get<Array<APIApplicationResponse<any>>>(
const resp = await axios.get<Array<APIApplicationResponse<any> & {portalmeta?: string}>>(
`/api/portal/application/${encodeURIComponent(email)}`,
);
if (resp.status == 200 && resp.data) {
return resp.data.map((d: any) => {
return resp.data.map((d) => {
d.meta = Tools.isString(d.portalmeta)
? JSON.parse(d.portalmeta)
: null;
? JSON.parse(d.portalmeta)
: null;
delete d.portalmeta;
return d;
});
Expand All @@ -513,6 +519,31 @@ export class Plugin<Meta extends object = any>
);
},
);
await this.events.onReturnableEvent(
"getApplication",
async (
uid: string,
hostname?: string,
username?: string,
password?: string,
) => {
const axios: Axios = await this.getAxios(hostname, username, password);
const resp = await axios.get<APIApplicationResponse<any> & {portalmeta?: string}>(
`/api/portal/application/id/${encodeURIComponent(uid)}`,
);
if (resp.status == 200 && resp.data) {
let d = resp.data;

Check failure on line 535 in src/plugins/service-iq-enterprise/plugin.ts

View workflow job for this annotation

GitHub Actions / build-plugin-release / build_plugin (18.x)

'd' is never reassigned. Use 'const' instead

Check failure on line 535 in src/plugins/service-iq-enterprise/plugin.ts

View workflow job for this annotation

GitHub Actions / build-plugin-release / build_plugin (20.x)

'd' is never reassigned. Use 'const' instead
d.meta = Tools.isString(d.portalmeta)
? JSON.parse(d.portalmeta)
: null;
delete d.portalmeta;
return d;
}
throw new Error(
`Error ${resp.status}: ${resp.statusText} [${resp.data}]`,
);
},
);
await this.events.onReturnableEvent(
"getServiceUsageByAccount",
async (
Expand All @@ -535,8 +566,8 @@ export class Plugin<Meta extends object = any>
`/api/portal/usage/${encodeURIComponent(
id,
)}?${Object.keys(query)
.map(x => `${x}=${encodeURIComponent(query[x])}`)
.join("&")}`,
.map(x => `${x}=${encodeURIComponent(query[x])}`)
.join("&")}`,
);
if (resp.status == 200) {
return resp.data;
Expand All @@ -560,7 +591,7 @@ export class Plugin<Meta extends object = any>
if (resp.status == 200 && Tools.isString(resp.data.status)) {
const result = resp.data.status.toLowerCase();
if (Object.keys(UpgradeDowngradeStatusTypes)
.indexOf(result) !== -1) {
.indexOf(result) !== -1) {
let dateAsEpoc = null;
if (Tools.isString(resp.data.eta)) {
const timeStr = "00:00:00";
Expand Down Expand Up @@ -771,7 +802,7 @@ export class Plugin<Meta extends object = any>
): Promise<Axios> {
const now = new Date().getTime();
if (cleanup) {
for (let i = 0 ; i < this._axios.length ; i++) {
for (let i = 0; i < this._axios.length; i++) {
if (this._axios[i].hostname !== hostname) {
continue;
}
Expand All @@ -781,7 +812,7 @@ export class Plugin<Meta extends object = any>
this._axios.splice(i, 1);
}
}
for (let i = 0 ; i < this._axios.length ; i++) {
for (let i = 0; i < this._axios.length; i++) {
if (this._axios[i].hostname !== hostname) {
continue;
}
Expand Down Expand Up @@ -821,7 +852,7 @@ export class Plugin<Meta extends object = any>
instance.defaults.headers.common[
"Authorization"
] = `Bearer ${await self.getToken(instance, username, password)}`;
for (let i = 0 ; i < self._axios.length ; i++) {
for (let i = 0; i < self._axios.length; i++) {
if (self._axios[i].hostname !== hostname) {
continue;
}
Expand All @@ -833,8 +864,7 @@ export class Plugin<Meta extends object = any>
}

return instance(config);
}
catch (e: any) {
} catch (e: any) {
self.log.error(e.message ?? e, {});
}
// Do something with response error
Expand Down

0 comments on commit 88bdc26

Please sign in to comment.