From c8d8254fc13d004c70b32a259bd35ef95ec11996 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Mon, 9 Feb 2026 15:05:17 +0100 Subject: [PATCH] Add dashboard widget endpoint: Implemented getDashboardWidget method in FalukantService and updated router to provide compact user data for the dashboard widget, ensuring frontend compatibility. --- backend/controllers/falukantController.js | 2 + backend/routers/falukantRouter.js | 4 +- backend/services/falukantService.js | 55 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/backend/controllers/falukantController.js b/backend/controllers/falukantController.js index 5c43c7b..f9af83b 100644 --- a/backend/controllers/falukantController.js +++ b/backend/controllers/falukantController.js @@ -26,6 +26,8 @@ class FalukantController { }, { successStatus: 201 }); this.getInfo = this._wrapWithUser((userId) => this.service.getInfo(userId)); + // Dashboard widget: originaler Endpoint (siehe Commit 62d8cd7) + this.getDashboardWidget = this._wrapWithUser((userId) => this.service.getDashboardWidget(userId)); this.getBranches = this._wrapWithUser((userId) => this.service.getBranches(userId)); this.createBranch = this._wrapWithUser((userId, req) => this.service.createBranch(userId, req.body.cityId, req.body.branchTypeId)); this.getBranchTypes = this._wrapWithUser((userId) => this.service.getBranchTypes(userId)); diff --git a/backend/routers/falukantRouter.js b/backend/routers/falukantRouter.js index 88e7a4e..b80d406 100644 --- a/backend/routers/falukantRouter.js +++ b/backend/routers/falukantRouter.js @@ -39,8 +39,8 @@ router.get('/directors', falukantController.getAllDirectors); router.post('/directors', falukantController.updateDirector); // Legacy endpoint (wurde in einem Refactor entfernt, wird aber von WidgetTypes/Frontend erwartet) -// Liefert die Daten, die im Dashboard-Falukant-Widget angezeigt werden. -router.get('/dashboard-widget', falukantController.getInfo); +// Liefert eine schlanke, frontend-kompatible Widget-Antwort (ohne hashedIds). +router.get('/dashboard-widget', falukantController.getDashboardWidget); router.post('/family/acceptmarriageproposal', falukantController.acceptMarriageProposal); router.post('/family/cancel-wooing', falukantController.cancelWooing); diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index c67f464..681a780 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -4435,6 +4435,61 @@ class FalukantService extends BaseService { return { updated: count }; } + /** + * Kompakte Daten für das Dashboard-Widget (Charakter-Name, Geschlecht, Alter, Geld, ungelesene Nachrichten, Kinder). + * (Originale Implementierung aus Commit 62d8cd7) + * @param {string} hashedUserId + * @returns {Promise<{ characterName: string, gender: string|null, age: number|null, money: number, unreadNotificationsCount: number, childrenCount: number }>} + */ + async getDashboardWidget(hashedUserId) { + const falukantUser = await FalukantUser.findOne({ + include: [ + { model: User, as: 'user', attributes: [], where: { hashedId: hashedUserId } }, + { + model: FalukantCharacter, + as: 'character', + attributes: ['id', 'birthdate', 'gender'], + include: [ + { model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }, + { model: FalukantPredefineLastname, as: 'definedLastName', attributes: ['name'] }, + { model: TitleOfNobility, as: 'nobleTitle', attributes: ['labelTr'] } + ] + } + ], + attributes: ['id', 'money'] + }); + if (!falukantUser || !falukantUser.character) { + throw new Error('No Falukant character found for this user'); + } + const character = falukantUser.character; + const firstName = character.definedFirstName?.name ?? ''; + const lastName = character.definedLastName?.name ?? ''; + const title = character.nobleTitle?.labelTr ?? ''; + const characterName = [title, firstName, lastName].filter(Boolean).join(' ') || '—'; + const age = character.birthdate ? calcAge(character.birthdate) : null; + + const [unreadNotificationsCount, childrenCount] = await Promise.all([ + Notification.count({ where: { userId: falukantUser.id, shown: false } }), + ChildRelation.count({ + where: { + [Op.or]: [ + { fatherCharacterId: character.id }, + { motherCharacterId: character.id } + ] + } + }) + ]); + + return { + characterName, + gender: character.gender ?? null, + age, + money: Number(falukantUser.money ?? 0), + unreadNotificationsCount, + childrenCount + }; + } + async getPoliticalOfficeHolders(hashedUserId) { const user = await getFalukantUserOrFail(hashedUserId); const character = await FalukantCharacter.findOne({