Refactor character creation and heir fetching logic in FalukantService and OverviewView

- Updated character creation process to utilize transactions for improved data integrity during heir generation.
- Enhanced heir fetching logic in OverviewView to check for both mainBranchRegion.id and mainBranchRegionId, adding error handling for missing regions.
- Added warnings for empty heir responses from the API to improve debugging and user feedback.
This commit is contained in:
Torsten Schulz (local)
2026-01-07 11:20:03 +01:00
parent 75dbd78da1
commit d42e1da14b
2 changed files with 72 additions and 48 deletions

View File

@@ -417,7 +417,7 @@ class FalukantService extends BaseService {
model: RegionData, model: RegionData,
as: 'mainBranchRegion', as: 'mainBranchRegion',
include: [{ model: RegionType, as: 'regionType' }], include: [{ model: RegionType, as: 'regionType' }],
attributes: ['name'] attributes: ['id', 'name']
}, },
{ {
model: Branch, model: Branch,
@@ -3003,53 +3003,67 @@ class FalukantService extends BaseService {
const genders = ['male', 'female']; const genders = ['male', 'female'];
const createdHeirs = []; const createdHeirs = [];
for (let i = 0; i < 5; i++) { // Erstelle neue Charaktere in einer Transaktion
// Zufälliges Geschlecht await sequelize.transaction(async (t) => {
const gender = genders[Math.floor(Math.random() * genders.length)]; 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 // Zufälliger Vorname für das Geschlecht
const firstName = await this.randomFirstName(gender); const firstName = await this.randomFirstName(gender);
let fnObj = await FalukantPredefineFirstname.findOne({ where: { name: firstName, gender } }); let fnObj = await FalukantPredefineFirstname.findOne({
if (!fnObj) { where: { name: firstName, gender },
fnObj = await FalukantPredefineFirstname.create({ 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; potentialHeirs = createdHeirs;
} }

View File

@@ -339,13 +339,23 @@ export default {
return new Date(timestamp).toLocaleString(); return new Date(timestamp).toLocaleString();
}, },
async fetchPotentialHeirs() { 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; this.loadingHeirs = true;
try { try {
const response = await apiClient.get('/api/falukant/heirs/potential'); const response = await apiClient.get('/api/falukant/heirs/potential');
this.potentialHeirs = response.data || []; this.potentialHeirs = response.data || [];
if (this.potentialHeirs.length === 0) {
console.warn('No potential heirs returned from API');
}
} catch (error) { } catch (error) {
console.error('Error fetching potential heirs:', error); console.error('Error fetching potential heirs:', error);
console.error('Error details:', error.response?.data || error.message);
this.potentialHeirs = []; this.potentialHeirs = [];
} finally { } finally {
this.loadingHeirs = false; this.loadingHeirs = false;