Enhance error handling in FalukantService: Add validation for product sellCost in calcRegionalSellPrice and getProductPriceInRegion methods, ensuring proper error messages are logged when sellCost is undefined or null.

This commit is contained in:
Torsten Schulz (local)
2026-01-28 15:05:28 +01:00
parent 71b4a02592
commit 0c407b81b7

View File

@@ -101,6 +101,11 @@ async function calcRegionalSellPrice(product, knowledgeFactor, regionId, worthPe
worthPercent = townWorth?.worthPercent || 50; // Default 50% wenn nicht gefunden worthPercent = townWorth?.worthPercent || 50; // Default 50% wenn nicht gefunden
} }
// Prüfe ob sellCost vorhanden ist
if (product.sellCost === null || product.sellCost === undefined) {
throw new Error(`Product ${product.id} has no sellCost defined`);
}
// Basispreis basierend auf regionalem worthPercent // Basispreis basierend auf regionalem worthPercent
const basePrice = product.sellCost * (worthPercent / 100); const basePrice = product.sellCost * (worthPercent / 100);
@@ -4178,6 +4183,7 @@ class FalukantService extends BaseService {
} }
async getProductPriceInRegion(hashedUserId, productId, regionId) { async getProductPriceInRegion(hashedUserId, productId, regionId) {
try {
const user = await this.getFalukantUserByHashedId(hashedUserId); const user = await this.getFalukantUserByHashedId(hashedUserId);
const character = await FalukantCharacter.findOne({ where: { userId: user.id } }); const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
if (!character) { if (!character) {
@@ -4190,6 +4196,11 @@ class FalukantService extends BaseService {
throw new Error(`Product not found with id ${productId}`); throw new Error(`Product not found with id ${productId}`);
} }
// Prüfe ob sellCost vorhanden ist
if (product.sellCost === null || product.sellCost === undefined) {
throw new Error(`Product ${productId} has no sellCost defined`);
}
// Knowledge für dieses Produkt abrufen // Knowledge für dieses Produkt abrufen
const knowledge = await Knowledge.findOne({ const knowledge = await Knowledge.findOne({
where: { characterId: character.id, productId: productId } where: { characterId: character.id, productId: productId }
@@ -4200,6 +4211,10 @@ class FalukantService extends BaseService {
const price = await calcRegionalSellPrice(product, knowledgeFactor, regionId); const price = await calcRegionalSellPrice(product, knowledgeFactor, regionId);
return { price }; return { price };
} catch (error) {
console.error(`[getProductPriceInRegion] Error for productId=${productId}, regionId=${regionId}:`, error);
throw error;
}
} }
async getProductPricesInCities(hashedUserId, productId, currentPrice, currentRegionId = null) { async getProductPricesInCities(hashedUserId, productId, currentPrice, currentRegionId = null) {