-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
131 lines (96 loc) · 3.63 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import express from 'express'
import cors from "cors"
import compression from "compression"
import mercadopago from "mercadopago"
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fetch from "node-fetch"
import Mongo_db from "./model/db_Mongo.js"
import config from "./config.js"
import routerItem from "./router/item.js"
const PORT = config.PORT
mercadopago.configure({
access_token: "APP_USR-415255069673949-013103-53933e062c46695b33189542f57470bc-92731233",
});
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Mongo_db.conectarDB() //conexion base de datos + feedback
const app = express()
app.use(cors())
app.use(compression());
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use("/items", routerItem)
let listaDB
const fetchItems = async () => { //preguntar a mongo los pares nombre precio para
listaDB = await fetch(`https://resto-tortuga.herokuapp.com/items`).then(r => r.json())
}
app.post("/create_preference", async (req, res) => {
await fetchItems()
console.log(listaDB)
let arrReq = Array.from(req.body)
let arrLocales = []
//setear array preferencias
for (let i = 0; i < arrReq.length; i++) { // setea preferencia de cada item
let producto = {
title: req.body[i].description,
unit_price: Number(req.body[i].price),
quantity: Number(req.body[i].quantity),
}
arrLocales.push(producto)
}
//listas para comparar
let arrLocalComparable = arrLocales.map(prod => JSON.stringify(({ title: prod.title, unit_price: prod.unit_price })))
let arrDBComparable = listaDB.map(prod => JSON.stringify(({ title: prod.name, unit_price: prod.price })))
console.log("lista local: ", arrLocalComparable)
console.log("lista db ", arrDBComparable)
let listaCoincidencias = []
function verificar() {
arrLocalComparable.forEach(localSTR => { //por cada item en la lista
let estaEnDB = arrDBComparable.includes(localSTR)
listaCoincidencias.push(estaEnDB)
});
}
const coincidenTodasEnDB = () => listaCoincidencias.every(ele => ele == true)
verificar()
console.log(listaCoincidencias)
try {
if (coincidenTodasEnDB()) {
console.log(arrLocales)
let preference = {
items: arrLocales,
back_urls: {
"success": "https://resto-tortuga.herokuapp.com/feedback",
"failure": "https://resto-tortuga.herokuapp.com/feedback",
"pending": "https://resto-tortuga.herokuapp.com/feedback"
},
auto_return: "approved",
};
mercadopago.preferences.create(preference)
.then(function (response) {
res.json({
id: response.body.id
});
}).catch(function (error) {
console.log(error);
});
} else {
console.log("existe un item que no concuerda con la DB")
}
} catch {
console.log(error)
}
});
app.get('/feedback', function (req, res) {
let info = {
Payment: req.query.payment_id,
Status: req.query.status,
MerchantOrder: req.query.merchant_order_id
};
console.log(info)
res.redirect("/#carrito")
});
// config
const server = app.listen(PORT, () => console.log(`Servidor express escuchando en el puerto ${PORT}`))
server.on('error', error => console.log(`Error en servidor express: ${error.message}`))