From 91637ba7a3efa104777746d4a9b1fdca5c358cf6 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Sat, 14 Feb 2026 16:19:31 +0100 Subject: [PATCH] =?UTF-8?q?F=C3=BCge=20Validierung=20f=C3=BCr=20Geschenke?= =?UTF-8?q?=20in=20FalukantService=20hinzu=20und=20erstelle=20Skripte=20zu?= =?UTF-8?q?r=20Reparatur=20ung=C3=BCltiger=20Werte=20in=20PromotionalGift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/falukantService.js | 14 +++++- backend/tools/dumpGiftsDebug.js | 37 +++++++++++++++ backend/tools/repairPromotionalGifts.js | 61 +++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 backend/tools/dumpGiftsDebug.js create mode 100644 backend/tools/repairPromotionalGifts.js diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index 4fdc7a6..6482632 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -3119,8 +3119,18 @@ class FalukantService extends BaseService { characterTitleOfNobility = reloadChar?.titleOfNobility ?? lowestTitleOfNobility?.id ?? 1; } - return Promise.all(gifts.map(async gift => { - const value = typeof gift.value === 'number' ? gift.value : (gift.value ? Number(gift.value) : 0); + // Filtere Gifts ohne gültigen 'value' (0 oder fehlend) — solche sollten in der DB korrigiert werden + const validGifts = gifts.filter(g => Number(g.value) > 0); + const skipped = gifts.length - validGifts.length; + if (skipped > 0) { + console.warn(`getGifts: skipped ${skipped} promotional gifts with invalid value`); + for (const g of gifts) { + if (!(Number(g.value) > 0)) console.warn(` skipped gift id=${g.id} name=${g.name} value=${g.value}`); + } + } + + return Promise.all(validGifts.map(async gift => { + const value = Number(gift.value); const cost = await this.getGiftCost(value, characterTitleOfNobility, lowestTitleOfNobility?.id ?? 1); return { id: gift.id, diff --git a/backend/tools/dumpGiftsDebug.js b/backend/tools/dumpGiftsDebug.js new file mode 100644 index 0000000..95cc193 --- /dev/null +++ b/backend/tools/dumpGiftsDebug.js @@ -0,0 +1,37 @@ +import { sequelize } from '../utils/sequelize.js'; +import PromotionalGift from '../models/falukant/type/promotional_gift.js'; +import PromotionalGiftMood from '../models/falukant/predefine/promotional_gift_mood.js'; +import PromotionalGiftCharacterTrait from '../models/falukant/predefine/promotional_gift_character_trait.js'; + +async function dump() { + try { + await sequelize.authenticate(); + console.log('DB connected'); + + const gifts = await PromotionalGift.findAll({ + include: [ + { model: PromotionalGiftMood, as: 'promotionalgiftmoods', attributes: ['moodId', 'suitability'], required: false }, + { model: PromotionalGiftCharacterTrait, as: 'characterTraits', attributes: ['traitId', 'suitability'], required: false } + ] + }); + + console.log(`found ${gifts.length} gifts`); + for (const g of gifts) { + console.log('---'); + console.log('id:', g.id, 'name:', g.name, 'raw value type:', typeof g.value, 'value:', g.value); + try { + const plain = g.get({ plain: true }); + console.log('plain value:', JSON.stringify(plain)); + } catch (e) { + console.log('could not stringify plain', e); + } + } + } catch (err) { + console.error('dump failed', err); + process.exit(2); + } finally { + await sequelize.close(); + } +} + +dump(); diff --git a/backend/tools/repairPromotionalGifts.js b/backend/tools/repairPromotionalGifts.js new file mode 100644 index 0000000..aea2c0c --- /dev/null +++ b/backend/tools/repairPromotionalGifts.js @@ -0,0 +1,61 @@ +import PromotionalGift from '../models/falukant/type/promotional_gift.js'; +import { sequelize } from '../utils/sequelize.js'; + +// Mapping basierend auf initializeFalukantTypes.js +const seedValues = { + 'Gold Coin': 100, + 'Silk Scarf': 50, + 'Exotic Perfume': 200, + 'Crystal Pendant': 150, + 'Leather Journal': 75, + 'Fine Wine': 120, + 'Artisan Chocolate': 40, + 'Pearl Necklace': 300, + 'Rare Painting': 500, + 'Silver Watch': 250, + 'Cat': 70, + 'Dog': 150, + 'Horse': 1000 +}; + +async function repair() { + console.log('Repair promotional_gift values - starting'); + try { + await sequelize.authenticate(); + console.log('DB connection ok'); + + // Liste aller problematischen Einträge + const [rows] = await sequelize.query("SELECT id, name, value FROM falukant_type.promotional_gift WHERE value IS NULL OR value <= 0"); + if (!rows.length) { + console.log('No invalid promotional_gift rows found. Nothing to do.'); + return process.exit(0); + } + + console.log(`Found ${rows.length} invalid promotional_gift rows:`); + for (const r of rows) console.log(` id=${r.id} name='${r.name}' value=${r.value}`); + + // Update rows where we have a seed mapping + let updated = 0; + for (const r of rows) { + const seed = seedValues[r.name]; + if (seed && Number(seed) > 0) { + await PromotionalGift.update({ value: seed }, { where: { id: r.id } }); + console.log(` updated id=${r.id} name='${r.name}' -> value=${seed}`); + updated++; + } else { + console.warn(` no seed value for id=${r.id} name='${r.name}' - skipping`); + } + } + + console.log(`Done. Updated ${updated} rows. Remaining invalid: `); + const [left] = await sequelize.query("SELECT id, name, value FROM falukant_type.promotional_gift WHERE value IS NULL OR value <= 0"); + for (const l of left) console.log(` id=${l.id} name='${l.name}' value=${l.value}`); + console.log('If any remain, inspect and adjust manually.'); + process.exit(0); + } catch (err) { + console.error('Repair failed:', err); + process.exit(2); + } +} + +repair();