This commit is contained in:
2024-09-27 17:20:37 +02:00
parent ee932a2339
commit f43e802501
59 changed files with 810 additions and 236 deletions
+3 -3
View File
@@ -6,10 +6,10 @@ exports.save = asyncHandler(async (req, res, next) => {
try{
let result = await save(req.body);
console.log(result);
res.status(204).send();
res.status(204).send({message: "Favorite created"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -22,6 +22,6 @@ 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});
}
});
+23 -16
View File
@@ -20,6 +20,8 @@ exports.getInfos = asyncHandler(async (req, res, next) => {
})
.catch(error => {
console.error(error);
res.json({error: error});
});
});
@@ -35,6 +37,7 @@ exports.getPictures = asyncHandler(async (req, res, next) => {
})
.catch(error => {
console.error(error);
res.json({error: error});
});
});
@@ -44,7 +47,7 @@ exports.getLotsBySale = asyncHandler(async (req, res, next) => {
const 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);
@@ -60,7 +63,7 @@ exports.NextItem = asyncHandler(async (req, res, next) => {
Sale = await saleDb.getByIDPlatform(req.body.idSalePlatform, req.body.platform);
if(!Sale){
console.error("Sale not found");
return res.status(404).send("Sale not found");
return res.status(404).send({error: "Sale not found"});
}
let Lot = await lotDb.getByIDPlatform(req.body.idPlatform, req.body.platform);
@@ -96,10 +99,10 @@ exports.NextItem = asyncHandler(async (req, res, next) => {
await lotDb.put(Lot._id, Lot);
}
res.status(204).send();
res.status(204).send({message: "Lot updated"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -126,13 +129,13 @@ exports.Bid = asyncHandler(async (req, res, next) => {
await lotDb.put(Lot._id, Lot);
}else{
console.error("Lot not found");
return res.status(404).send("Lot not found");
return res.status(404).send({error: "Lot not found"});
}
res.status(204).send();
res.status(204).send({message: "Lot updated"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -152,15 +155,19 @@ exports.AuctionedItem = asyncHandler(async (req, res, next) => {
}
await lotDb.put(Lot._id, Lot);
//update stats
await saleDb.processStats(Lot.sale_id);
}else{
console.error("Lot not found");
return res.status(404).send("Lot not found");
return res.status(404).send({error: "Lot not found"});
}
res.status(204).send();
res.status(204).send({message: "Lot updated"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -173,7 +180,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});
}
});
@@ -184,15 +191,15 @@ exports.post = asyncHandler(async (req, res, next) => {
// check if double
let Lot = await lotDb.getByIDPlatform(req.body.idPlatform, req.body.platform);
if(Sale){
return res.status(500).send("Lot already exists");
return res.status(500).send({ error: "Lot already exists"});
}
let createLot = await lotDb.post(req.body);
res.status(204).send();
res.status(204).send({message: "Lot created"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -209,7 +216,7 @@ exports.put = 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});
}
});
@@ -224,7 +231,7 @@ exports.delete = asyncHandler(async (req, res, next) => {
res.status(200).send({"message": "Lots deleted"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
+17 -181
View File
@@ -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);
+21 -20
View File
@@ -3,6 +3,7 @@ const moment = require('moment-timezone');
const { ObjectId } = require('mongodb');
const { UserDb } = require("../services/userDb");
const crypto = require('crypto');
const { error } = require("console");
function ClearUserData(user){
delete user.salt;
@@ -33,7 +34,7 @@ exports.get = asyncHandler(async (req, res, next) => {
}
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -45,24 +46,24 @@ exports.post = asyncHandler(async (req, res, next) => {
// check if double
let User = await userDb.getByEmail(req.body.email);
if(User){
return res.status(500).send("User already exists");
return res.status(500).send({error: "User already exists"});
}
// check password
if(!req.body.password){
return res.status(500).send("Password not set");
return res.status(500).send({error: "Password not set"});
}
if(req.body.password != req.body.confirmPassword){
return res.status(500).send("Passwords do not match");
return res.status(500).send({error: "Passwords do not match"});
}
if(req.body.password.length < 8){
return res.status(500).send("Password too short");
return res.status(500).send({error: "Password too short"});
}
if(req.body.isAdmin){
if(req.user){
if(!req.user.isAdmin){
return res.status(500).send("You are not allowed to create an admin user");
return res.status(500).send({error: "You are not allowed to create an admin user"});
}
}else{
req.body.isAdmin = false
@@ -72,7 +73,7 @@ exports.post = asyncHandler(async (req, res, next) => {
if(req.body.isAgent){
if(req.user){
if(!req.user.isAgent){
return res.status(500).send("You are not allowed to create an agent user");
return res.status(500).send({error: "You are not allowed to create an agent user"});
}
}else{
req.body.isAgent = false
@@ -91,10 +92,10 @@ exports.post = asyncHandler(async (req, res, next) => {
let createData = await userDb.post(user);
res.status(204).send();
res.status(204).send({message: "User created"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -107,7 +108,7 @@ exports.put = asyncHandler(async (req, res, next) => {
const User = await userDb.get(id);
if(!User){
return res.status(500).send("User not found");
return res.status(500).send({error:"User not found"});
}
// check password
@@ -115,10 +116,10 @@ exports.put = asyncHandler(async (req, res, next) => {
let salt = "";
if(req.body.password){
if(req.body.password != req.body.confirmPassword){
return res.status(500).send("Passwords do not match");
return res.status(500).send({error:"Passwords do not match"});
}
if(req.body.password.length < 8){
return res.status(500).send("Password too short");
return res.status(500).send({error:"Password too short"});
}
salt = crypto.randomBytes(16).toString('hex');
hashed_password = crypto.pbkdf2Sync(req.body.password, salt, 310000, 32, 'sha256').toString('hex');
@@ -129,12 +130,12 @@ exports.put = asyncHandler(async (req, res, next) => {
if(req.body.isAdmin){
if(!req.user.isAdmin){
return res.status(500).send("You are not allowed to create an admin user");
return res.status(500).send({error:"You are not allowed to create an admin user"});
}
}
if(req.body.isAgent){
if(!req.user.isAdmin){
return res.status(500).send("You are not allowed to create an agent user");
return res.status(500).send({error:"You are not allowed to create an agent user"});
}
}
@@ -152,7 +153,7 @@ exports.put = 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});
}
});
@@ -168,7 +169,7 @@ exports.delete = asyncHandler(async (req, res, next) => {
res.status(200).send({"message": "User deleted"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -180,7 +181,7 @@ exports.current = asyncHandler(async (req, res, next) => {
res.status(200).send(user);
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -188,10 +189,10 @@ exports.current = asyncHandler(async (req, res, next) => {
exports.agentConnected = asyncHandler(async (req, res, next) => {
try{
res.status(200).send("OK");
res.status(200).send({message: "Agent connected"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -203,7 +204,7 @@ exports.getAllUsers = 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});
}
});
+144
View File
@@ -1,6 +1,8 @@
const { ObjectId } = require('mongodb');
const connectDb = require("./db");
const { LotDb } = require("./lotDb");
const lotDb = new LotDb();
const SaleDb = class
{
@@ -63,6 +65,148 @@ const SaleDb = class
let result = await this.collection.findOne({idPlatform: String(idSalePlatform), platform: String(platformName)});
return result;
}
async processStats(id)
{
let Sale = await this.get(id);
if(!Sale){
console.error("Sale not found");
throw new Error("Sale not found");
}
console.log(Sale);
let Lots = await lotDb.getBySaleId(Sale._id.toString(),Sale.platform);
let 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 this.put(Sale._id, Sale);
}
}
module.exports = { SaleDb };