11import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' ;
22import { validator , schema } from '@ioc:Adonis/Core/Validator' ;
3- import Workspace from 'App/Models/Workspace' ;
3+ import Workspace , { type CustomData } from 'App/Models/Workspace' ;
44import { v4 as uuid } from 'uuid' ;
55
66const createSchema = schema . create ( {
@@ -89,13 +89,24 @@ export default class WorkspaceController {
8989 const data = request . all ( ) ;
9090 const { id } = params ;
9191
92+ const currentWorkspace = await Workspace . query ( )
93+ . where ( 'workspaceId' , id )
94+ . where ( 'userId' , user . id )
95+ . firstOrFail ( ) ;
96+ const customData : CustomData = JSON . parse ( currentWorkspace . data || '{}' ) ;
97+ Object . assign ( customData , {
98+ name : data . name ,
99+ iconUrl : data . iconUrl ,
100+ } ) ;
101+
92102 // Update data in database
93103 await Workspace . query ( )
94104 . where ( 'workspaceId' , id )
95105 . where ( 'userId' , user . id )
96106 . update ( {
97107 name : data . name ,
98108 services : JSON . stringify ( data . services ) ,
109+ data : JSON . stringify ( customData ) ,
99110 } ) ;
100111
101112 // Get updated row
@@ -106,10 +117,11 @@ export default class WorkspaceController {
106117
107118 return response . send ( {
108119 id : workspace . workspaceId ,
109- name : data . name ,
120+ name : workspace . name ,
110121 order : workspace . order ,
111- services : data . services ,
122+ services : workspace . services ,
112123 userId : user . id ,
124+ iconUrl : customData . iconUrl ,
113125 } ) ;
114126 }
115127
@@ -165,19 +177,30 @@ export default class WorkspaceController {
165177
166178 const workspaces = await user . related ( 'workspaces' ) . query ( ) ;
167179 // Convert to array with all data Franz wants
168- let workspacesArray : object [ ] = [ ] ;
180+ let workspacesArray : Workspace [ ] = [ ] ;
169181 if ( workspaces ) {
170- // eslint-disable-next-line @typescript-eslint/no-explicit-any
171- workspacesArray = workspaces . map ( ( workspace : any ) => ( {
172- id : workspace . workspaceId ,
173- name : workspace . name ,
174- order : workspace . order ,
175- services :
176- typeof workspace . services === 'string'
177- ? JSON . parse ( workspace . services )
178- : workspace . services ,
179- userId : user . id ,
180- } ) ) ;
182+ workspacesArray = workspaces . map ( ( workspace : Workspace ) => {
183+ let data : CustomData = { } ;
184+ try {
185+ data = JSON . parse ( workspace . data ) ;
186+ } catch ( error ) {
187+ console . warn (
188+ `[WorkspaceController] list ${ workspace . workspaceId } . Error parsing data JSON` ,
189+ error ,
190+ ) ;
191+ }
192+ return {
193+ id : workspace . workspaceId ,
194+ name : workspace . name ,
195+ order : workspace . order ,
196+ services :
197+ typeof workspace . services === 'string'
198+ ? JSON . parse ( workspace . services )
199+ : workspace . services ,
200+ userId : user . id ,
201+ iconUrl : data . iconUrl ?? '' ,
202+ } ;
203+ } ) ;
181204 }
182205
183206 return response . send ( workspacesArray ) ;
0 commit comments