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:
@@ -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)];
|
||||
// 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 } });
|
||||
if (!fnObj) {
|
||||
fnObj = await FalukantPredefineFirstname.create({ name: firstName, gender });
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user