Skip to content

Commit 2cfc355

Browse files
committed
fix(device): include widget type and add widgetsCount on dashboard update
- Add widget 'type' field to the deviceRouter dashboard widgets mapping - Retrieve current dashboard with widgets to get widgetsCount before update - Throw 'NOT_FOUND' error if dashboard is missing during update - Return updated dashboard with widgetsCount included in response - Add 'type' field to DeviceInfoWidgetSchema for validation - Fix enhanced-logger functions to return null explicitly when no error found
1 parent 2a8a874 commit 2cfc355

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

web/src/server/trpc/routers/device.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TRPCError } from '@trpc/server';
22

33
import { prisma } from '../../prisma';
4-
import { DashboardSchema, DashboardType } from '../../types/DashboardSchema';
4+
import { DashboardSchema } from '../../types/DashboardSchema';
55
import {
66
DeviceInfoSchema,
77
DeviceLinkSchema,
@@ -132,8 +132,9 @@ export const deviceRouter = router({
132132
id: dashboard.id,
133133
name: dashboard.name,
134134
isBlackTheme: dashboard.isBlackTheme,
135-
widgets: dashboard.Widget.map(widget => ({
135+
widgets: dashboard.Widget.map((widget) => ({
136136
id: widget.id,
137+
type: widget.type,
137138
options: widget.options,
138139
state: widget.state,
139140
columnIndex: widget.columnIndex,
@@ -159,12 +160,34 @@ export const deviceRouter = router({
159160
message: 'Device ID not found!',
160161
});
161162
}
162-
return (await prisma.dashboard.update({
163+
// Get the current dashboard to get the widgetsCount
164+
const currentDashboard = await prisma.dashboard.findFirst({
165+
where: { deviceId: ctx.deviceId, userId: ctx.user.id },
166+
include: {
167+
Widget: true,
168+
},
169+
});
170+
171+
if (!currentDashboard) {
172+
throw new TRPCError({
173+
code: 'NOT_FOUND',
174+
message: 'Dashboard not found!',
175+
});
176+
}
177+
178+
// Update the dashboard
179+
const updatedDashboard = await prisma.dashboard.update({
163180
data: {
164181
isBlackTheme: input.isBlackTheme,
165182
updatedAt: new Date(),
166183
},
167184
where: { deviceId: ctx.deviceId, userId: ctx.user.id },
168-
})) satisfies DashboardType;
185+
});
186+
187+
// Return the dashboard with widgetsCount
188+
return {
189+
...updatedDashboard,
190+
widgetsCount: currentDashboard.Widget.length,
191+
};
169192
}),
170193
});

web/src/server/types/DeviceSchema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type DeviceLinkType = z.infer<typeof DeviceLinkSchema>;
1717
// Device info response schema
1818
export const DeviceInfoWidgetSchema = z.object({
1919
[WidgetScalarFieldEnum.id]: z.string().uuid(),
20+
[WidgetScalarFieldEnum.type]: z.string(),
2021
[WidgetScalarFieldEnum.options]: z.any().nullish(),
2122
[WidgetScalarFieldEnum.state]: z.any().nullish(),
2223
[WidgetScalarFieldEnum.columnIndex]: z.number().nullish(),
@@ -47,4 +48,4 @@ export const SaveDeviceSettingsSchema = z.object({
4748
[DashboardScalarFieldEnum.isBlackTheme]: z.boolean(),
4849
});
4950

50-
export type SaveDeviceSettingsType = z.infer<typeof SaveDeviceSettingsSchema>;
51+
export type SaveDeviceSettingsType = z.infer<typeof SaveDeviceSettingsSchema>;

web/src/server/utils/enhanced-logger.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export function ifThisIsAnObjectWithCauseThatsAZodErrorTRPCWrapsZod({
213213
}
214214
return { serverError, clientError };
215215
}
216+
return null;
216217
}
217218

218219
// standardErrorObjectHandlingErrorInstancesOrTRPCError
@@ -278,6 +279,7 @@ export function standardErrorObjectHandlingErrorInstancesOrTRPCError(
278279
},
279280
};
280281
}
282+
return null;
281283
}
282284

283285
// catchPrismaErrors
@@ -356,6 +358,7 @@ export function catchPrismaErrors(
356358
clientError,
357359
};
358360
}
361+
return null;
359362
}
360363

361364
// normalStructuredOrStringPayloads

0 commit comments

Comments
 (0)