/
Brand.ts
139 lines (124 loc) · 3.49 KB
/
Brand.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
import { QlikFormData, QlikSaaSClient } from "qlik-rest-api";
import { BrandActions } from "./BrandActions";
import { BrandFile, IBrandFile, contentTypeMime } from "./BrandFile";
export interface IBrand {
id: string;
active: boolean;
name: string;
description: string;
createdBy: string;
updatedBy: string;
createdAt: string;
updatedAt: string;
files: BrandFile[];
}
export interface IBrandResponse {
id: string;
active: boolean;
name: string;
description: string;
createdBy: string;
updatedBy: string;
createdAt: string;
updatedAt: string;
files: IBrandFile[];
}
export interface IBrandUpdate {
path: "name" | "description";
value: string;
op: "replace" | "add" | "renew";
}
export class Brand {
#id: string;
#saasClient: QlikSaaSClient;
details: IBrand;
_actions: BrandActions;
constructor(saasClient: QlikSaaSClient, id: string, details?: IBrand) {
if (!id) throw new Error(`brands.get: "id" parameter is required`);
this.details = details ?? ({} as IBrand);
this.#id = id;
this.#saasClient = saasClient;
this._actions = new BrandActions(this.#saasClient, this.#id);
}
async init(arg?: { force: true }) {
if (Object.keys(this.details).length == 0 || arg?.force == true) {
this.details = await this.#saasClient
.Get<IBrandResponse>(`brands/${this.#id}`)
.then((res) => {
const filesInstances = res.data.files.map(
(f) => new BrandFile(this.#saasClient, this.#id, f.id, f)
);
return {
active: res.data.active,
createdAt: res.data.createdAt,
createdBy: res.data.createdBy,
description: res.data.description,
id: res.data.id,
name: res.data.name,
updatedAt: res.data.updatedAt,
updatedBy: res.data.updatedBy,
files: filesInstances,
};
});
}
}
/**
* Deletes the current brand
*/
async remove() {
return await this.#saasClient
.Delete(`brands/${this.#id}`)
.then((res) => res.status);
}
/**
* Updates the name or the description of the current brand
*/
async update(arg: IBrandUpdate[]) {
if (!arg) throw new Error(`brand.update: update arguments are missing`);
const data = arg.map((a) => ({
path: `/${a.path}`,
value: a.value,
op: a.op,
}));
let updateStatus = -1;
return await this.#saasClient
.Patch(`brands/${this.#id}`, data)
.then((res) => {
updateStatus = res.status;
return this.init({ force: true });
})
.then(() => updateStatus);
}
/**
* Adds file to the existing brand
*/
async addFile(arg: {
/**
* The binary content of the file
*/
file: Buffer | string;
/**
* the possible options are: logo OR styles OR favIcon
*/
id: "logo" | "styles" | "favIcon";
}) {
if (arg.id != "logo" && arg.id != "styles" && arg.id != "favIcon")
throw new Error(
`brand.addFile: "id" parameter should be one of "logo" or "styles" or "favIcon". Provided was "${arg.id}"`
);
const fd = new QlikFormData();
fd.append({
data: arg.file,
field: "file",
contentType: contentTypeMime[arg.id],
});
let addFileStatus = 0;
return this.#saasClient
.Post(`brands/${this.#id}/files/${arg.id}`, fd.getData, fd.getHeaders)
.then((res) => {
addFileStatus = res.status;
return this.init({ force: true });
})
.then(() => addFileStatus);
}
}