diff --git a/api/index.js b/api/index.js index 2a68ec0..1dc3ea2 100644 --- a/api/index.js +++ b/api/index.js @@ -2,7 +2,7 @@ const server = require('./src/app.js'); const { conn, Diet } = require('./src/db.js'); const {DB_PORT} = process.env const dietTypes = require('./src/utils/apispoon') - +const { getApi } = require("./src/getApiRecipes/getApiRecipe") // import dietTypes from './src/utils/apispoon' // Syncing all the models at once. @@ -26,4 +26,6 @@ conn.sync({ alter: true }).then(() => { }) }); + + getApi() }); \ No newline at end of file diff --git a/api/package.json b/api/package.json index f76ced8..77512a9 100644 --- a/api/package.json +++ b/api/package.json @@ -22,7 +22,8 @@ "nodemailer": "^6.8.0", "pg": "^8.8.0", "sequelize": "^6.23.2", - "stripe": "^10.13.0" + "stripe": "^10.13.0", + "uuid": "^9.0.0" }, "devDependencies": { "nodemon": "^2.0.20" diff --git a/api/src/controllers/recipecontrollers.js b/api/src/controllers/recipecontrollers.js index 44af5f9..0cd6d42 100644 --- a/api/src/controllers/recipecontrollers.js +++ b/api/src/controllers/recipecontrollers.js @@ -83,6 +83,8 @@ const updateRecipe = async (id) => { } + + module.exports = { getApiRecipeByID, createRecipe, diff --git a/api/src/controllers/recipescontrollers.js b/api/src/controllers/recipescontrollers.js index 5cf23e8..c8a0496 100644 --- a/api/src/controllers/recipescontrollers.js +++ b/api/src/controllers/recipescontrollers.js @@ -2,38 +2,41 @@ const axios = require ("axios") const { API_KEY } = process.env const { Recipe, Diet } = require("../db.js") // import dietTypes from '../utils/apispoon' +const uuid = require('uuid'); -const getApiRecipes = async() => { - try { +// const getApiRecipes = async() => { +// try { - const axiosResponse = await axios.get(`https://api.spoonacular.com/recipes/complexSearch?apiKey=${API_KEY}&addRecipeInformation=true&number=100`) - const { results } = axiosResponse.data; +// const axiosResponse = await axios.get(`https://api.spoonacular.com/recipes/complexSearch?apiKey=${API_KEY}&addRecipeInformation=true&number=100`) +// const { results } = axiosResponse.data; - if(results !== 0 ) { - let dishRecipe = await results?.map((e) => { - return { - id: e.id, - name: e.title, - healthScore: e.healthScore, - image: e.image, - banned: e.banned, - summary: e.summary, - //cuisines: e.cuisines?.map(ele => ele), - //dishTypes: e.dishTypes?.map(ele => ele), - diets: e.diets?.map(ele => ele), - createdInDB: false - //ingredients: e.analyzedInstructions[0].steps?.map(ele => ele.ingredients.name): "does not have any ingredient" - } - }) - return dishRecipe - } +// if(results !== 0 ) { +// let dishRecipe = await results?.map((e) => { +// return { +// //id: uuid.v4(), +// id: e.id, +// name: e.title, +// healthScore: e.healthScore, +// image: e.image, +// banned: e.banned, +// summary: e.summary, +// //cuisines: e.cuisines?.map(ele => ele), +// //dishTypes: e.dishTypes?.map(ele => ele), +// diets: e.diets?.map(ele => ele), +// createdInDB: false +// //ingredients: e.analyzedInstructions[0].steps?.map(ele => ele.ingredients.name): "does not have any ingredient" +// } +// }) +// return dishRecipe +// } + +// } catch(error) { +// console.log(error) +// } +// } - } catch(error) { - console.log(error) - } -} const getDBRecipes = async() => { @@ -72,6 +75,7 @@ const getApiNameRecipes = async(name) => { if(results !==0) { let dishName = results?.map((el) => { return { + id: el.id, name: el.title, healthScore: el.healthScore, @@ -124,7 +128,7 @@ const getDBNameRecipes = async(name) => { const getAllInfo = async() => { try { - let data = await getApiRecipes() + let data //await getApiRecipes() let dbData = await getDBRecipes() if(!dbData || dbData.length === 0) { diff --git a/api/src/controllers/usersControllers/PostRanking.controllers.js b/api/src/controllers/usersControllers/PostRanking.controllers.js new file mode 100644 index 0000000..7376461 --- /dev/null +++ b/api/src/controllers/usersControllers/PostRanking.controllers.js @@ -0,0 +1,156 @@ +const { User, Post, Recipe, Ranking } = require("../../db"); + +const getAllPost = async (userId) =>{ + try { + let user = await User.findByPk(userId, { include: Post } ) + let posts = user.posts + let post =[] + // console.log('user',user); + // console.log('posts',posts); + //return await User.findByPk(userId, { include: Post } ) + if (posts) { + await Promise.allSettled( + await posts.map(async p=>{ + let recipe = await Recipe.findByPk(p.recipeId) + post.push({ + userId:p.dataValues.id, + username: user.dataValues.username, + content:p.dataValues.content, + recipeId: recipe.dataValues.id, + recipeImg:recipe.dataValues.image, + recipeName:recipe.dataValues.name, + })})) + console.log('post',post); + return post + } + + } catch (error) { + console.log(error) + }} + +const getRecipePost = async (recipeId) =>{ + try { + let {posts} = await Recipe.findByPk(recipeId, { include: Post } ) + let post = [] + if(posts){ + await Promise.allSettled( + await posts.map( async( p)=>{ + let user = await User.findByPk(p.userId) + post.push( { + username:user.dataValues.username, + post: p.dataValues, + })})) + return post + } + } catch (error) { + console.log(error) + }} + +const createPost =async (userId, content,recipeId) =>{ + try { + let user = await User.findByPk(userId) + let recipe = await Recipe.findByPk(recipeId) + let newPost = await Post.create({ + content, + userId, + recipeId,}) + + await user.addPost(newPost) + await recipe.addPost(newPost) + return await User.findByPk(userId, { include: Post } ) + } catch (error) { + console.log(error) + }} + +const countRanking = async (recipeId) =>{ + try { + let {count, rows} = await Ranking.findAndCountAll({ + where:{recipeId:recipeId} + }) + let sum = 0 + rows.forEach(ele => { + sum = sum + ele.dataValues.ranking + }); + return sum/count + + } catch (error) { + console.log(error) + } +} +const getUserRanking =async(userId , recipeId)=>{ +try { + let rank = await findOne({ + where:{ + userId,recipeId + }}) + return rank +} catch (error) { + console.log(error) +} +} + +const addRanking =async (userId , recipeId, ranking) =>{ + try { + let user = await User.findByPk(userId) + let recipe = await Recipe.findByPk(recipeId) + let rank = await Ranking.findOne({ + where:{ + userId,recipeId + } + }) + if (!user || !recipe) { + throw new Error('need more data') + }else{ + if (rank) { + return await rank.update({ranking:ranking}) + } else { + let newRanking = await Ranking.create({ + ranking, + userId, + recipeId}) + await user.addRanking(newRanking) + await recipe.addRanking(newRanking) + return await Recipe.findByPk(recipeId, { include: Ranking } ) + }} + } catch (error) { + console.log(error) + } +} + +const updatePost =async (postId,content) =>{ + try { + let post = await Post.findByPk(postId) + return await post.update({content:content}) + } catch (error) { + console.log(error) + } +} +const updateRanking =async (ranking, rankingId) =>{ + try { + let rank = await Ranking.findByPk(rankingId) + return await rank.update({ranking:ranking}) + } catch (error) { + console.log(error) + } +} +const deletePost =async (postId) =>{ + try { + let post = await Post.findByPk(postId) + await post.destroy() + return('The post was delete') + } catch (error) { + console.log(error) + } +} + +module.exports ={ + getAllPost, + createPost, + getRecipePost, + getUserRanking, + addRanking, + countRanking, + updateRanking, + updatePost, + deletePost +} \ No newline at end of file diff --git a/api/src/controllers/usersControllers/admin.controllers.js b/api/src/controllers/usersControllers/admin.controllers.js index 211cd0a..5562ca5 100644 --- a/api/src/controllers/usersControllers/admin.controllers.js +++ b/api/src/controllers/usersControllers/admin.controllers.js @@ -1,4 +1,4 @@ -const { User, Favorites } = require("../../db.js"); +const { User, Favorites, Recipe } = require("../../db.js"); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const authConfig = require('../config/auth.js') @@ -89,6 +89,16 @@ const userBanned =async(id,banned) =>{ } } +const recipeBanned =async(id,banned) =>{ + try { + let recipe = await Recipe.findByPk(id) + await recipe.update({banned: banned}) + return recipe + } catch (error) { + console.log(error) + } + } + const defaultList = async (user) =>{ let defList = await Favorites.create({ userId: user.id @@ -103,5 +113,6 @@ module.exports = { usersList, userByid, userByName, - userBanned + userBanned, + recipeBanned } \ No newline at end of file diff --git a/api/src/controllers/usersControllers/notifications/notifications.js b/api/src/controllers/usersControllers/notifications/notifications.js index 72631a9..fede68b 100644 --- a/api/src/controllers/usersControllers/notifications/notifications.js +++ b/api/src/controllers/usersControllers/notifications/notifications.js @@ -18,7 +18,7 @@ if(banned.toString() == "false"){ from: "nutri.u.contact@gmail.com", to: email, subject:"Your nutri-u account has been unbanned", - html:`
Your account has been unbanned for administratornutri.u.contact@gmail.com
`, + html:`Your account has been unbanned for administrator nutri.u.contact@gmail.com
`, }) } @@ -27,7 +27,7 @@ if(banned.toString() == "true") { from: "nutri.u.contact@gmail.com", to: email, subject:"Your nutri-u account has been banned", - html:`You were blocked by a Nutri-u administrator, if you see that this was an error, contact this emailnutri.u.contact@gmail.com
`, + html:`You were blocked by a Nutri-u administrator, if you see that this was an error, contact this email nutri.u.contact@gmail.com
`, }) } @@ -58,9 +58,28 @@ if(email && token){ } +async function paymentNotification(email, recibo){ + let transporter = nodemailer.createTransport({ + host: `${HOST_EMAIL}`, + port:`${PORT_EMAIL}`, + secure:false, + auth:{ + user:`${EMAIL}`, + pass:`${EMAIL_PASS}` + } + }); + return transporter.sendMail({ + from: "nutri.u.contact@gmail.com", + to: email, + subject:"Your suscription to Nutri-U has been successful!", + html:`You are a Premium User now, enjoy!
You can download your invoice here:
${recibo}nutri.u.contact@gmail.com