testprod
This commit is contained in:
+17
-181
@@ -18,18 +18,8 @@ exports.getSaleInfos = asyncHandler(async (req, res, next) => {
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
return res.status(500).send(error);
|
||||
return res.status(500).send({error: error});
|
||||
});
|
||||
// url = encodeURIComponent(url);
|
||||
|
||||
// fetch(ApiAgentURL+'/sale/getSaleInfos/'+url)
|
||||
// .then(response => response.json())
|
||||
// .then(data => {
|
||||
// res.json(data);
|
||||
// })
|
||||
// .catch(error => {
|
||||
// console.error(error);
|
||||
// });
|
||||
});
|
||||
|
||||
exports.prepareSale = asyncHandler(async (req, res, next) => {
|
||||
@@ -43,27 +33,11 @@ exports.prepareSale = asyncHandler(async (req, res, next) => {
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
return res.status(500).send(error);
|
||||
return res.status(500).send({error: error});
|
||||
});
|
||||
// url = encodeURIComponent(url);
|
||||
// fetch(ApiAgentURL+'/sale/getLotList/'+url)
|
||||
// .then(response => response.json())
|
||||
// .then(async data => {
|
||||
// console.log(data);
|
||||
// for (let lot of data) {
|
||||
// lot.sale_id = Sale._id
|
||||
|
||||
// await lotDb.post(lot);
|
||||
// }
|
||||
// res.status(200).send({"message": "Lots created"})
|
||||
// })
|
||||
// .catch(error => {
|
||||
// console.error(error);
|
||||
// return res.status(500).send(error);
|
||||
// });
|
||||
}catch(err){
|
||||
console.error(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -78,11 +52,11 @@ exports.followSale = asyncHandler(async (req, res, next) => {
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
return res.status(500).send(error);
|
||||
return res.status(500).send({error: error});
|
||||
});
|
||||
}catch(err){
|
||||
console.error(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -96,7 +70,7 @@ exports.get = asyncHandler(async (req, res, next) => {
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -107,7 +81,7 @@ exports.post = asyncHandler(async (req, res, next) => {
|
||||
// check if double
|
||||
let Sale = await saleDb.getByIDPlatform(req.body.idPlatform, req.body.platform);
|
||||
if(Sale){
|
||||
return res.status(500).send("Sale already exists");
|
||||
return res.status(500).send({error: "Sale already exists"});
|
||||
}
|
||||
|
||||
let createData = await saleDb.post(req.body);
|
||||
@@ -131,10 +105,10 @@ exports.post = asyncHandler(async (req, res, next) => {
|
||||
await jobFollow.save();
|
||||
}else{ console.log("Sale is in the past, no Follow Job");}
|
||||
|
||||
res.status(204).send();
|
||||
res.status(204).send({"message": "Sale created"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -147,11 +121,11 @@ exports.put = asyncHandler(async (req, res, next) => {
|
||||
delete updatedDocument._id;
|
||||
console.log(updatedDocument);
|
||||
let result = await saleDb.put(id, updatedDocument);
|
||||
console.log(result);
|
||||
//console.log(result);
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -176,7 +150,7 @@ exports.delete = asyncHandler(async (req, res, next) => {
|
||||
res.status(200).send({"message": "Sale and Lots deleted"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -189,7 +163,7 @@ exports.getAll = asyncHandler(async (req, res, next) => {
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -203,156 +177,18 @@ exports.getByUrl = asyncHandler(async (req, res, next) => {
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
});
|
||||
|
||||
exports.postProcessing = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
const id = req.params.id;
|
||||
|
||||
Sale = await saleDb.get(id);
|
||||
if(!Sale){
|
||||
console.error("Sale not found");
|
||||
return res.status(404).send("Sale not found");
|
||||
}
|
||||
|
||||
Lots = await lotDb.getBySaleId(Sale._id.toString(),Sale.platform);
|
||||
|
||||
TimestampInSecond = (timestamp) => {
|
||||
const stringTimestamp = String(timestamp);
|
||||
if (stringTimestamp.length === 13) {
|
||||
return timestamp / 1000;
|
||||
} else if (stringTimestamp.length === 10) {
|
||||
return timestamp;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Create an array to hold the updated lots
|
||||
let updatedLots = [];
|
||||
let bidsDuration = 0;
|
||||
|
||||
// process each lot
|
||||
for (let lot of Lots) {
|
||||
let highestBid, duration, percentageAboveUnderLow, percentageAboveUnderHigh = 0;
|
||||
|
||||
// if bid
|
||||
let nbrBids = 0;
|
||||
if (Array.isArray(lot.Bids)) {
|
||||
|
||||
nbrBids = lot.Bids.length;
|
||||
|
||||
highestBid = lot.Bids.reduce((prev, current) => (prev.amount > current.amount) ? prev : current).amount;
|
||||
let startTime = TimestampInSecond(lot.Bids[0].timestamp);
|
||||
let endTime = TimestampInSecond(lot.Bids[lot.Bids.length-1].timestamp);
|
||||
duration = endTime - startTime;
|
||||
|
||||
// total time of bids
|
||||
bidsDuration += duration;
|
||||
|
||||
duration = duration.toFixed(0);
|
||||
}
|
||||
|
||||
// if auctioned
|
||||
percentageAboveUnderLow = 0;
|
||||
percentageAboveUnderHigh = 0;
|
||||
if (lot.auctioned) {
|
||||
|
||||
if(lot.EstimateLow){
|
||||
percentageAboveUnderLow = ((lot.auctioned.amount - lot.EstimateLow) / lot.EstimateLow) * 100;
|
||||
}
|
||||
|
||||
if(lot.EstimateHigh){
|
||||
percentageAboveUnderHigh = ((lot.auctioned.amount - lot.EstimateHigh) / lot.EstimateHigh) * 100;
|
||||
}
|
||||
}
|
||||
|
||||
let lotPostProcessing = {
|
||||
nbrBids: nbrBids,
|
||||
highestBid: highestBid,
|
||||
duration: duration,
|
||||
percentageAboveUnderLow: percentageAboveUnderLow.toFixed(0),
|
||||
percentageAboveUnderHigh: percentageAboveUnderHigh.toFixed(0)
|
||||
}
|
||||
lot.postProcessing = lotPostProcessing;
|
||||
await lotDb.put(lot._id, lot);
|
||||
|
||||
// Add the updated lot to the array
|
||||
updatedLots.push(lot);
|
||||
}
|
||||
|
||||
// refresh with postprocess datas
|
||||
Lots = updatedLots;
|
||||
|
||||
|
||||
let startTime = 0;
|
||||
if (Array.isArray(Lots[0].Bids)) {
|
||||
startTime = TimestampInSecond(Lots[0].Bids[0].timestamp);
|
||||
}else{
|
||||
startTime = TimestampInSecond(Lots[0].timestamp);
|
||||
}
|
||||
|
||||
let LastBid = [...Lots].reverse().find(lot => lot.auctioned !== undefined);
|
||||
|
||||
let endTime = 0;
|
||||
if (Array.isArray(LastBid.Bids)) {
|
||||
endTime = TimestampInSecond(LastBid.Bids[LastBid.Bids.length-1].timestamp);
|
||||
}else{
|
||||
endTime = TimestampInSecond(LastBid.timestamp);
|
||||
}
|
||||
console.log("Start Time: "+startTime);
|
||||
console.log("End Time: "+endTime);
|
||||
|
||||
let duration = (endTime-startTime).toFixed(0);
|
||||
|
||||
let totalAmount = 0;
|
||||
let unsoldLots = 0;
|
||||
for (let lot of Lots) {
|
||||
if (lot.auctioned) {
|
||||
totalAmount += lot.auctioned.amount;
|
||||
} else {
|
||||
unsoldLots++;
|
||||
}
|
||||
}
|
||||
|
||||
function calculateMedian(array) {
|
||||
array.sort((a, b) => a - b);
|
||||
let middleIndex = Math.floor(array.length / 2);
|
||||
|
||||
if (array.length % 2 === 0) { // array has an even length
|
||||
return (array[middleIndex - 1] + array[middleIndex]) / 2;
|
||||
} else { // array has an odd length
|
||||
return array[middleIndex];
|
||||
}
|
||||
}
|
||||
const amounts = Lots.map(lot => lot.auctioned?.amount).filter(Boolean);
|
||||
|
||||
//console.error(Lots);
|
||||
let postProcessing = {
|
||||
nbrLots: Lots.length,
|
||||
duration: duration,
|
||||
bidsDuration: bidsDuration.toFixed(0),
|
||||
durationPerLots: (duration/Lots.length).toFixed(0),
|
||||
totalAmount: totalAmount,
|
||||
averageAmount: (totalAmount/Lots.length).toFixed(2),
|
||||
medianAmount: calculateMedian(amounts).toFixed(2),
|
||||
minAmount: Math.min(...amounts).toFixed(2),
|
||||
maxAmount: Math.max(...amounts).toFixed(2),
|
||||
unsoldLots: unsoldLots,
|
||||
unsoldPercentage: ((unsoldLots/Lots.length)*100).toFixed(2)
|
||||
}
|
||||
|
||||
console.log(postProcessing);
|
||||
|
||||
Sale.postProcessing = postProcessing;
|
||||
await saleDb.put(Sale._id, Sale);
|
||||
|
||||
await saleDb.processStats(id);
|
||||
res.status(200).send({"message": "Post Processing done"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
return res.status(500).send({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -364,7 +200,7 @@ exports.SaleStatXsl = asyncHandler(async (req, res, next) => {
|
||||
Sale = await saleDb.get(id);
|
||||
if(!Sale){
|
||||
console.error("Sale not found");
|
||||
return res.status(404).send("Sale not found");
|
||||
return res.status(404).send({error: "Sale not found"});
|
||||
}
|
||||
|
||||
Lots = await lotDb.getBySaleId(Sale._id.toString(),Sale.platform);
|
||||
|
||||
Reference in New Issue
Block a user