202 lines
6.1 KiB
TypeScript
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 { Request, Response } from 'express';
import mailer from '../../../services/mailer';
import { formatter } from '../../../utils';
import { ThemedCombo } from '../rate/controller';
type OrderBody = {
combo?: ThemedCombo;
date?: string;
info: {
date: string;
memberQuantity: number;
price: number;
quest: string;
rate?: string;
time?: string;
};
name: string;
page?: 'birthday' | 'graduation' | 'master-classes' | 'outdoors' | 'show-programs';
phoneNumber: string;
rate?: {
price?: number;
pricePerPerson?: number;
title: string;
};
text?: string;
type: 'calculator' | 'callback' | 'maintenance' | 'newRate' | 'quest-calculator' | 'themed-combo';
};
export const getNewOrder = async (req: Request<any, any, OrderBody>, res: Response) => {
const {
combo,
date,
info,
name,
page,
phoneNumber,
rate,
text,
type,
} = req.body;
const getSubject = () => {
switch (type) {
case 'callback':
return 'Заявка на звонок';
case 'newRate':
return 'Заявка на комбо';
case 'calculator':
return 'Калькулятор праздника';
case 'quest-calculator':
return 'Калькулятор квеста';
case 'themed-combo':
return 'Заявка на тематическое комбо';
case 'maintenance':
return 'Заявка на сопровождение детей';
default:
return 'Нужно связаться';
}
};
const getHtml = () => {
const getPageTitle = () => {
if (page === 'birthday') {
return 'День рождения';
}
if (page === 'graduation') {
return 'Выпускной';
}
if (page === 'master-classes') {
return 'Мастер-классы';
}
if (page === 'show-programs') {
return 'Шоу-программы';
}
return 'Выездной праздник';
};
switch (type) {
case 'callback':
return `
<p>Имя: <b>${name}</b></p>
<p>Телефон: <b>${phoneNumber}</b></p>
`;
case 'newRate':
return `
<p>Имя: <b>${name}</b></p>
<p>Телефон: <b>${phoneNumber}</b></p>
<p>Страница: <b>${getPageTitle()}</b></p>
<p>Пакет: <b>${rate?.title}</b></p>
<p>Цена пакета: <b>${formatter.format(rate?.price || rate?.pricePerPerson || 0)}</b></p>
`;
case 'calculator':
return `
<p>Имя: <b>${name}</b></p>
<p>Телефон: <b>${phoneNumber}</b></p>
<p>Страница: <b>${getPageTitle()}</b></p>
<p>Комбо: <b>${info.rate}</b></p>
<p>Квест: <b>${info.quest}</b></p>
<p>Кол-во людей: <b>${info.memberQuantity}</b></p>
<p>Дата: <b>${info.date}</b></p>
<p>Стоимость: <b>${formatter.format(info.price)}</b></p>
`;
case 'quest-calculator':
return `
<p>Имя: <b>${name}</b></p>
<p>Телефон: <b>${phoneNumber}</b></p>
<p>Квест: <b>${info.quest}</b></p>
<p>Кол-во людей: <b>${info.memberQuantity}</b></p>
<p>Дата: <b>${info.date}</b></p>
<p>Время: <b>${info.time}</b></p>
<p>Стоимость: <b>${formatter.format(info.price)}</b></p>
`;
case 'themed-combo':
return `
<p>Имя: <b>${name}</b></p>
<p>Телефон: <b>${phoneNumber}</b></p>
<p>Комбо: <b>${combo?.title}</b></p>
<p>Стоимость: <b>${formatter.format(combo?.price || 0)}</b></p>
`;
case 'maintenance':
return `
<p>Имя: <b>${name}</b></p>
<p>Телефон: <b>${phoneNumber}</b></p>
<p>Формат мероприятия: <b>${text}</b></p>
<p>Дата: <b>${date}</b></p>
`;
default:
return 'ошибка';
}
};
async function main() {
// eslint-disable-next-line @typescript-eslint/no-shadow
const info = await mailer.sendMail({
from: '"Arkids Test" <ksv741-test@yandex.ru>',
html: getHtml(),
subject: getSubject(),
to: 'ksv741-test@yandex.ru',
});
switch (type) {
case 'callback':
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо с просьбой перезвонить: %s \x1b[0m', info.messageId);
break;
case 'newRate':
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо с заявкой на комбо: %s\x1b[0m', info.messageId);
break;
case 'calculator':
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо с калькулятором праздника: %s\x1b[0m', info.messageId);
break;
case 'quest-calculator':
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо с калькулятором праздника: %s\x1b[0m', info.messageId);
break;
case 'themed-combo':
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо с заявкой на тематическое комбо: %s\x1b[0m', info.messageId);
break;
case 'maintenance':
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо с заявкой на сопровождение детей: %s\x1b[0m', info.messageId);
break;
default:
// eslint-disable-next-line no-console
console.log('\x1b[33m Отправлено письмо: %s\x1b[0m', info.messageId);
}
}
main()
.then(() => res.status(200).json({}))
.catch((e) => {
// eslint-disable-next-line no-console
console.log('Mailer error', e);
});
};