-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathschema.service.ts
More file actions
282 lines (233 loc) · 10.6 KB
/
schema.service.ts
File metadata and controls
282 lines (233 loc) · 10.6 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { HttpClient, HttpParams, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ISchema, ISchemaDeletionPreview, SchemaCategory, SchemaEntity, SchemaNode } from '@guardian/interfaces';
import { Observable } from 'rxjs';
import { API_BASE_URL } from './api';
import { AuthService } from './auth.service';
import { headersV2 } from '../constants';
type ITask = { taskId: string, expectation: number };
/**
* Services for working from Schemas.
*/
@Injectable()
export class SchemaService {
private readonly url: string = `${API_BASE_URL}/schemas`;
private readonly singleSchemaUrl: string = `${API_BASE_URL}/schema`;
constructor(
private http: HttpClient,
private auth: AuthService,
) {
}
public static getOptions(filters?: {
pageIndex?: number,
pageSize?: number | string,
[key: string]: any
}): HttpParams {
let params = new HttpParams();
if (filters && typeof filters === 'object') {
for (const key of Object.keys(filters)) {
if (filters[key] !== undefined && filters[key] !== null) {
if (key !== 'pageIndex' && key !== 'pageSize') {
params = params.set(key, filters[key]);
}
}
}
if (filters.pageSize === 'all') {
params = params.set('pageIndex', '0');
params = params.set('pageSize', 'all');
} else if (Number.isInteger(filters.pageIndex) && Number.isInteger(filters.pageSize)) {
params = params.set('pageIndex', String(filters.pageIndex));
params = params.set('pageSize', String(filters.pageSize));
}
}
return params;
}
public create(category: SchemaCategory, schema: ISchema, topicId: any): Observable<ISchema[]> {
schema.category = category;
return this.http.post<any[]>(`${this.url}/${topicId || null}`, schema);
}
public pushCreate(category: SchemaCategory, schema: ISchema, topicId: any): Observable<ITask> {
schema.category = category;
return this.http.post<ITask>(`${this.url}/push/${topicId || null}`, schema);
}
public update(schema: ISchema, id?: string): Observable<ISchema[]> {
const data = Object.assign({}, schema, { id: id || schema.id });
return this.http.put<any[]>(`${this.url}`, data);
}
public newVersion(category: SchemaCategory, schema: ISchema, id?: string): Observable<ITask> {
const data = Object.assign({}, schema, { id: id || schema.id });
schema.category = category;
return this.http.post<ITask>(`${this.url}/push/${data.topicId || null}`, data);
}
public list(): Observable<any[]> {
return this.http.get<any[]>(`${this.url}/list/all`);
}
public getSchemas(topicId?: string): Observable<ISchema[]> {
if (topicId) {
return this.http.get<ISchema[]>(`${this.url}/${topicId}`);
}
return this.http.get<ISchema[]>(`${this.url}`);
}
public getSchemaWithSubSchemas(
category: string,
schemaId?: string,
topicId?: string,
): Observable<Record<string, any>> {
let url = `${this.url}/schema-with-sub-schemas?category=${category}`;
if (schemaId) {
url += `&schemaId=${schemaId}`;
}
if (topicId) {
url += `&topicId=${topicId}`;
}
return this.http.get<any[]>(url);
}
public getSchemasByPolicy(policyId: string, pageIndex: number = 0, pageSize: number = 1000): Observable<ISchema[]> {
return this.http.get<ISchema[]>(`${this.url}?policyId=${policyId}&pageIndex=${pageIndex}&pageSize=${pageSize}`);
}
public getSchemasByPage(options?: {
category?: SchemaCategory,
topicId?: string,
search?: string,
searchOptions?: string[],
pageIndex?: number,
pageSize?: number | string,
}): Observable<HttpResponse<ISchema[]>> {
const params = SchemaService.getOptions(options);
return this.http.get<any>(`${this.url}`, { observe: 'response', headers: headersV2, params });
}
public getSchemasByType(type: string): Observable<ISchema> {
return this.http.get<ISchema>(`${this.url}/type/${type}`);
}
public getSchemasByTypeAndUser(type: string): Observable<ISchema> {
return this.http.get<ISchema>(`${this.url}/type-by-user/${type}`);
}
public publish(id: string, version: string): Observable<ISchema[]> {
return this.http.put<any[]>(`${this.url}/${id}/publish`, { version });
}
public pushPublish(id: string, version: string): Observable<ITask> {
return this.http.put<ITask>(`${this.url}/push/${id}/publish`, { version });
}
public unpublished(id: string): Observable<ISchema[]> {
return this.http.put<any[]>(`${this.url}/${id}/unpublish`, null);
}
public delete(id: string, includeChildren?: boolean): Observable<{ taskId: string, expectation: number }> {
return this.http.delete<{ taskId: string, expectation: number }>(`${this.url}/${id}`, {
params: {
includeChildren: includeChildren ? true : false
}
});
}
public deleteMultiple(schemaIds: string[], includeChildren?: boolean): Observable<{ taskId: string, expectation: number }> {
return this.http.post<{ taskId: string, expectation: number }>(`${this.url}/delete-multiple`, { schemaIds }, {
params: {
includeChildren: includeChildren ? true : false
}
});
}
public exportInFile(id: string): Observable<ArrayBuffer> {
return this.http.get(`${this.url}/${id}/export/file`, {
responseType: 'arraybuffer',
});
}
public exportInMessage(id: string): Observable<ISchema[]> {
return this.http.get<any[]>(`${this.url}/${id}/export/message`);
}
public pushImportByMessage(messageId: string, topicId: any, schemasForReplace?: string[]): Observable<ITask> {
var query = schemasForReplace?.length ? `?schemas=${schemasForReplace.join(',')}` : '';
return this.http.post<ITask>(`${this.url}/push/${topicId || null}/import/message${query}`, { messageId });
}
public pushImportByFile(schemasFile: any, topicId: any, schemasForReplace?: string[]): Observable<ITask> {
var query = schemasForReplace?.length ? `?schemas=${schemasForReplace.join(',')}` : '';
return this.http.post<ITask>(`${this.url}/push/${topicId || null}/import/file${query}`, schemasFile, {
headers: {
'Content-Type': 'binary/octet-stream',
},
});
}
public previewByMessage(messageId: string): Observable<ISchema> {
return this.http.post<any>(`${this.url}/import/message/preview`, { messageId });
}
public pushPreviewByMessage(messageId: string): Observable<ITask> {
return this.http.post<ITask>(`${this.url}/push/import/message/preview`, { messageId });
}
public previewByFile(schemasFile: any): Observable<ISchema[]> {
return this.http.post<any>(`${this.url}/import/file/preview`, schemasFile, {
headers: {
'Content-Type': 'binary/octet-stream',
},
});
}
public createSystemSchemas(schema: ISchema): Observable<ISchema> {
const username = encodeURIComponent(this.auth.getUsername());
return this.http.post<any>(`${this.url}/system/${username}`, schema);
}
public getSystemSchemas(options?: {
pageIndex?: number,
pageSize?: number
}): Observable<HttpResponse<ISchema[]>> {
const username = encodeURIComponent(this.auth.getUsername());
const url = `${this.url}/system/${username}`;
const params = SchemaService.getOptions(options);
return this.http.get<any>(url, { observe: 'response', headers: headersV2, params });
}
public deleteSystemSchema(id: string): Observable<any> {
return this.http.delete<any>(`${this.url}/system/${id}`);
}
public updateSystemSchema(schema: ISchema, id?: string): Observable<ISchema[]> {
const data = Object.assign({}, schema, { id: id || schema.id });
return this.http.put<any[]>(`${this.url}/system/${id}`, data);
}
public activeSystemSchema(id: string): Observable<any> {
return this.http.put<any>(`${this.url}/system/${id}/active`, null);
}
public getSystemSchemasByEntity(entity: SchemaEntity): Observable<ISchema> {
return this.http.get<ISchema>(`${this.url}/system/entity/${entity}`);
}
public copySchema(copyInfo: any) {
return this.http.post<ITask>(`${this.url}/push/copy`, copyInfo);
}
public getSchemaParents(id: string): Observable<ISchema[]> {
return this.http.get<ISchema[]>(`${this.singleSchemaUrl}/${id}/parents`);
}
public getSchemaTree(id: string): Observable<SchemaNode> {
return this.http.get<SchemaNode>(`${this.singleSchemaUrl}/${id}/tree`);
}
public getSchemaDeletionPreview(schemaIds: string[]): Observable<ISchemaDeletionPreview> {
return this.http.post<ISchemaDeletionPreview>(`${this.url}/deletionPreview`, { schemaIds });
}
public deleteSchemasByTopicId(topicId: string): Observable<any> {
return this.http.delete<any>(`${this.url}/topic/${topicId}`, {});
}
public properties(): Observable<any[]> {
return this.http.get<any>(`${API_BASE_URL}/projects/properties`);
}
public exportToExcel(id: string): Observable<ArrayBuffer> {
return this.http.get(`${this.url}/${id}/export/xlsx`, {
responseType: 'arraybuffer',
});
}
public downloadExcelExample(): Observable<ArrayBuffer> {
return this.http.get(`${this.url}/export/template`, {
responseType: 'arraybuffer',
});
}
public previewByXlsx(file: any): Observable<any> {
return this.http.post<any[]>(`${this.url}/import/xlsx/preview`, file, {
headers: {
'Content-Type': 'binary/octet-stream',
},
});
}
public checkForDublicates(data: { schemaNames: string[]; policyId: string }): Observable<any> {
return this.http.post<any>(`${this.url}/import/schemas/duplicates`, data);
}
public pushImportByXlsx(schemasFile: any, topicId: any, schemasForReplace?: string[]): Observable<{ taskId: string, expectation: number }> {
var query = schemasForReplace?.length ? `?schemas=${schemasForReplace.join(',')}` : '';
return this.http.post<ITask>(`${this.url}/push/${topicId || null}/import/xlsx${query}`, schemasFile, {
headers: {
'Content-Type': 'binary/octet-stream',
},
});
}
}