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,
|
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
|
||||||
// Zufälliger Vorname für das Geschlecht
|
const gender = genders[Math.floor(Math.random() * genders.length)];
|
||||||
const firstName = await this.randomFirstName(gender);
|
|
||||||
let fnObj = await FalukantPredefineFirstname.findOne({ where: { name: firstName, gender } });
|
// Zufälliger Vorname für das Geschlecht
|
||||||
if (!fnObj) {
|
const firstName = await this.randomFirstName(gender);
|
||||||
fnObj = await FalukantPredefineFirstname.create({ name: firstName, 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;
|
potentialHeirs = createdHeirs;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user