1313
1414import { NotFoundError } from '@aliceo2/web-ui' ;
1515import { BaseRepository } from './BaseRepository.js' ;
16+ import { addLabelsToLayout } from '../utils/layout/addLabelsToLayout.js' ;
17+ import { trimLayoutPerRequiredFields } from '../utils/layout/trimLayoutPerRequiredFields.js' ;
1618
1719/**
1820 * LayoutRepository class to handle CRUD operations for Layouts.
@@ -26,20 +28,18 @@ export class LayoutRepository extends BaseRepository {
2628 * @param {object } [options.filter] - Filter layouts by containing filter.objectPath, case insensitive
2729 * @returns {Array<object> } Array of layout objects matching the filters, containing only the specified fields
2830 */
29- listLayouts ( { name, fields = [ ] , filter } = { } ) {
31+ listLayouts ( { name, fields, filter } = { } ) {
3032 const { layouts } = this . _jsonFileService . data ;
3133 const filteredLayouts = this . _filterLayouts ( layouts , { ...filter , name } ) ;
3234
33- if ( fields . length === 0 ) {
34- return filteredLayouts ;
35- }
36- return filteredLayouts . map ( ( layout ) => {
37- const layoutObj = { } ;
38- fields . forEach ( ( field ) => {
39- layoutObj [ field ] = layout [ field ] ;
35+ const trimmedAndLabelledLayouts = filteredLayouts
36+ . map ( ( layout ) => {
37+ const labeledLayout = addLabelsToLayout ( layout ) ;
38+ const trimmedLayout = trimLayoutPerRequiredFields ( labeledLayout , fields ) ;
39+ return trimmedLayout ;
4040 } ) ;
41- return layoutObj ;
42- } ) ;
41+
42+ return trimmedAndLabelledLayouts ;
4343 }
4444
4545 /**
@@ -79,25 +79,27 @@ export class LayoutRepository extends BaseRepository {
7979 * @throws {NotFoundError } - if the layout is not found
8080 */
8181 readLayoutById ( layoutId ) {
82- const foundLayout = this . _jsonFileService . data . layouts . find ( ( layout ) => layout . id === layoutId ) ;
83- if ( ! foundLayout ) {
82+ const layout = this . _jsonFileService . data . layouts . find ( ( layout ) => layout . id === layoutId ) ;
83+ if ( ! layout ) {
8484 throw new NotFoundError ( `layout (${ layoutId } ) not found` ) ;
8585 }
86- return foundLayout ;
86+ const labeledLayout = addLabelsToLayout ( layout ) ;
87+ return labeledLayout ;
8788 }
8889
8990 /**
9091 * Given a string, representing layout name, retrieve the layout if it exists
9192 * @param {string } layoutName - name of the layout to retrieve
9293 * @returns {Layout } - object with layout information
93- * @throws
94+ * @throws { NotFoundError } - if the layout is not found
9495 */
9596 readLayoutByName ( layoutName ) {
9697 const layout = this . _jsonFileService . data . layouts . find ( ( layout ) => layout . name === layoutName ) ;
9798 if ( ! layout ) {
9899 throw new NotFoundError ( `Layout (${ layoutName } ) not found` ) ;
99100 }
100- return layout ;
101+ const labeledLayout = addLabelsToLayout ( layout ) ;
102+ return labeledLayout ;
101103 }
102104
103105 /**
@@ -127,9 +129,17 @@ export class LayoutRepository extends BaseRepository {
127129 * @param {string } layoutId - id of the layout to be updated
128130 * @param {LayoutDto } newData - layout new data
129131 * @returns {string } id of the layout updated
132+ * @throws {NotFoundError } - if the layout is not found
130133 */
131134 async updateLayout ( layoutId , newData ) {
132- const layout = this . readLayoutById ( layoutId ) ;
135+ if ( newData . labels ) {
136+ // labels are retrieved on front-end and might be send as PATCH/PUT if forgotten by developer
137+ delete newData . labels ;
138+ }
139+ const layout = this . _jsonFileService . data . layouts . find ( ( layout ) => layout . id === layoutId ) ;
140+ if ( ! layout ) {
141+ throw new NotFoundError ( `layout (${ layoutId } ) not found` ) ;
142+ }
133143 Object . assign ( layout , newData ) ;
134144 await this . _jsonFileService . writeToFile ( ) ;
135145 return layoutId ;
@@ -139,10 +149,13 @@ export class LayoutRepository extends BaseRepository {
139149 * Delete a single layout by its id
140150 * @param {string } layoutId - id of the layout to be removed
141151 * @returns {string } id of the layout deleted
152+ * @throws {NotFoundError } - if the layout is not found
142153 */
143154 async deleteLayout ( layoutId ) {
144- const layout = this . readLayoutById ( layoutId ) ;
145- const index = this . _jsonFileService . data . layouts . indexOf ( layout ) ;
155+ const index = this . _jsonFileService . data . layouts . findIndex ( ( layout ) => layout . id === layoutId ) ;
156+ if ( index === - 1 ) {
157+ throw new NotFoundError ( `layout (${ layoutId } ) not found` ) ;
158+ }
146159 this . _jsonFileService . data . layouts . splice ( index , 1 ) ;
147160 await this . _jsonFileService . writeToFile ( ) ;
148161 return layoutId ;
0 commit comments