diff --git a/index.ts b/index.ts index df09d60..dbbafaf 100644 --- a/index.ts +++ b/index.ts @@ -1,26 +1,25 @@ -import express from 'express'; -import cors from 'cors'; import * as path from 'path'; -import apiRouter from './src/api'; + import bodyParser from 'body-parser'; +import cors from 'cors'; +import express from 'express'; + +import apiRouter from './src/api'; const app = express(); const port = 5000; -app.use(cors()) -app.use('/static', express.static(path.join(__dirname, 'static'))) -// app.use(express.static(path.join(__dirname, 'static'))) -// @ts-ignore +app.use(cors()); +app.use('/static', express.static(path.join(__dirname, 'static'))); + app.use(bodyParser.raw({ type: 'application/soap+xml' })); app.use(bodyParser.json({ limit: '20mb' })); -// app.use(express.urlencoded({ extended: false })); -// @ts-ignore + app.use(express.json()); -app.use('/api', apiRouter) - +app.use('/api', apiRouter); app.listen(port, () => { + // eslint-disable-next-line no-console console.log(`Arkids backend app start on port ${port}`); }); - diff --git a/package.json b/package.json index 74c6a0b..5c84a63 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "body-parser": "1.20.2", "cors": "2.8.5", "express": "4.18.2", + "nodemailer": "^6.9.3", "nodemon": "2.0.22", "ts-node": "10.9.1", "typescript": "5.0.4", diff --git a/src/api/index.ts b/src/api/index.ts index 223c460..cfbf983 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -13,6 +13,7 @@ import servicesRouter from './routes/services'; import statisticsRouter from './routes/statistic'; import storiesRouter from './routes/stories'; import textRouter from './routes/text'; +import orderRouter from './routes/order'; const apiRouter = express.Router(); @@ -27,5 +28,6 @@ apiRouter.use(ApiRoute.STORIES, storiesRouter); apiRouter.use(ApiRoute.STATISTIC_INFO, statisticsRouter); apiRouter.use(ApiRoute.QUESTS, questRouter); apiRouter.use(ApiRoute.IMAGE, imageRouter); +apiRouter.use(ApiRoute.ORDER, orderRouter); export default apiRouter; diff --git a/src/api/routes/order/controller.ts b/src/api/routes/order/controller.ts new file mode 100644 index 0000000..7783735 --- /dev/null +++ b/src/api/routes/order/controller.ts @@ -0,0 +1,93 @@ +import { Request, Response } from 'express'; + +import mailer from '../../../services/mailer'; +import { formatter } from '../../../utils'; + +export const getNewOrder = async (req: Request, res: Response) => { + // @ts-ignore + const { + name, + page, + phoneNumber, + rate, + type, + } = req.body; + + const getSubject = () => { + switch (type) { + case 'callback': + return 'Заявка на звонок'; + + case 'newRate': + return 'Заявка на комбо'; + + default: + return 'Нужно связаться'; + } + }; + + const getHtml = () => { + const getPageTitle = () => { + if (page === 'birthday') { + return 'День рождения'; + } + + if (page === 'graduation') { + return 'Выпускной'; + } + + return 'Выездной праздник'; + }; + switch (type) { + case 'callback': + return ` +

Имя: ${name}

+

Телефон: ${phoneNumber}

+ `; + + case 'newRate': + return ` +

Имя: ${name}

+

Телефон: ${phoneNumber}

+

Страница: ${getPageTitle()}

+

Пакет: ${rate.title}

+

Цена пакета: ${formatter.format(rate.price || rate.pricePerPerson)}

+ `; + + default: + return 'ошибка'; + } + }; + + async function main() { + const info = await mailer.sendMail({ + from: '"Arkids Test" ', + html: getHtml(), + subject: getSubject(), + to: 'ksv741-test@yandex.ru', + }); + + switch (type) { + case 'callback': + // eslint-disable-next-line no-console + console.log('Отправлено письмо с просьбой перезвонить: %s', info.messageId); + break; + + case 'newRate': + // eslint-disable-next-line no-console + console.log('Отправлено письмо с заявкой на комбо: %s', info.messageId); + break; + + default: + // eslint-disable-next-line no-console + console.log('Отправлено письмо: %s', info.messageId); + } + } + + main() + .then(() => res.status(200).json({})) + .catch((e) => { + // eslint-disable-next-line no-console + console.log('Mailer error', e); + }); +}; diff --git a/src/api/routes/order/index.ts b/src/api/routes/order/index.ts new file mode 100644 index 0000000..b81a3fd --- /dev/null +++ b/src/api/routes/order/index.ts @@ -0,0 +1,9 @@ +import express, { Request, Response } from 'express'; + +import { getNewOrder } from './controller'; + +const router = express.Router(); + +router.post('/', (req: Request, res: Response) => getNewOrder(req, res)); + +export default router; diff --git a/src/constants/index.ts b/src/constants/index.ts index db0c358..6b32640 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -125,6 +125,7 @@ export enum ApiRoute { STATISTIC_INFO = '/statistic', STORIES = '/stories', TEXT = '/text', + ORDER = '/order', } export const servicesList: { [k in SERVICE]: ServiceType } = { diff --git a/src/services/mailer/index.ts b/src/services/mailer/index.ts new file mode 100644 index 0000000..e55566c --- /dev/null +++ b/src/services/mailer/index.ts @@ -0,0 +1,14 @@ +// @ts-ignore +import nodemailer from 'nodemailer'; + +const mailer = nodemailer.createTransport({ + auth: { + pass: 'vytjrxwzqcnmbpja', + user: 'ksv741-test@yandex.ru', + }, + host: 'smtp.yandex.ru', + port: 465, + secure: true, +}); + +export default mailer; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..7958e4c --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,5 @@ +export const formatter = new Intl.NumberFormat('ru-RU', { + currency: 'RUB', + maximumFractionDigits: 0, + style: 'currency', +}); diff --git a/yarn-offline-mirror/nodemailer-6.9.3.tgz b/yarn-offline-mirror/nodemailer-6.9.3.tgz new file mode 100644 index 0000000..6e4e780 Binary files /dev/null and b/yarn-offline-mirror/nodemailer-6.9.3.tgz differ diff --git a/yarn.lock b/yarn.lock index bc3be94..50430cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1669,6 +1669,11 @@ negotiator@0.6.3: resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd" integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg== +nodemailer@^6.9.3: + version "6.9.3" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.9.3.tgz#e4425b85f05d83c43c5cd81bf84ab968f8ef5cbe" + integrity sha512-fy9v3NgTzBngrMFkDsKEj0r02U7jm6XfC3b52eoNV+GCrGj+s8pt5OqhiJdWKuw51zCTdiNR/IUD1z33LIIGpg== + nodemon@2.0.22: version "2.0.22" resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-2.0.22.tgz#182c45c3a78da486f673d6c1702e00728daf5258"