forked from Jeyhun085/Pekin-E-Commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (86 loc) · 2.6 KB
/
index.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
import express from "express";
import axios from "axios";
import cors from "cors";
import mongoose from "mongoose";
import { config, mongoURI } from "./token.js";
const urls = {
sectionUrl:
"https://online.moysklad.ru/api/remap/1.2/entity/product/metadata/attributes/450ea93d-7d1a-11ec-0a80-07540034ad7a",
modelUrl:
"https://online.moysklad.ru/api/remap/1.2/entity/product/metadata/attributes/2bcc7ebb-78f0-11ec-0a80-0c3000047732",
filterUrl:
"https://online.moysklad.ru/api/remap/1.2/entity/assortment?filter",
};
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(cors());
app.set("view engine", "ejs");
app.post("/", express.static("public"), async (req, res) => {
const model = req.body.model;
const section = req.body.section;
if (model === "") {
res.send(null);
} else {
const getItems = async () => {
try {
const response = await axios.get(
`${urls.filterUrl}=${urls.modelUrl}~${model};${urls.sectionUrl}=${section}`,
config
);
return response.data.rows;
} catch (err) {
console.error(err);
}
};
const items = await getItems();
const data = items.map((item) => {
const officialName =
item.attributes.find(
(attribute) => attribute.id === "76d741ff-bce9-11ec-0a80-0b4000127eff"
) === undefined
? "Adi heleki yoxdu"
: item.attributes.find(
(attribute) =>
attribute.id === "76d741ff-bce9-11ec-0a80-0b4000127eff"
).value;
return {
article: item.article,
name: officialName,
price: item.salePrices[0].value / 100,
available: item.stock > 0 ? true : false,
inTransit: item.inTransit > 0 ? true : false,
};
});
console.log(data.length);
res.send(data);
}
});
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
const ordersSchema = new Schema({
author: ObjectId,
firstName: String,
lastName: String,
email: String,
city: String,
items: String,
phoneNumber: Number,
date: Date,
});
app.post("/checkout", async (req, res) => {
console.log(req.body);
await mongoose.connect(mongoURI);
const Order = mongoose.model("Order", ordersSchema);
const newOrder = new Order({
firstName: JSON.stringify(req.body.firstName),
lastName: JSON.stringify(req.body.lastName),
email: JSON.stringify(req.body.email),
city: JSON.stringify(req.body.address),
phoneNumber: req.body.phone,
items: JSON.stringify(req.body.mehsullar),
date: new Date(),
});
newOrder.save();
});
app.listen(8080);