Skip to content

Commit 95b3939

Browse files
orgrimarrvraravam
authored andcommitted
feat: Store iconUrl in workspace.data
ferdium/ferdium-app#1240
1 parent ab1bcd9 commit 95b3939

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

app/Controllers/Http/WorkspaceController.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
22
import { validator, schema } from '@ioc:Adonis/Core/Validator';
3-
import Workspace from 'App/Models/Workspace';
3+
import Workspace, { type CustomData } from 'App/Models/Workspace';
44
import { v4 as uuid } from 'uuid';
55

66
const 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);

app/Models/Workspace.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ export default class Workspace extends BaseModel {
3131
public services: string;
3232

3333
@column()
34-
public data: string;
34+
public data: string; // JSON string that match type CustomData
3535

3636
@column.dateTime({ autoCreate: true })
3737
public createdAt: DateTime;
3838

3939
@column.dateTime({ autoCreate: true, autoUpdate: true })
4040
public updatedAt: DateTime;
4141
}
42+
43+
export interface CustomData {
44+
name?: string;
45+
iconUrl?: string;
46+
}

0 commit comments

Comments
 (0)