Files
estate-landing-page/server/app.js
T
2023-05-24 20:46:10 +05:00

117 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import express from "express";
import cors from "cors";
import ky from "ky";
import mongoose from "mongoose";
import nodemailer from "nodemailer";
import Mail from "./models/Mail.js";
const app = express();
const port = 3003;
app.use(cors());
app.use(express.json());
app.get("/regions", async (req, res) => {
let regions = [];
const result = await ky
.get(
"https://xn--80az8a.xn--d1aqf.xn--p1ai/аналитика/квартирография/api/dictionaries/regions"
)
.json();
regions = result
.map((item) => item.regions)
.flat()
.sort((a, b) => (a.name > b.name ? 1 : -1))
.filter((region) => {
if (![79, 41, 51, 83, 6, 87].includes(region.id)) {
return region;
}
});
console.log(regions);
res.send(regions);
});
app.get("/region/:id", async (req, res) => {
try {
const result = await ky
.get(
`https://наш.дом.рф/аналитика/квартирография/api/dashboard/by-room-count?regions=${req.params.id}`
)
.json();
const result2 = await ky
.get(
`https://наш.дом.рф/аналитика/api/rpp/?regionCode=${req.params.id}&repMonth=3&repYear=2023`
)
.json();
const result3 = await ky
.get(
`https://наш.дом.рф/аналитика/api/developers/dashboard?sort=objSquareLivingAmt:desc&subjectTypes=region&region=${req.params.id}&date=01-04-2023&limit=0`
)
.json();
const { totalArea, flatCount } = result;
const avgApartmentArea = Math.round(totalArea / flatCount);
const priceAvg = Math.round(result2.data.total[0].priceAvg / 1000);
const { objSquareLivingAmt, objCnt } = result3.data.total;
const avgAreaInComplex = Math.round(objSquareLivingAmt / objCnt);
res.send({ avgApartmentArea, priceAvg, avgAreaInComplex });
} catch (error) {
res.send({ error: 1 });
}
});
app.post("/mail", async (req, res) => {
const { fullname, email, company, phone } = req.body;
await Mail.create(req.body);
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "mail.netangels.ru",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "test@graff.tech", // generated ethereal user
pass: "ZmL0pKiDFWUyCDMq", // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: `${email}`, // sender address
to: "info@graff.tech", // list of receivers
subject: 'Заявка с сайта estate.graff.tech', // Subject line
text: `
Имя Фамилия: ${fullname}
Телефон: ${phone}
Компания: ${company}
`, // plain text body
html: `<div>
<p>Имя Фамилия: ${fullname}</p>
<p>Телефон: ${phone}</p>
<p>Компания: ${company}</p>
</div>`, // html body
});
console.log(info);
res.send({ ok: 1 });
});
app.listen(port, async () => {
await mongoose.connect(
"mongodb://192.168.1.159:27017/",
{ dbName: "estate" },
console.log("DB connection...")
);
console.log(`Server is started on port ${port}...`);
});