-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLayoutRepository.js
More file actions
184 lines (172 loc) · 6.62 KB
/
LayoutRepository.js
File metadata and controls
184 lines (172 loc) · 6.62 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
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import { NotFoundError } from '@aliceo2/web-ui';
import { BaseRepository } from './BaseRepository.js';
import { addLabelsToLayout } from '../utils/layout/addLabelsToLayout.js';
import { trimLayoutPerRequiredFields } from '../utils/layout/trimLayoutPerRequiredFields.js';
/**
* LayoutRepository class to handle CRUD operations for Layouts.
*/
export class LayoutRepository extends BaseRepository {
/**
* Retrieves a filtered list of layouts with optional field selection
* @param {object} [options] - Filtering and field selection options
* @param {string} [options.name] - Filter layouts by exact name match
* @param {Array<string>} [options.fields] - Array of field names to include in each returned layout object
* @param {object} [options.filter] - Filter layouts by containing filter.objectPath, case insensitive
* @returns {Array<object>} Array of layout objects matching the filters, containing only the specified fields
*/
listLayouts({ name, fields, filter } = {}) {
const { layouts } = this._jsonFileService.data;
const filteredLayouts = this._filterLayouts(layouts, { ...filter, name });
const trimmedAndLabelledLayouts = filteredLayouts
.map((layout) => {
const labeledLayout = addLabelsToLayout(layout);
const trimmedLayout = trimLayoutPerRequiredFields(labeledLayout, fields);
return trimmedLayout;
});
return trimmedAndLabelledLayouts;
}
/**
* Filters layouts by filter object
* @param {Array<object>} layouts - Array of layouts to filter
* @param {object} filter - Filtering object
* @param {number} [filter.owner_id] - owner id to filter by
* @param {string} [filter.name] - name to filter by
* @param {string} [filter.objectPath] - object path prefix for potential objects to be contained by layout
* @returns {Array<object>} Filtered layouts.
*/
_filterLayouts(layouts, { owner_id, name, objectPath } = {}) {
const objectPathLowerCase = objectPath?.toLowerCase();
return layouts.filter((layout) => {
if (owner_id !== undefined && layout.owner_id !== owner_id) {
return false;
}
if (name !== undefined && layout.name !== name) {
return false;
}
if (objectPathLowerCase) {
const hasMatchingObject = layout.tabs?.some((tab) =>
tab.objects?.some((obj) =>
obj.name?.toLowerCase().includes(objectPathLowerCase)));
if (!hasMatchingObject) {
return false;
}
}
return true;
});
}
/**
* Retrieve a layout by its id or throws an error
* @param {string} layoutId - layout id
* @returns {Layout} - layout object
* @throws {NotFoundError} - if the layout is not found
*/
readLayoutById(layoutId) {
const layout = this._jsonFileService.data.layouts.find((layout) => layout.id === layoutId);
if (!layout) {
throw new NotFoundError(`layout (${layoutId}) not found`);
}
const labeledLayout = addLabelsToLayout(layout);
return labeledLayout;
}
/**
* Given a string, representing layout name, retrieve the layout if it exists
* @param {string} layoutName - name of the layout to retrieve
* @returns {Layout} - object with layout information
* @throws {NotFoundError} - if the layout is not found
*/
readLayoutByName(layoutName) {
const layout = this._jsonFileService.data.layouts.find((layout) => layout.name === layoutName);
if (!layout) {
throw new NotFoundError(`Layout (${layoutName}) not found`);
}
const labeledLayout = addLabelsToLayout(layout);
return labeledLayout;
}
/**
* Create a layout
* @param {Layout} newLayout - layout object to be saved
* @returns {object} Empty details
*/
async createLayout(newLayout) {
if (!newLayout.id) {
throw new Error('layout id is mandatory');
}
if (!newLayout.name) {
throw new Error('layout name is mandatory');
}
const layout = this._jsonFileService.data.layouts.find((layout) => layout.id === newLayout.id);
if (layout) {
throw new Error(`layout with this id (${layout.id}) already exists`);
}
this._jsonFileService.data.layouts.push(newLayout);
await this._jsonFileService.writeToFile();
return newLayout;
}
/**
* Update a single layout by its id
* @param {string} layoutId - id of the layout to be updated
* @param {LayoutDto} newData - layout new data
* @returns {string} id of the layout updated
* @throws {NotFoundError} - if the layout is not found
*/
async updateLayout(layoutId, newData) {
if (newData.labels) {
// labels are retrieved on front-end and might be send as PATCH/PUT if forgotten by developer
delete newData.labels;
}
const layout = this._jsonFileService.data.layouts.find((layout) => layout.id === layoutId);
if (!layout) {
throw new NotFoundError(`layout (${layoutId}) not found`);
}
Object.assign(layout, newData);
await this._jsonFileService.writeToFile();
return layoutId;
}
/**
* Delete a single layout by its id
* @param {string} layoutId - id of the layout to be removed
* @returns {string} id of the layout deleted
* @throws {NotFoundError} - if the layout is not found
*/
async deleteLayout(layoutId) {
const index = this._jsonFileService.data.layouts.findIndex((layout) => layout.id === layoutId);
if (index === -1) {
throw new NotFoundError(`layout (${layoutId}) not found`);
}
this._jsonFileService.data.layouts.splice(index, 1);
await this._jsonFileService.writeToFile();
return layoutId;
}
/**
* Return an object by its id that is saved within a layout
* @param {string} id - id of the object to retrieve
* @returns {{object: object, layoutName: string}} - object configuration stored
*/
getObjectById(id) {
if (!id) {
throw new Error('Missing mandatory parameter: id');
}
for (const layout of this._jsonFileService.data.layouts) {
for (const tab of layout.tabs) {
for (const object of tab.objects) {
if (object.id === id) {
return { object, layoutName: layout.name, tabName: tab.name };
}
}
}
}
throw new Error(`Object with ${id} could not be found`);
}
}