Add dashboard widget functionality: Implement getDashboardWidget method in FalukantService to retrieve compact user data for the dashboard. Update FalukantController and router to expose the new endpoint, and enhance DashboardWidget component to display user-specific information including character name, gender, age, money, unread notifications, and children count.

This commit is contained in:
Torsten Schulz (local)
2026-01-29 17:03:32 +01:00
parent c09159d6ce
commit 62d8cd7b05
5 changed files with 105 additions and 2 deletions

View File

@@ -231,6 +231,7 @@ class FalukantController {
this.getNotifications = this._wrapWithUser((userId) => this.service.getNotifications(userId));
this.getAllNotifications = this._wrapWithUser((userId, req) => this.service.getAllNotifications(userId, req.query.page, req.query.size));
this.markNotificationsShown = this._wrapWithUser((userId) => this.service.markNotificationsShown(userId), { successStatus: 202 });
this.getDashboardWidget = this._wrapWithUser((userId) => this.service.getDashboardWidget(userId));
this.getUndergroundTargets = this._wrapWithUser((userId) => this.service.getPoliticalOfficeHolders(userId));
this.searchUsers = this._wrapWithUser((userId, req) => {

View File

@@ -11,6 +11,7 @@ router.get('/character/affect', falukantController.getCharacterAffect);
router.get('/name/randomfirstname/:gender', falukantController.randomFirstName);
router.get('/name/randomlastname', falukantController.randomLastName);
router.get('/info', falukantController.getInfo);
router.get('/dashboard-widget', falukantController.getDashboardWidget);
router.get('/branches/types', falukantController.getBranchTypes);
router.get('/branches/:branch', falukantController.getBranch);
router.get('/branches', falukantController.getBranches);

View File

@@ -4748,6 +4748,60 @@ ORDER BY r.id`,
return { updated: count };
}
/**
* Kompakte Daten für das Dashboard-Widget (Charakter-Name, Geschlecht, Alter, Geld, ungelesene Nachrichten, Kinder).
* @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({

View File

@@ -1,7 +1,8 @@
import WidgetType from '../models/type/widget_type.js';
const DEFAULT_WIDGET_TYPES = [
{ label: 'Termine', endpoint: '/api/termine', description: 'Bevorstehende Termine', orderId: 1 }
{ label: 'Termine', endpoint: '/api/termine', description: 'Bevorstehende Termine', orderId: 1 },
{ label: 'Falukant', endpoint: '/api/falukant/dashboard-widget', description: 'Charakter, Geld, Nachrichten, Kinder', orderId: 2 }
];
/**