Keeps more information when adding prelottery wine

This commit is contained in:
2021-01-26 22:35:12 +01:00
parent 6e02c5e393
commit 33070ae31a

View File

@@ -1,27 +1,30 @@
const path = require("path"); const path = require("path");
const Wine = require(path.join(__dirname, "/schemas/Wine")); const Wine = require(path.join(__dirname, "/schemas/Wine"));
async function findSaveWine(prelotteryWine) { const addWine = async wine => {
let wonWine = await Wine.findOne({ name: prelotteryWine.name }); let existingWine = await Wine.findOne({ name: wine.name, id: wine.id, year: wine.year });
if (wonWine == undefined) {
let newWonWine = new Wine({ if (existingWine == undefined) {
name: prelotteryWine.name, let newWine = new Wine({
vivinoLink: prelotteryWine.vivinoLink, name: wine.name,
rating: prelotteryWine.rating, vivinoLink: wine.vivinoLink,
rating: wine.rating,
occurences: 1, occurences: 1,
image: prelotteryWine.image, id: wine.id,
id: prelotteryWine.id year: wine.year,
image: wine.image,
price: wine.price,
country: wine.country
}); });
await newWonWine.save(); await newWine.save();
wonWine = newWonWine; return newWine;
} else { } else {
wonWine.occurences += 1; existingWine.occurences += 1;
wonWine.image = prelotteryWine.image; await existingWine.save();
wonWine.id = prelotteryWine.id; return existingWine;
await wonWine.save();
} }
};
return wonWine; module.exports = {
} addWine
};
module.exports.findSaveWine = findSaveWine;