/
ReloadTasks.ts
176 lines (149 loc) · 5.05 KB
/
ReloadTasks.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { QlikGenericRestClient, QlikRepositoryClient } from "qlik-rest-api";
import { URLBuild } from "./util/generic";
import { UpdateCommonProperties } from "./util/UpdateCommonProps";
import {
IEntityRemove,
IReloadTaskBundle,
ISelection,
ITask,
ITaskCreate,
} from "./types/interfaces";
import { ReloadTask } from "./ReloadTask";
import { getAppForReloadTask } from "./util/ReloadTaskUtil";
//TODO: why is no update method here?
export interface IClassReloadTasks {
get(arg: { id: string }): Promise<ReloadTask>;
getAll(): Promise<ReloadTask[]>;
getFilter(arg: { filter: string }): Promise<ReloadTask[]>;
count(arg?: { filter: string }): Promise<number>;
select(arg?: { filter: string }): Promise<ISelection>;
removeFilter(arg: { filter: string }): Promise<IEntityRemove[]>;
create(arg: ITaskCreate): Promise<ReloadTask>;
}
export class ReloadTasks implements IClassReloadTasks {
#repoClient: QlikRepositoryClient;
#genericClient: QlikGenericRestClient;
constructor(private mainRepoClient: QlikRepositoryClient) {
this.#repoClient = mainRepoClient;
}
public async get(arg: { id: string }) {
if (!arg.id) throw new Error(`reloadTasks.get: "id" parameter is required`);
const reloadTask: ReloadTask = new ReloadTask(this.#repoClient, arg.id);
await reloadTask.init();
return reloadTask;
}
public async getAll() {
return await this.#repoClient
.Get<ITask[]>(`reloadtask/full`)
.then((res) => res.data)
.then(async (data) => {
return await Promise.all(
data.map(async (t) => {
const task = new ReloadTask(this.#repoClient, t.id, t);
await task.init();
return task;
})
);
});
}
// "funny story" but tasks are slightly different from the rest
// not only their data must be init but also all the data for the
// triggers will have to be init at the same time
// for this reason we cant return only the instance (new ReloadTask)
// but actually have to call the init() method for each task
// this way when the task returned it will have
// triggersDetails populated
public async getFilter(arg: { filter: string }) {
if (!arg.filter)
throw new Error(`reloadTasks.getFilter: "path" parameter is required`);
return await this.#repoClient
.Get<ITask[]>(
`reloadtask/full?filter=(${encodeURIComponent(arg.filter)})`
)
.then((res) => res.data)
.then(async (data) => {
return await Promise.all(
data.map(async (t) => {
const task = new ReloadTask(this.#repoClient, t.id, t);
await task.init();
return task;
})
);
});
// .then(async (rt) => {
// return await Promise.all<ReloadTask>(
// rt.map((r) => {
// return r.init().then((r1) => r);
// })
// );
// })
// .then((r) => r);
}
public async count(arg?: { filter: string }) {
let url: string = `reloadtask/count`;
if (arg.filter) url += `${url}?filter=(${encodeURIComponent(arg.filter)})`;
return await this.#repoClient
.Get<{ value: number }>(`${url}`)
.then((res) => res.data.value);
}
public async removeFilter(arg: { filter: string }) {
if (!arg.filter)
throw new Error(
`reloadTasks.removeFilter: "filter" parameter is required`
);
const tasks = await this.getFilter({ filter: arg.filter });
return Promise.all<IEntityRemove>(
tasks.map((task) =>
task.remove().then((s) => ({ id: task.details.id, status: s }))
)
);
}
public async select(arg?: { filter: string }) {
const urlBuild = new URLBuild(`selection/reloadtask`);
urlBuild.addParam("filter", arg.filter);
return await this.#repoClient
.Post<ISelection>(urlBuild.getUrl(), {})
.then((res) => res.data);
}
public async create(arg: ITaskCreate) {
if (!arg.name) throw new Error(`task.create: "name" parameter is required`);
const app = await getAppForReloadTask(
arg.appId,
arg.appFilter,
this.#repoClient
);
let reloadTask: IReloadTaskBundle = {
schemaEvents: [],
compositeEvents: [],
task: {
tags: [],
customProperties: [],
app: { id: app.details.id },
name: arg.name,
taskType: 0,
enabled: true,
taskSessionTimeout: 1440,
maxRetries: 0,
},
};
const updateCommon = new UpdateCommonProperties(
this.#repoClient,
reloadTask,
arg
);
reloadTask = await updateCommon.updateAll();
// reloadTask.task.customProperties = reloadTask.customProperties;
// reloadTask.task.tags = (reloadTask as any).tags;
// delete (reloadTask as any).modifiedDate;
// delete (reloadTask as any).customProperties;
// delete (reloadTask as any).tags;
return await this.#repoClient
.Post<ITask>(`reloadtask/create`, { ...reloadTask })
.then((res) => res.data)
.then(async (t) => {
const rt = new ReloadTask(this.#repoClient, t.id, t);
await rt.init();
return rt;
});
}
}