/
Automation.ts
120 lines (100 loc) · 3.15 KB
/
Automation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { QlikSaaSClient } from "qlik-rest-api";
import { IAutomation } from "./Automation.interfaces";
import { IRun, Run } from "./Run";
export class Automation {
#id: string;
#saasClient: QlikSaaSClient;
details: IAutomation;
constructor(saasClient: QlikSaaSClient, id: string, details?: IAutomation) {
if (!id) throw new Error(`automation.get: "id" parameter is required`);
this.details = details ?? ({} as IAutomation);
this.#id = id;
this.#saasClient = saasClient;
}
async init(arg?: { force: boolean }) {
if (
!this.details ||
Object.keys(this.details).length == 0 ||
arg?.force == true
) {
this.details = await this.#saasClient
.Get<IAutomation>(`automations/${this.#id}`)
.then((res) => res.data);
}
}
async remove() {
return await this.#saasClient
.Delete(`automations/${this.#id}`)
.then((res) => res.status);
}
async copy(arg: { name: string }) {
if (!arg.name)
throw new Error(`automation.copy: "name" parameter is required`);
const copyResponse = await this.#saasClient
.Post<IAutomation>(`automations/${this.#id}/actions/copy`, {
name: arg.name,
})
.then((res) => res.data);
const newAutomation = new Automation(
this.#saasClient,
(copyResponse as any).id
);
await newAutomation.init();
return newAutomation;
}
async enable() {
let updateStatus = 0;
return await this.#saasClient
.Post(`automations/${this.#id}/actions/enable`, {})
.then((res) => {
updateStatus = res.status;
return this.init({ force: true });
})
.then(() => updateStatus);
}
async disable() {
let updateStatus = 0;
return await this.#saasClient
.Post(`automations/${this.#id}/actions/disable`, {})
.then((res) => {
updateStatus = res.status;
return this.init({ force: true });
})
.then(() => updateStatus);
}
async move() {
let updateStatus = 0;
return await this.#saasClient
.Post(`automations/${this.#id}/actions/move`, {})
.then((res) => {
updateStatus = res.status;
return this.init({ force: true });
})
.then(() => updateStatus);
}
async getRuns() {
return await this.#saasClient
.Get<IRun[]>(`automations/automations/${this.#id}/runs`)
.then((res) => {
return res.data.map((t) => new Run(this.#saasClient, t.id, this.#id, t));
});
}
async getRun(arg: { runId: string }) {
if (!arg.runId)
throw new Error(`automation.getRun: "runId" parameter is required`);
return await this.#saasClient
.Get<IRun[]>(`automations/automations/${this.#id}/runs`)
.then((res) => {
const runData = res.data.filter((r) => r.id == arg.runId);
if (runData.length == 0)
throw new Error(
`automation.getRun: run with id "${arg.runId}" was not found`
);
if (runData.length > 1)
throw new Error(
`automation.getRun: found more than one run with id "${arg.runId}"`
);
return new Run(this.#saasClient, runData[0].id, this.#id, runData[0]);
});
}
}