user managment
This commit is contained in:
@@ -38,7 +38,6 @@ exports.getPictures = asyncHandler(async (req, res, next) => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
exports.getLotsBySale = asyncHandler(async (req, res, next) => {
|
||||
let id = req.params.id
|
||||
|
||||
@@ -165,3 +164,67 @@ exports.AuctionedItem = asyncHandler(async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
// DB
|
||||
exports.get = asyncHandler(async (req, res, next) => {
|
||||
|
||||
try{
|
||||
const id = req.params.id;
|
||||
let result = await lotDb.get(id);
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.post = asyncHandler(async (req, res, next) => {
|
||||
|
||||
try{
|
||||
// check if double
|
||||
let Lot = await lotDb.getByIDPlatform(req.body.idPlatform, req.body.platform);
|
||||
if(Sale){
|
||||
return res.status(500).send("Lot already exists");
|
||||
}
|
||||
|
||||
let createLot = await lotDb.post(req.body);
|
||||
|
||||
res.status(204).send();
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.put = asyncHandler(async (req, res, next) => {
|
||||
|
||||
try{
|
||||
const id = req.params.id;
|
||||
let updatedDocument = { ...req.body };
|
||||
delete updatedDocument._id;
|
||||
console.log(updatedDocument);
|
||||
let result = await lotDb.put(id, updatedDocument);
|
||||
console.log(result);
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.delete = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
const id = req.params.id;
|
||||
|
||||
// Remove the lot
|
||||
await lotDb.remove(id);
|
||||
|
||||
res.status(200).send({"message": "Lots deleted"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
+164
-6
@@ -8,7 +8,7 @@ const lotDb = new LotDb();
|
||||
const agenda = require('../services/agenda');
|
||||
const {Agent} = require('../services/agent');
|
||||
const agent = new Agent();
|
||||
|
||||
const ExcelJS = require('exceljs');
|
||||
|
||||
exports.getSaleInfos = asyncHandler(async (req, res, next) => {
|
||||
let url = req.params.url
|
||||
@@ -218,31 +218,102 @@ exports.postProcessing = asyncHandler(async (req, res, next) => {
|
||||
}
|
||||
|
||||
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 = Lots[0].Bids[0].timestamp;
|
||||
startTime = TimestampInSecond(Lots[0].Bids[0].timestamp);
|
||||
}else{
|
||||
startTime = Lots[0].timestamp;
|
||||
startTime = TimestampInSecond(Lots[0].timestamp);
|
||||
}
|
||||
|
||||
let LastBid = [...Lots].reverse().find(lot => lot.auctioned !== undefined);
|
||||
|
||||
let endTime = 0;
|
||||
if (Array.isArray(LastBid.Bids)) {
|
||||
endTime = LastBid.Bids[LastBid.Bids.length-1].timestamp;
|
||||
endTime = TimestampInSecond(LastBid.Bids[LastBid.Bids.length-1].timestamp);
|
||||
}else{
|
||||
endTime = LastBid.timestamp;
|
||||
endTime = TimestampInSecond(LastBid.timestamp);
|
||||
}
|
||||
console.log("Start Time: "+startTime);
|
||||
console.log("End Time: "+endTime);
|
||||
|
||||
let duration = endTime-startTime;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,10 +333,15 @@ exports.postProcessing = asyncHandler(async (req, res, next) => {
|
||||
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);
|
||||
@@ -279,4 +355,86 @@ exports.postProcessing = asyncHandler(async (req, res, next) => {
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.SaleStatXsl = 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);
|
||||
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const worksheet = workbook.addWorksheet('Sale Stats');
|
||||
|
||||
worksheet.columns = [
|
||||
{ header: 'Lot #', key: 'lotNumber', width: 10 },
|
||||
{ header: 'Description', key: 'description', width: 100 },
|
||||
{ header: 'Auctioned Amount', key: 'auctionedAmount', width: 20 },
|
||||
{ header: 'Estimate Low', key: 'estimateLow', width: 20 },
|
||||
{ header: 'Estimate High', key: 'estimateHigh', width: 20 },
|
||||
{ header: 'Bids', key: 'nbrBids', width: 10 },
|
||||
{ header: 'Highest Bid', key: 'highestBid', width: 20 },
|
||||
{ header: 'Duration (in s)', key: 'duration', width: 10 },
|
||||
{ header: '% Above Low', key: 'percentageAboveUnderLow', width: 20 },
|
||||
{ header: '% Above High', key: 'percentageAboveUnderHigh', width: 20 }
|
||||
];
|
||||
|
||||
let row = 2;
|
||||
for (let lot of Lots) {
|
||||
let Row = worksheet.addRow({
|
||||
lotNumber: lot.lotNumber,
|
||||
description: lot.description,
|
||||
auctionedAmount: lot.auctioned?.amount,
|
||||
estimateLow: lot.EstimateLow,
|
||||
estimateHigh: lot.EstimateHigh,
|
||||
nbrBids: lot.postProcessing?.nbrBids,
|
||||
highestBid: lot.postProcessing?.highestBid,
|
||||
duration: lot.postProcessing?.duration,
|
||||
percentageAboveUnderLow: lot.postProcessing?.percentageAboveUnderLow/100,
|
||||
percentageAboveUnderHigh: lot.postProcessing?.percentageAboveUnderHigh/100
|
||||
});
|
||||
|
||||
Row.getCell('C').numFmt = '€0.00';
|
||||
Row.getCell('D').numFmt = '€0.00';
|
||||
Row.getCell('E').numFmt = '€0.00';
|
||||
Row.getCell('B').numFmt = '€0.00';
|
||||
Row.getCell('G').numFmt = '€0.00';
|
||||
|
||||
Row.getCell('I').numFmt = '0%';
|
||||
Row.getCell('J').numFmt = '0%';
|
||||
|
||||
row++;
|
||||
}
|
||||
|
||||
worksheet.addRow({
|
||||
lotNumber: 'Total',
|
||||
auctionedAmount: Sale.postProcessing?.totalAmount,
|
||||
estimateLow: '',
|
||||
estimateHigh: '',
|
||||
nbrBids: '',
|
||||
highestBid: '',
|
||||
duration: Sale.postProcessing?.duration,
|
||||
percentageAboveUnderLow: '',
|
||||
percentageAboveUnderHigh: ''
|
||||
});
|
||||
|
||||
// send the Xls File
|
||||
res.setHeader(
|
||||
"Content-Disposition",
|
||||
"attachment; filename=" + "SaleStats.xlsx"
|
||||
);
|
||||
|
||||
await workbook.xlsx.write(res);
|
||||
return res.status(200).end();
|
||||
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,199 @@
|
||||
const asyncHandler = require("express-async-handler");
|
||||
const moment = require('moment-timezone');
|
||||
const { ObjectId } = require('mongodb');
|
||||
const { UserDb } = require("../services/userDb");
|
||||
const crypto = require('crypto');
|
||||
|
||||
function ClearUserData(user){
|
||||
delete user.salt;
|
||||
delete user.hashed_password;
|
||||
delete user.salt;
|
||||
delete user.isAgent;
|
||||
return user;
|
||||
}
|
||||
|
||||
function ClearUserDataForAdmin(user){
|
||||
delete user.salt;
|
||||
delete user.hashed_password;
|
||||
delete user.salt;
|
||||
return user;
|
||||
}
|
||||
|
||||
// DB
|
||||
exports.get = asyncHandler(async (req, res, next) => {
|
||||
|
||||
try{
|
||||
const userDb = await UserDb.init();
|
||||
const id = req.params.id;
|
||||
let result = await userDb.get(id);
|
||||
if (req.user.isAdmin){
|
||||
res.status(200).send(ClearUserDataForAdmin(result));
|
||||
}else{
|
||||
res.status(200).send(ClearUserData(result));
|
||||
}
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.post = asyncHandler(async (req, res, next) => {
|
||||
|
||||
try{
|
||||
const userDb = await UserDb.init();
|
||||
// check if double
|
||||
let User = await userDb.getByEmail(req.body.email);
|
||||
if(User){
|
||||
return res.status(500).send("User already exists");
|
||||
}
|
||||
|
||||
// check password
|
||||
if(!req.body.password){
|
||||
return res.status(500).send("Password not set");
|
||||
}
|
||||
if(req.body.password != req.body.confirmPassword){
|
||||
return res.status(500).send("Passwords do not match");
|
||||
}
|
||||
if(req.body.password.length < 8){
|
||||
return res.status(500).send("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");
|
||||
}
|
||||
}else{
|
||||
req.body.isAdmin = false
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}else{
|
||||
req.body.isAgent = false
|
||||
}
|
||||
}
|
||||
|
||||
let salt = crypto.randomBytes(16).toString('hex');
|
||||
let user = {
|
||||
username: req.body.username,
|
||||
hashed_password: crypto.pbkdf2Sync(req.body.password, salt, 310000, 32, 'sha256').toString('hex'),
|
||||
salt: salt,
|
||||
email: req.body.email,
|
||||
isAdmin: req.body.isAdmin,
|
||||
isAgent: req.body.isAgent,
|
||||
}
|
||||
|
||||
let createData = await userDb.post(user);
|
||||
|
||||
res.status(204).send();
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.put = asyncHandler(async (req, res, next) => {
|
||||
|
||||
try{
|
||||
const userDb = await UserDb.init();
|
||||
const id = req.params.id;
|
||||
|
||||
const User = await userDb.get(id);
|
||||
if(!User){
|
||||
return res.status(500).send("User not found");
|
||||
}
|
||||
|
||||
// check password
|
||||
let hashed_password = "";
|
||||
let salt = "";
|
||||
if(req.body.password){
|
||||
if(req.body.password != req.body.confirmPassword){
|
||||
return res.status(500).send("Passwords do not match");
|
||||
}
|
||||
if(req.body.password.length < 8){
|
||||
return res.status(500).send("Password too short");
|
||||
}
|
||||
salt = crypto.randomBytes(16).toString('hex');
|
||||
hashed_password = crypto.pbkdf2Sync(req.body.password, salt, 310000, 32, 'sha256').toString('hex');
|
||||
}else{
|
||||
salt = User.salt;
|
||||
hashed_password = User.hashed_password;
|
||||
}
|
||||
|
||||
if(req.body.isAdmin){
|
||||
if(!req.user.isAdmin){
|
||||
return res.status(500).send("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");
|
||||
}
|
||||
}
|
||||
|
||||
let user = {
|
||||
username: req.body.username,
|
||||
hashed_password: hashed_password,
|
||||
salt: salt,
|
||||
email: req.body.email,
|
||||
isAdmin: req.body.isAdmin,
|
||||
isAgent: req.body.isAgent,
|
||||
}
|
||||
|
||||
let result = await userDb.put(id, user);
|
||||
console.log(result);
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.delete = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
const userDb = await UserDb.init();
|
||||
const id = req.params.id;
|
||||
|
||||
// Remove the sale
|
||||
await userDb.remove(id);
|
||||
|
||||
res.status(200).send({"message": "User deleted"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// Functions
|
||||
exports.current = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
const user = ClearUserData(req.user);
|
||||
res.status(200).send(user);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
exports.getAllUsers = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
const userDb = await UserDb.init();
|
||||
let result = await userDb.getAll();
|
||||
result = result.map(user => ClearUserDataForAdmin(user));
|
||||
res.status(200).send(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user