mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +01:00
send mail
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import * as mongoose from 'mongoose'
|
import * as mongoose from 'mongoose'
|
||||||
|
|
||||||
import { getDays, dateFormat } from '../helpers/date'
|
import { getDays, dateFormatBackend } from '../helpers/date'
|
||||||
|
|
||||||
const BookingSchema = new mongoose.Schema(
|
const BookingSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
@@ -12,10 +12,15 @@ const BookingSchema = new mongoose.Schema(
|
|||||||
startDate: {
|
startDate: {
|
||||||
type: Date,
|
type: Date,
|
||||||
required: true,
|
required: true,
|
||||||
get: dateFormat,
|
get: dateFormatBackend,
|
||||||
|
min: new Date(),
|
||||||
|
},
|
||||||
|
endDate: {
|
||||||
|
type: Date,
|
||||||
|
required: false,
|
||||||
|
get: dateFormatBackend,
|
||||||
min: new Date(),
|
min: new Date(),
|
||||||
},
|
},
|
||||||
endDate: { type: Date, required: false, get: dateFormat, min: new Date() },
|
|
||||||
status: {
|
status: {
|
||||||
type: String,
|
type: String,
|
||||||
enum: ['requested', 'confirmed', 'rejected'],
|
enum: ['requested', 'confirmed', 'rejected'],
|
||||||
|
|||||||
33
helpers/mail.js
Normal file
33
helpers/mail.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY
|
||||||
|
const SENDGRID_URL = 'https://api.sendgrid.com/v3/mail/send'
|
||||||
|
const BOOKING_ADMIN_EMAIL = 'tomru@mail.id0.link'
|
||||||
|
const FROM_EMAIL = 'thomasruoff@gmail.com'
|
||||||
|
|
||||||
|
async function sendMail(data) {
|
||||||
|
const response = await fetch(SENDGRID_URL, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${SENDGRID_API_KEY}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
})
|
||||||
|
return response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendReceivedBookingMail(booking) {
|
||||||
|
const data = {
|
||||||
|
personalizations: [
|
||||||
|
{
|
||||||
|
to: [{ email: BOOKING_ADMIN_EMAIL }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
from: { email: FROM_EMAIL },
|
||||||
|
subject: 'Pfadi Bussle - Buchung eingegangen!',
|
||||||
|
content: [
|
||||||
|
{ type: 'text/plain', value: `${JSON.stringify(booking, null, 4)}` },
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
await sendMail(data)
|
||||||
|
}
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
import { createBooking } from '../../db/index'
|
import { createBooking } from '../../db/index'
|
||||||
import { Error } from 'mongoose'
|
import { Error } from 'mongoose'
|
||||||
|
|
||||||
|
import { sendReceivedBookingMail } from '../../helpers/mail'
|
||||||
|
|
||||||
export default async function userHandler(req, res) {
|
export default async function userHandler(req, res) {
|
||||||
const { method } = req
|
const { method } = req
|
||||||
|
|
||||||
|
let booking
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'POST':
|
case 'POST':
|
||||||
try {
|
try {
|
||||||
const data = await createBooking(req.body)
|
booking = await createBooking(req.body)
|
||||||
res.status(200).json(data)
|
res.status(200).json(booking)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
|
||||||
@@ -18,6 +22,9 @@ export default async function userHandler(req, res) {
|
|||||||
}
|
}
|
||||||
res.status(500).end(`Internal Server Error...Guru is meditating...`)
|
res.status(500).end(`Internal Server Error...Guru is meditating...`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await sendReceivedBookingMail(booking)
|
||||||
|
console.log('sent receivedBookingMail')
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
res.setHeader('Allow', ['POST'])
|
res.setHeader('Allow', ['POST'])
|
||||||
|
|||||||
Reference in New Issue
Block a user