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:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user