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.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.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.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.getUndergroundTargets = this._wrapWithUser((userId) => this.service.getPoliticalOfficeHolders(userId));
this.searchUsers = this._wrapWithUser((userId, req) => { 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/randomfirstname/:gender', falukantController.randomFirstName);
router.get('/name/randomlastname', falukantController.randomLastName); router.get('/name/randomlastname', falukantController.randomLastName);
router.get('/info', falukantController.getInfo); router.get('/info', falukantController.getInfo);
router.get('/dashboard-widget', falukantController.getDashboardWidget);
router.get('/branches/types', falukantController.getBranchTypes); router.get('/branches/types', falukantController.getBranchTypes);
router.get('/branches/:branch', falukantController.getBranch); router.get('/branches/:branch', falukantController.getBranch);
router.get('/branches', falukantController.getBranches); router.get('/branches', falukantController.getBranches);

View File

@@ -4748,6 +4748,60 @@ ORDER BY r.id`,
return { updated: count }; 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) { async getPoliticalOfficeHolders(hashedUserId) {
const user = await getFalukantUserOrFail(hashedUserId); const user = await getFalukantUserOrFail(hashedUserId);
const character = await FalukantCharacter.findOne({ const character = await FalukantCharacter.findOne({

View File

@@ -1,7 +1,8 @@
import WidgetType from '../models/type/widget_type.js'; import WidgetType from '../models/type/widget_type.js';
const DEFAULT_WIDGET_TYPES = [ 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 }
]; ];
/** /**

View File

@@ -14,7 +14,23 @@
<div v-else-if="error" class="dashboard-widget__state dashboard-widget__error">{{ error }}</div> <div v-else-if="error" class="dashboard-widget__state dashboard-widget__error">{{ error }}</div>
<div v-else class="dashboard-widget__body"> <div v-else class="dashboard-widget__body">
<slot :data="data"> <slot :data="data">
<template v-if="dataList.length"> <template v-if="falukantData">
<dl class="dashboard-widget__falukant">
<dt>Name</dt>
<dd>{{ falukantData.characterName }}</dd>
<dt>Geschlecht</dt>
<dd>{{ falukantData.gender ?? '—' }}</dd>
<dt>Alter</dt>
<dd>{{ falukantData.age != null ? falukantData.age + ' Tage' : '—' }}</dd>
<dt>Geld</dt>
<dd>{{ formatMoney(falukantData.money) }}</dd>
<dt>Ungelesene Nachrichten</dt>
<dd>{{ falukantData.unreadNotificationsCount }}</dd>
<dt>Kinder</dt>
<dd>{{ falukantData.childrenCount }}</dd>
</dl>
</template>
<template v-else-if="dataList.length">
<ul class="dashboard-widget__list"> <ul class="dashboard-widget__list">
<li v-for="(item, i) in dataList" :key="i" class="dashboard-widget__list-item"> <li v-for="(item, i) in dataList" :key="i" class="dashboard-widget__list-item">
<span v-if="item.datum" class="dashboard-widget__date">{{ formatDatum(item.datum) }}</span> <span v-if="item.datum" class="dashboard-widget__date">{{ formatDatum(item.datum) }}</span>
@@ -53,6 +69,11 @@ export default {
}; };
}, },
computed: { computed: {
falukantData() {
const d = this.data;
if (d && typeof d === 'object' && 'characterName' in d && 'money' in d) return d;
return null;
},
dataList() { dataList() {
if (!Array.isArray(this.data) || this.data.length === 0) return []; if (!Array.isArray(this.data) || this.data.length === 0) return [];
const first = this.data[0]; const first = this.data[0];
@@ -110,6 +131,11 @@ export default {
const d = new Date(dateStr); const d = new Date(dateStr);
if (Number.isNaN(d.getTime())) return String(dateStr); if (Number.isNaN(d.getTime())) return String(dateStr);
return d.toLocaleDateString('de-DE', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' }); return d.toLocaleDateString('de-DE', { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' });
},
formatMoney(value) {
const n = Number(value);
if (Number.isNaN(n)) return '—';
return n.toLocaleString('de-DE');
} }
} }
}; };
@@ -215,4 +241,24 @@ export default {
color: #555; color: #555;
line-height: 1.4; line-height: 1.4;
} }
.dashboard-widget__falukant {
margin: 0;
display: grid;
grid-template-columns: auto 1fr;
gap: 4px 16px;
align-items: baseline;
}
.dashboard-widget__falukant dt {
margin: 0;
font-weight: 600;
color: #555;
font-size: 0.85rem;
}
.dashboard-widget__falukant dd {
margin: 0;
font-size: 0.9rem;
}
</style> </style>