|
@@ -88,9 +88,11 @@ class CandleController {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
if (!candle) {
|
|
if (!candle) {
|
|
|
- const error = new Error('No candle data found for this symbol');
|
|
|
|
|
- error.statusCode = 404;
|
|
|
|
|
- return next(error);
|
|
|
|
|
|
|
+ return res.json({
|
|
|
|
|
+ success: true,
|
|
|
|
|
+ data: null,
|
|
|
|
|
+ message: 'No candle data found for this symbol'
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
res.json({
|
|
@@ -165,7 +167,12 @@ class CandleController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Verify all symbols exist
|
|
// Verify all symbols exist
|
|
|
- const symbolIds = [...new Set(candles.map(c => c.symbolId))];
|
|
|
|
|
|
|
+ const processedCandles = candles.map(candle => ({
|
|
|
|
|
+ ...candle,
|
|
|
|
|
+ symbolId: parseInt(candle.symbolId)
|
|
|
|
|
+ }));
|
|
|
|
|
+
|
|
|
|
|
+ const symbolIds = [...new Set(processedCandles.map(c => c.symbolId))];
|
|
|
const existingSymbols = await Symbol.findAll({
|
|
const existingSymbols = await Symbol.findAll({
|
|
|
where: { id: symbolIds },
|
|
where: { id: symbolIds },
|
|
|
attributes: ['id']
|
|
attributes: ['id']
|
|
@@ -180,7 +187,35 @@ class CandleController {
|
|
|
return next(error);
|
|
return next(error);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const createdCandles = await Candle1h.bulkCreate(candles);
|
|
|
|
|
|
|
+ // Check for existing candles to identify duplicates
|
|
|
|
|
+ const existingCandles = await Candle1h.findAll({
|
|
|
|
|
+ where: {
|
|
|
|
|
+ [Op.or]: processedCandles.map(candle => ({
|
|
|
|
|
+ symbolId: candle.symbolId,
|
|
|
|
|
+ openTime: candle.openTime
|
|
|
|
|
+ }))
|
|
|
|
|
+ },
|
|
|
|
|
+ attributes: ['symbolId', 'openTime']
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // Create a set of existing keys for quick lookup
|
|
|
|
|
+ const existingKeys = new Set(
|
|
|
|
|
+ existingCandles.map(c => `${c.symbolId}-${c.openTime.toISOString()}`)
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // Filter out duplicates
|
|
|
|
|
+ const newCandles = processedCandles.filter(candle =>
|
|
|
|
|
+ !existingKeys.has(`${candle.symbolId}-${candle.openTime.toISOString()}`)
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ const duplicateCount = processedCandles.length - newCandles.length;
|
|
|
|
|
+
|
|
|
|
|
+ // Log duplicates if any
|
|
|
|
|
+ if (duplicateCount > 0) {
|
|
|
|
|
+ console.log(`Bulk create candles: ${duplicateCount} duplicates skipped`);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const createdCandles = await Candle1h.bulkCreate(newCandles);
|
|
|
|
|
|
|
|
// Emit WebSocket events for real-time updates
|
|
// Emit WebSocket events for real-time updates
|
|
|
const io = req.app.get('io');
|
|
const io = req.app.get('io');
|
|
@@ -226,7 +261,7 @@ class CandleController {
|
|
|
res.status(201).json({
|
|
res.status(201).json({
|
|
|
success: true,
|
|
success: true,
|
|
|
data: createdCandles,
|
|
data: createdCandles,
|
|
|
- message: `${createdCandles.length} candles created successfully`
|
|
|
|
|
|
|
+ message: `${createdCandles.length} candles created successfully${duplicateCount > 0 ? `, ${duplicateCount} duplicates skipped` : ''}`
|
|
|
});
|
|
});
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
next(error);
|
|
next(error);
|