diff --git a/config/routes/main.js b/config/routes/main.js
index a418720..26d1850 100644
--- a/config/routes/main.js
+++ b/config/routes/main.js
@@ -1,4 +1,5 @@
const ObjectID = require('mongodb').ObjectID;
+const nodemailer = require('../nodemailer.config');
module.exports = function (server, db) {
server.get('/', async (req, res) => {
@@ -121,4 +122,34 @@ module.exports = function (server, db) {
res.status(500).render('main/404');
});
});
+ server.post('/my-orders', async (req, res) => {
+ const buyers = await db.collection('buyers').find({email: req.body.email}).toArray();
+ if (buyers.length > 0) {
+ // рендерим html
+ const html = buyers.map((buyer) => {
+ return `
+ Номер заказа: ${buyer.bill_id}
+ Дата оплаты: ${new Date(buyer.date).toLocaleDateString()}
+ Данные товара:
+
${buyer.data.join('
')}
======================================================================
+ `; + }); + + // высылаем данные покупателю + try { + await nodemailer.transporter.sendMail({ + from: `"Ваши покупки." <${process.env.EMAIL_LOGIN}>`, + to: req.body.email, + subject: "Данные купленных товаров находятся в этом письме.", + html: html.join('') + }); + res.json({status: 'OK'}); + } catch (error) { + res.json({status: 'ERR'}); + } + } else { + res.json({status: 'NF'}); + } + }); } diff --git a/dist/views/main/my-orders.pug b/dist/views/main/my-orders.pug index 490da45..943d703 100644 --- a/dist/views/main/my-orders.pug +++ b/dist/views/main/my-orders.pug @@ -6,6 +6,7 @@ include ./assets/templates/header.pug main.main-section include ./assets/templates/modals/cart.pug include ./assets/templates/modals/pay.pug + #notification .container .row .col.s12 @@ -22,7 +23,7 @@ main.main-section form(action='#') .email-field(style='max-width: 400px;margin-bottom: 10px;') label(for='email') Почта - input#email(required='' type='text' name='email') + input#email(required='' type='email' name='email') .show-orders button.waves-effect.waves-light.btn(type='submit') Узнать diff --git a/src/main/components/notification.component.js b/src/main/components/notification.component.js index 7ee9746..4193303 100644 --- a/src/main/components/notification.component.js +++ b/src/main/components/notification.component.js @@ -5,16 +5,16 @@ export class NotificationComponent extends Component { super(id); } - notificate(text, color) { + notificate(text, color, delay) { const card = document.createElement('div'); card.classList.add('card-panel'); card.style.backgroundColor = color; card.innerHTML = `${text}`; this.$el.innerHTML += card.outerHTML; - this.removeNotificate(); + this.removeNotificate(delay); } - removeNotificate() { + removeNotificate(delay) { // Полифилл для IE ^9 (function() { var arr = [window.Element, window.CharacterData, window.DocumentType]; @@ -45,6 +45,6 @@ export class NotificationComponent extends Component { setTimeout(() => { this.$el.querySelector('.card-panel').remove(); - }, 2000) + }, delay || 2000); } } \ No newline at end of file diff --git a/src/main/components/orders.component.js b/src/main/components/orders.component.js new file mode 100644 index 0000000..4d4e2c9 --- /dev/null +++ b/src/main/components/orders.component.js @@ -0,0 +1,40 @@ +import { Component } from "../core/component"; +import axios from "axios"; + +export class OrdersComponent extends Component { + constructor(id, {notification}) { + super(id); + this.notification = notification; + } + + init() { + if (this.$el) { + let valid = true; + this.$el.querySelector('form').onsubmit = async (e) => { + e.preventDefault(); + if (valid) { + valid = false; + const email = e.target.elements.email.value, + request = await axios.post('/my-orders', {email}); + + switch (request.data.status) { + case 'OK': + this.notification.notificate(`Данные были успешно высланы на почту "${email}".`, '#26A69A', 3500); + break; + case 'NF': + this.notification.notificate(`Покупатель с почтой ${email} не был найден.`, '#f44336', 3500); + break; + case 'ERR': + this.notification.notificate(`Произошла ошибка, попробуйте повторить запрос позже.`, '#f44336', 3500); + break; + default: + break; + } + setTimeout(() => valid = true, 10000); + } else { + this.notification.notificate(`Подождите еще нескольно секунд прежде чем сделать запрос...`, '#f44336', 3500); + } + }; + } + } +} \ No newline at end of file diff --git a/src/main/index.js b/src/main/index.js index 4e61b88..aaaa7b3 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -9,6 +9,7 @@ import { CartModal } from "./modals/cart.modal.js"; import { PayModal } from "./modals/pay.modal.js"; import { NotificationComponent } from "./components/notification.component.js"; import { MaterializeConfig } from "./config/materialize.config.js"; +import { OrdersComponent } from "./components/orders.component.js"; // Components const header = new HeaderComponent('header'); @@ -19,6 +20,7 @@ const items = new ItemsComponent('items', {loader}, {notification}); const filter = new FilterComponent('filter', {items}); const category = new CategoryComponent('category', {items}, {filter}); const breadcumb = new BreadcrumbComponent('breadcrumb'); +const orders = new OrdersComponent('orders', {notification}); // Modals const cart = new CartModal('cart', 'cartOpen', 'cartClose', {items});