Merge branch 'master' of github.com:KevinMidboe/vinlottis
This commit is contained in:
@@ -33,7 +33,6 @@ const addWinnerWithWine = async (winner, wine) => {
|
||||
wine: savedWine,
|
||||
color: winner.color
|
||||
};
|
||||
|
||||
if (exisitingWinner == undefined) {
|
||||
const newWinner = new Winner({
|
||||
name: winner.name,
|
||||
|
||||
@@ -5,10 +5,13 @@ const Attendee = require(path.join(__dirname, "/schemas/Attendee"));
|
||||
const PreLotteryWine = require(path.join(__dirname, "/schemas/PreLotteryWine"));
|
||||
const VirtualWinner = require(path.join(__dirname, "/schemas/VirtualWinner"));
|
||||
const Lottery = require(path.join(__dirname, "/schemas/Purchase"));
|
||||
const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
|
||||
|
||||
const Message = require(path.join(__dirname, "/message"));
|
||||
const historyRepository = require(path.join(__dirname, "/history"));
|
||||
const wineRepository = require(path.join(__dirname, "/wine"));
|
||||
const winnerRepository = require(path.join(__dirname, "/winner"));
|
||||
const prelotteryWineRepository = require(path.join(__dirname, "/prelotteryWine"));
|
||||
|
||||
const {
|
||||
WinnerNotFound,
|
||||
@@ -17,11 +20,36 @@ const {
|
||||
LotteryByDateNotFound
|
||||
} = require(path.join(__dirname, "/vinlottisErrors"));
|
||||
|
||||
const moveUnfoundPrelotteryWineToWines = async (error, tempWine) => {
|
||||
if(!(error instanceof WineNotFound)) {
|
||||
throw error
|
||||
}
|
||||
|
||||
if(!tempWine.winner) {
|
||||
throw new WinnerNotFound()
|
||||
}
|
||||
|
||||
const prelotteryWine = await prelotteryWineRepository.wineById(tempWine._id);
|
||||
const winner = await winnerRepository.winnerById(tempWine.winner.id, true);
|
||||
|
||||
return wineRepository
|
||||
.addWine(prelotteryWine)
|
||||
.then(_ => prelotteryWineRepository.addWinnerToWine(prelotteryWine, winner)) // prelotteryWine.deleteById
|
||||
.then(_ => historyRepository.addWinnerWithWine(winner, prelotteryWine))
|
||||
.then(_ => winnerRepository.setWinnerChosenById(winner.id))
|
||||
}
|
||||
|
||||
const archive = (date, raffles, stolen, wines) => {
|
||||
const { blue, red, yellow, green } = raffles;
|
||||
const bought = blue + red + yellow + green;
|
||||
|
||||
return Promise.all(wines.map(wine => wineRepository.findWine(wine))).then(resolvedWines => {
|
||||
return Promise.all(
|
||||
wines.map(wine => wineRepository
|
||||
.findWine(wine)
|
||||
.catch(error => moveUnfoundPrelotteryWineToWines(error, wine)
|
||||
.then(_ => wineRepository.findWine(wine))
|
||||
))
|
||||
).then(resolvedWines => {
|
||||
const lottery = new Lottery({
|
||||
date,
|
||||
blue,
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
const path = require("path");
|
||||
const Highscore = require(path.join(__dirname, "/schemas/Highscore"));
|
||||
|
||||
async function findSavePerson(foundWinner, wonWine, date) {
|
||||
let person = await Highscore.findOne({
|
||||
name: foundWinner.name
|
||||
});
|
||||
|
||||
if (person == undefined) {
|
||||
let newPerson = new Highscore({
|
||||
name: foundWinner.name,
|
||||
wins: [
|
||||
{
|
||||
color: foundWinner.color,
|
||||
date: date,
|
||||
wine: wonWine
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
await newPerson.save();
|
||||
} else {
|
||||
person.wins.push({
|
||||
color: foundWinner.color,
|
||||
date: date,
|
||||
wine: wonWine
|
||||
});
|
||||
person.markModified("wins");
|
||||
await person.save();
|
||||
}
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
module.exports.findSavePerson = findSavePerson;
|
||||
@@ -23,7 +23,7 @@ const addWines = wines => {
|
||||
country: wine.country,
|
||||
id: wine.id
|
||||
});
|
||||
|
||||
console.log(newPrelotteryWine)
|
||||
return newPrelotteryWine.save();
|
||||
});
|
||||
|
||||
|
||||
@@ -87,6 +87,9 @@ router.get("/chat/history", chatController.getAllHistory);
|
||||
router.delete("/chat/history", mustBeAuthenticated, chatController.deleteHistory);
|
||||
|
||||
router.post("/login", userController.login);
|
||||
|
||||
// We should have a check here if we are dev/prod,
|
||||
// and disable the mustBeAuthentacted for the register
|
||||
router.post("/register", mustBeAuthenticated, userController.register);
|
||||
router.get("/logout", userController.logout);
|
||||
|
||||
|
||||
@@ -10,17 +10,19 @@ const redactWinnerInfoMapper = winner => {
|
||||
};
|
||||
};
|
||||
|
||||
const addWinner = winner => {
|
||||
let newWinner = new VirtualWinner({
|
||||
name: winner.name,
|
||||
color: winner.color,
|
||||
timestamp_drawn: new Date().getTime()
|
||||
});
|
||||
|
||||
return newWinner.save()
|
||||
}
|
||||
|
||||
const addWinners = winners => {
|
||||
return Promise.all(
|
||||
winners.map(winner => {
|
||||
let newWinnerElement = new VirtualWinner({
|
||||
name: winner.name,
|
||||
color: winner.color,
|
||||
timestamp_drawn: new Date().getTime()
|
||||
});
|
||||
|
||||
return newWinnerElement.save();
|
||||
})
|
||||
winners.map(winner => addWinner(winner))
|
||||
);
|
||||
};
|
||||
|
||||
@@ -49,6 +51,14 @@ const winnerById = (id, isAdmin = false) => {
|
||||
});
|
||||
};
|
||||
|
||||
const setWinnerChosenById = (id) => {
|
||||
return VirtualWinner.findOne({id: id}).then(winner => {
|
||||
winner.prize_selected = true
|
||||
winner.markModified("wins")
|
||||
return winner.save()
|
||||
})
|
||||
}
|
||||
|
||||
const updateWinnerById = (id, updateModel) => {
|
||||
return VirtualWinner.findOne({ id: id }).then(winner => {
|
||||
if (winner == null) {
|
||||
@@ -86,10 +96,12 @@ const deleteWinners = () => {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
addWinner,
|
||||
addWinners,
|
||||
allWinners,
|
||||
winnerById,
|
||||
updateWinnerById,
|
||||
deleteWinnerById,
|
||||
deleteWinners
|
||||
deleteWinners,
|
||||
setWinnerChosenById
|
||||
};
|
||||
|
||||
78
db/seedSingleDay.js
Normal file
78
db/seedSingleDay.js
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
|
||||
const session = require("express-session");
|
||||
const mongoose = require("mongoose");
|
||||
const MongoStore = require("connect-mongo")(session);
|
||||
mongoose.promise = global.Promise;
|
||||
mongoose
|
||||
.connect("mongodb://localhost/vinlottis", {
|
||||
useCreateIndex: true,
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
serverSelectionTimeoutMS: 10000 // initial connection timeout
|
||||
})
|
||||
.then(_ => console.log("Mongodb connection established!"))
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
console.error("ERROR! Mongodb required to run.");
|
||||
process.exit(1);
|
||||
});
|
||||
mongoose.set("debug", false);
|
||||
|
||||
const path = require("path")
|
||||
const prelotteryWineRepository = require(path.join(__dirname, "../api/prelotteryWine"));
|
||||
const attendeeRepository = require(path.join(__dirname, "../api/attendee"));
|
||||
|
||||
async function add() {
|
||||
const wines = [
|
||||
{
|
||||
vivinoLink: 'https://www.vinmonopolet.no/Land/Frankrike/Devevey-Bourgogne-Hautes-C%C3%B4tes-de-Beaune-Rouge-2018/p/12351301',
|
||||
name: 'Devevey Bourgogne Hautes-Côtes de Beaune Rouge 2018',
|
||||
rating: 3,
|
||||
id: '12351301',
|
||||
year: 2018,
|
||||
image: "https://bilder.vinmonopolet.no/cache/300x300-0/12351301-1.jpg",
|
||||
price: '370',
|
||||
country: "Frankrike"
|
||||
},
|
||||
{
|
||||
vivinoLink: 'https://www.vinmonopolet.no/Land/Frankrike/Devevey-Rully-La-Chaume-Rouge-2018/p/12351101',
|
||||
name: 'Devevey Rully La Chaume Rouge 2018',
|
||||
rating: 4,
|
||||
id: '12351101',
|
||||
year: 2018,
|
||||
image: 'https://bilder.vinmonopolet.no/cache/300x300-0/12351101-1.jpg',
|
||||
price: '372',
|
||||
country: 'Frankrike'
|
||||
}
|
||||
]
|
||||
|
||||
const attendees = [
|
||||
{
|
||||
name: "Kasper Rynning-Tønnesen",
|
||||
red: 0,
|
||||
blue: 10,
|
||||
green: 0,
|
||||
yellow: 0,
|
||||
phoneNumber: 97777777,
|
||||
winner: false
|
||||
},
|
||||
{
|
||||
name: "Kevin Midbøe",
|
||||
red: 3,
|
||||
blue: 3,
|
||||
green: 3,
|
||||
yellow: 3,
|
||||
phoneNumber: 95012321,
|
||||
winner: false
|
||||
}
|
||||
]
|
||||
|
||||
await prelotteryWineRepository.addWines(wines)
|
||||
await Promise.all(attendees.map(attendee => attendeeRepository.addAttendee(attendee)))
|
||||
|
||||
console.log("Added some wines, and 2 attendees to database.")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
add()
|
||||
@@ -9,7 +9,8 @@
|
||||
"watch": "yarn webpack serve --mode development --env development",
|
||||
"start": "node server.js",
|
||||
"dev": "cross-env NODE_ENV=development node server.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"seed:single": "node db/seedSingleDay.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
|
||||
Reference in New Issue
Block a user