diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index 2017ad9..7e5a56e 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -417,7 +417,7 @@ class FalukantService extends BaseService { model: RegionData, as: 'mainBranchRegion', include: [{ model: RegionType, as: 'regionType' }], - attributes: ['name'] + attributes: ['id', 'name'] }, { model: Branch, @@ -3003,53 +3003,67 @@ class FalukantService extends BaseService { const genders = ['male', 'female']; const createdHeirs = []; - for (let i = 0; i < 5; i++) { - // Zufälliges Geschlecht - const gender = genders[Math.floor(Math.random() * genders.length)]; - - // Zufälliger Vorname für das Geschlecht - const firstName = await this.randomFirstName(gender); - let fnObj = await FalukantPredefineFirstname.findOne({ where: { name: firstName, gender } }); - if (!fnObj) { - fnObj = await FalukantPredefineFirstname.create({ name: firstName, gender }); + // Erstelle neue Charaktere in einer Transaktion + await sequelize.transaction(async (t) => { + for (let i = 0; i < 5; i++) { + // Zufälliges Geschlecht + const gender = genders[Math.floor(Math.random() * genders.length)]; + + // Zufälliger Vorname für das Geschlecht + const firstName = await this.randomFirstName(gender); + let fnObj = await FalukantPredefineFirstname.findOne({ + where: { name: firstName, gender }, + transaction: t + }); + if (!fnObj) { + fnObj = await FalukantPredefineFirstname.create({ + name: firstName, gender + }, { transaction: t }); + } + + // Zufälliger Nachname + const lastName = await this.randomLastName(); + let lnObj = await FalukantPredefineLastname.findOne({ + where: { name: lastName }, + transaction: t + }); + if (!lnObj) { + lnObj = await FalukantPredefineLastname.create({ + name: lastName + }, { transaction: t }); + } + + // Zufälliges Alter zwischen 10 und 14 Jahren (in Tagen) + const randomAge = Math.floor(Math.random() * 5) + 10; // 10-14 + const birthdate = new Date(now); + birthdate.setDate(birthdate.getDate() - randomAge); + + // Erstelle den Charakter + const newCharacter = await FalukantCharacter.create({ + userId: null, // Wichtig: noch keinem User zugeordnet + regionId: user.mainBranchRegionId, + firstName: fnObj.id, + lastName: lnObj.id, + gender: gender, + birthdate: birthdate, + titleOfNobility: noncivilTitle.id, + health: 100, + moodId: 1 + }, { transaction: t }); + + // Lade die Namen für die Rückgabe + const heirWithNames = await FalukantCharacter.findOne({ + where: { id: newCharacter.id }, + include: [ + { model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }, + { model: FalukantPredefineLastname, as: 'definedLastName', attributes: ['name'] } + ], + transaction: t + }); + + createdHeirs.push(heirWithNames); } - - // Zufälliger Nachname - const lastName = await this.randomLastName(); - let lnObj = await FalukantPredefineLastname.findOne({ where: { name: lastName } }); - if (!lnObj) { - lnObj = await FalukantPredefineLastname.create({ name: lastName }); - } - - // Zufälliges Alter zwischen 10 und 14 Jahren (in Tagen) - const randomAge = Math.floor(Math.random() * 5) + 10; // 10-14 - const birthdate = new Date(now); - birthdate.setDate(birthdate.getDate() - randomAge); - - // Erstelle den Charakter - const newCharacter = await FalukantCharacter.create({ - userId: null, // Wichtig: noch keinem User zugeordnet - regionId: user.mainBranchRegionId, - firstName: fnObj.id, - lastName: lnObj.id, - gender: gender, - birthdate: birthdate, - titleOfNobility: noncivilTitle.id, - health: 100, - moodId: 1 - }); - - // Lade die Namen für die Rückgabe - const heirWithNames = await FalukantCharacter.findOne({ - where: { id: newCharacter.id }, - include: [ - { model: FalukantPredefineFirstname, as: 'definedFirstName', attributes: ['name'] }, - { model: FalukantPredefineLastname, as: 'definedLastName', attributes: ['name'] } - ] - }); - - createdHeirs.push(heirWithNames); - } + }); potentialHeirs = createdHeirs; } diff --git a/frontend/src/views/falukant/OverviewView.vue b/frontend/src/views/falukant/OverviewView.vue index b968f9b..472c18a 100644 --- a/frontend/src/views/falukant/OverviewView.vue +++ b/frontend/src/views/falukant/OverviewView.vue @@ -339,13 +339,23 @@ export default { return new Date(timestamp).toLocaleString(); }, async fetchPotentialHeirs() { - if (!this.falukantUser?.mainBranchRegion?.id) return; + // Prüfe sowohl mainBranchRegion.id als auch mainBranchRegionId + const regionId = this.falukantUser?.mainBranchRegion?.id || this.falukantUser?.mainBranchRegionId; + if (!regionId) { + console.error('No main branch region found', this.falukantUser); + this.potentialHeirs = []; + return; + } this.loadingHeirs = true; try { const response = await apiClient.get('/api/falukant/heirs/potential'); this.potentialHeirs = response.data || []; + if (this.potentialHeirs.length === 0) { + console.warn('No potential heirs returned from API'); + } } catch (error) { console.error('Error fetching potential heirs:', error); + console.error('Error details:', error.response?.data || error.message); this.potentialHeirs = []; } finally { this.loadingHeirs = false;