system now uses seperate pricing.
All checks were successful
Build Frontend / Build Frontend (push) Successful in 24s

This commit is contained in:
2026-01-11 03:24:54 +00:00
parent b686acee8f
commit 7a32454b83
6 changed files with 718 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import axios from "axios";
import Item from "../models/Item.js";
import MarketPrice from "../models/MarketPrice.js";
/**
* Pricing Service
@@ -275,15 +276,177 @@ class PricingService {
}
/**
* Update prices for all games
* Update MarketPrice reference database for a specific game
* Fetches all items from Steam market and updates the reference database
* @param {string} game - Game identifier ('cs2' or 'rust')
* @returns {Promise<Object>} - Update statistics
*/
async updateMarketPriceDatabase(game) {
console.log(
`\n🔄 Updating MarketPrice reference database for ${game.toUpperCase()}...`
);
try {
const appId = this.appIds[game];
if (!appId) {
throw new Error(`Invalid game: ${game}`);
}
if (!this.apiKey) {
throw new Error("Steam API key not configured");
}
// Fetch all market items from Steam API
console.log(`📡 Fetching market data from Steam API...`);
const response = await axios.get(
`${this.baseUrl}/market/items/${appId}`,
{
params: { api_key: this.apiKey },
timeout: 60000,
}
);
if (!response.data || !response.data.data) {
throw new Error("No data returned from Steam API");
}
const items = response.data.data;
const itemCount = Object.keys(items).length;
console.log(`✅ Received ${itemCount} items from API`);
let inserted = 0;
let updated = 0;
let skipped = 0;
let errors = 0;
// Process items in batches
const bulkOps = [];
for (const item of Object.values(items)) {
try {
// Get the best available price
const price =
item.prices?.safe ||
item.prices?.median ||
item.prices?.mean ||
item.prices?.avg ||
item.prices?.latest;
if (!price || price <= 0) {
skipped++;
continue;
}
const marketHashName = item.market_hash_name || item.market_name;
const marketName = item.market_name || item.market_hash_name;
if (!marketHashName || !marketName) {
skipped++;
continue;
}
// Determine which price type was used
let priceType = "safe";
if (item.prices?.safe) priceType = "safe";
else if (item.prices?.median) priceType = "median";
else if (item.prices?.mean) priceType = "mean";
else if (item.prices?.avg) priceType = "avg";
else if (item.prices?.latest) priceType = "latest";
bulkOps.push({
updateOne: {
filter: { marketHashName: marketHashName },
update: {
$set: {
name: marketName,
game: game,
appId: appId,
marketHashName: marketHashName,
price: price,
priceType: priceType,
image: item.image || null,
borderColor: item.border_color || null,
nameId: item.nameID || null,
lastUpdated: new Date(),
},
},
upsert: true,
},
});
// Execute in batches of 1000
if (bulkOps.length >= 1000) {
const result = await MarketPrice.bulkWrite(bulkOps);
inserted += result.upsertedCount;
updated += result.modifiedCount;
console.log(
` 📦 Batch: ${inserted} inserted, ${updated} updated`
);
bulkOps.length = 0;
}
} catch (err) {
errors++;
}
}
// Execute remaining items
if (bulkOps.length > 0) {
const result = await MarketPrice.bulkWrite(bulkOps);
inserted += result.upsertedCount;
updated += result.modifiedCount;
}
console.log(`✅ MarketPrice update complete for ${game.toUpperCase()}:`);
console.log(` 📥 Inserted: ${inserted}`);
console.log(` 🔄 Updated: ${updated}`);
console.log(` ⏭️ Skipped: ${skipped}`);
if (errors > 0) {
console.log(` ❌ Errors: ${errors}`);
}
return {
success: true,
game,
total: itemCount,
inserted,
updated,
skipped,
errors,
};
} catch (error) {
console.error(
`❌ Error updating MarketPrice for ${game}:`,
error.message
);
return {
success: false,
game,
error: error.message,
total: 0,
inserted: 0,
updated: 0,
skipped: 0,
errors: 1,
};
}
}
/**
* Update prices for all games (both Item and MarketPrice databases)
* @returns {Promise<Object>} - Combined update statistics
*/
async updateAllPrices() {
console.log("🔄 Starting price update for all games...");
const results = {
cs2: await this.updateDatabasePrices("cs2"),
rust: await this.updateDatabasePrices("rust"),
marketPrices: {
cs2: await this.updateMarketPriceDatabase("cs2"),
rust: await this.updateMarketPriceDatabase("rust"),
},
itemPrices: {
cs2: await this.updateDatabasePrices("cs2"),
rust: await this.updateDatabasePrices("rust"),
},
timestamp: new Date(),
};