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:
@@ -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,28 +4183,38 @@ class FalukantService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getProductPriceInRegion(hashedUserId, productId, regionId) {
|
async getProductPriceInRegion(hashedUserId, productId, regionId) {
|
||||||
const user = await this.getFalukantUserByHashedId(hashedUserId);
|
try {
|
||||||
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
|
const user = await this.getFalukantUserByHashedId(hashedUserId);
|
||||||
if (!character) {
|
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
|
||||||
throw new Error(`No FalukantCharacter found for user with id ${user.id}`);
|
if (!character) {
|
||||||
|
throw new Error(`No FalukantCharacter found for user with id ${user.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Produkt abrufen
|
||||||
|
const product = await ProductType.findOne({ where: { id: productId } });
|
||||||
|
if (!product) {
|
||||||
|
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
|
||||||
|
const knowledge = await Knowledge.findOne({
|
||||||
|
where: { characterId: character.id, productId: productId }
|
||||||
|
});
|
||||||
|
const knowledgeFactor = knowledge?.knowledge || 0;
|
||||||
|
|
||||||
|
// Verwende die bereits existierende calcRegionalSellPrice Funktion
|
||||||
|
const price = await calcRegionalSellPrice(product, knowledgeFactor, regionId);
|
||||||
|
|
||||||
|
return { price };
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[getProductPriceInRegion] Error for productId=${productId}, regionId=${regionId}:`, error);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Produkt abrufen
|
|
||||||
const product = await ProductType.findOne({ where: { id: productId } });
|
|
||||||
if (!product) {
|
|
||||||
throw new Error(`Product not found with id ${productId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Knowledge für dieses Produkt abrufen
|
|
||||||
const knowledge = await Knowledge.findOne({
|
|
||||||
where: { characterId: character.id, productId: productId }
|
|
||||||
});
|
|
||||||
const knowledgeFactor = knowledge?.knowledge || 0;
|
|
||||||
|
|
||||||
// Verwende die bereits existierende calcRegionalSellPrice Funktion
|
|
||||||
const price = await calcRegionalSellPrice(product, knowledgeFactor, regionId);
|
|
||||||
|
|
||||||
return { price };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProductPricesInCities(hashedUserId, productId, currentPrice, currentRegionId = null) {
|
async getProductPricesInCities(hashedUserId, productId, currentPrice, currentRegionId = null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user