Add dashboard widget endpoint: Implemented getDashboardWidget method in FalukantService and updated router to provide compact user data for the dashboard widget, ensuring frontend compatibility.

This commit is contained in:
Torsten Schulz (local)
2026-02-09 15:05:17 +01:00
parent 83455f1e83
commit c8d8254fc1
3 changed files with 59 additions and 2 deletions

View File

@@ -26,6 +26,8 @@ class FalukantController {
}, { successStatus: 201 }); }, { successStatus: 201 });
this.getInfo = this._wrapWithUser((userId) => this.service.getInfo(userId)); 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.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.createBranch = this._wrapWithUser((userId, req) => this.service.createBranch(userId, req.body.cityId, req.body.branchTypeId));
this.getBranchTypes = this._wrapWithUser((userId) => this.service.getBranchTypes(userId)); this.getBranchTypes = this._wrapWithUser((userId) => this.service.getBranchTypes(userId));

View File

@@ -39,8 +39,8 @@ router.get('/directors', falukantController.getAllDirectors);
router.post('/directors', falukantController.updateDirector); router.post('/directors', falukantController.updateDirector);
// Legacy endpoint (wurde in einem Refactor entfernt, wird aber von WidgetTypes/Frontend erwartet) // Legacy endpoint (wurde in einem Refactor entfernt, wird aber von WidgetTypes/Frontend erwartet)
// Liefert die Daten, die im Dashboard-Falukant-Widget angezeigt werden. // Liefert eine schlanke, frontend-kompatible Widget-Antwort (ohne hashedIds).
router.get('/dashboard-widget', falukantController.getInfo); router.get('/dashboard-widget', falukantController.getDashboardWidget);
router.post('/family/acceptmarriageproposal', falukantController.acceptMarriageProposal); router.post('/family/acceptmarriageproposal', falukantController.acceptMarriageProposal);
router.post('/family/cancel-wooing', falukantController.cancelWooing); router.post('/family/cancel-wooing', falukantController.cancelWooing);

View File

@@ -4435,6 +4435,61 @@ class FalukantService extends BaseService {
return { updated: count }; 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) { async getPoliticalOfficeHolders(hashedUserId) {
const user = await getFalukantUserOrFail(hashedUserId); const user = await getFalukantUserOrFail(hashedUserId);
const character = await FalukantCharacter.findOne({ const character = await FalukantCharacter.findOne({