Enhance nobility ID validation in FalukantService

- Added checks to ensure that provided nobility IDs are valid and exist in the database, improving error handling and user feedback.
- Updated logic to use loaded nobility objects when adding invited nobilities to a party, optimizing database interactions.
This commit is contained in:
Torsten Schulz (local)
2025-12-05 20:49:12 +01:00
parent e756b3692d
commit b1d29f2083

View File

@@ -2821,9 +2821,15 @@ class FalukantService extends BaseService {
if (!ptype || !music || !banquette) { if (!ptype || !music || !banquette) {
throw new Error('Ungültige Party-, Musik- oder Bankett-Auswahl'); throw new Error('Ungültige Party-, Musik- oder Bankett-Auswahl');
} }
const nobilities = nobilityIds.length const nobilities = nobilityIds && nobilityIds.length
? await TitleOfNobility.findAll({ where: { id: { [Op.in]: nobilityIds } } }) ? await TitleOfNobility.findAll({ where: { id: { [Op.in]: nobilityIds } } })
: []; : [];
// Prüfe, ob alle angegebenen IDs gefunden wurden
if (nobilityIds && nobilityIds.length > 0 && nobilities.length !== nobilityIds.length) {
throw new Error('Einige ausgewählte Adelstitel existieren nicht');
}
let cost = (ptype.cost || 0) + (music.cost || 0) + (banquette.cost || 0); let cost = (ptype.cost || 0) + (music.cost || 0) + (banquette.cost || 0);
cost += (50 / servantRatio - 1) * 1000; cost += (50 / servantRatio - 1) * 1000;
const nobilityCost = nobilities.reduce((sum, n) => sum + ((n.id ^ 5) * 1000), 0); const nobilityCost = nobilities.reduce((sum, n) => sum + ((n.id ^ 5) * 1000), 0);
@@ -2848,8 +2854,9 @@ class FalukantService extends BaseService {
servantRatio, servantRatio,
cost: cost cost: cost
}); });
if (nobilityIds.length) { if (nobilities.length > 0) {
await party.addInvitedNobilities(nobilityIds); // Verwende die bereits geladenen Objekte
await party.addInvitedNobilities(nobilities);
} }
const user = await User.findByPk(falukantUser.userId); const user = await User.findByPk(falukantUser.userId);
notifyUser(user.hashedId, 'falukantPartyUpdate', { notifyUser(user.hashedId, 'falukantPartyUpdate', {