Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 45 additions & 31 deletions packages/mailer/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,44 @@ interface FormData {
waiters_count: string
}

const sendEmail = (formData: FormData) => {
const SOURCE_EMAIL = 'hi@menuo.app'
const DESTINATION_EMAIL = 'hi@menuo.app'
interface SendEmailInput {
from: string
to: string[]
replyTo: string[]
message: string
subject: string
}

const buildEmailParams = (data: SendEmailInput) => {
const emailParams = {
Source: data.from,
ReplyToAddresses: data.replyTo,
Destination: {
ToAddresses: data.to,
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: data.message,
},
},
Subject: {
Charset: 'UTF-8',
Data: data.subject,
},
},
}

return emailParams
}

function sendContactEmail(formData: FormData) {
const from = 'hi@menuo.app'
const to = ['hi@menuo.app']
const {
email,
message,
message: messageFormText,
first_name,
restaurant_name,
last_name,
Expand All @@ -30,9 +61,10 @@ const sendEmail = (formData: FormData) => {
table_list,
waiters_count,
} = formData

const messageData = `
${message}
const replyTo = [email]
const subject = `Nowa wiadomość z formularza menuo od ${restaurant_name}`
const message = `
${messageFormText}

-----------------------------------------------------------------------------
Restauracja: ${restaurant_name}
Expand All @@ -44,33 +76,15 @@ Menu: ${menu}
Lista stolików: ${table_list}
`

const emailParams = {
Source: SOURCE_EMAIL,
ReplyToAddresses: [email],
Destination: {
ToAddresses: [DESTINATION_EMAIL],
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: messageData,
},
},
Subject: {
Charset: 'UTF-8',
Data: `Nowa wiadomość z formularza menuo od ${restaurant_name}`,
},
},
}

return ses.sendEmail(emailParams).promise()
return ses
.sendEmail(buildEmailParams({ from, to, replyTo, message, subject }))
.promise()
}

export const staticSiteMailer: APIGatewayProxyHandler = async (
export const staticSiteMailer: APIGatewayProxyHandler = async function (
event,
_context,
): Promise<APIGatewayProxyResult> => {
): Promise<APIGatewayProxyResult> {
const formData = JSON.parse(event.body)

const headers = {
Expand All @@ -85,7 +99,7 @@ export const staticSiteMailer: APIGatewayProxyHandler = async (
})

try {
await sendEmail(formData)
await sendContactEmail(formData)
} catch (e) {
console.log(e)
return sendMailErrorResponse(e)
Expand Down
9 changes: 9 additions & 0 deletions packages/server/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ provider:
- Effect: 'Allow'
Action:
- 'lambda:*'
- 'ses:SendEmail'
Resource: '*'

functions:
Expand Down Expand Up @@ -195,6 +196,14 @@ functions:
send_notifications:
handler: src/handlers.sendNotifications

send_email_order:
handler: src/handlers.send-email-order.handler
events:
- http:
method: post
path: static-site-mailer
cors: true

# logs:
# handler: src/logs/handlers/log.handler
# events:
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { handler as registerWaiter } from './auth/register-waiter'
export { handler as verifyToken } from './auth/authorizer'
export { handler as me } from './auth/me'
export { handler as sendNotifications } from './notifications/handlers/send-notifications'
export { handler as sendEmailOrder } from './notifications/handlers/send-email-order'
31 changes: 31 additions & 0 deletions packages/server/src/notifications/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export interface SendEmailInput {
from: string
to: string[]
replyTo: string[]
message: string
subject: string
}

export const buildEmailParams = (data: SendEmailInput) => {
const emailParams = {
Source: data.from,
ReplyToAddresses: data.replyTo,
Destination: {
ToAddresses: data.to,
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: data.message,
},
},
Subject: {
Charset: 'UTF-8',
Data: data.subject,
},
},
}

return emailParams
}
39 changes: 39 additions & 0 deletions packages/server/src/notifications/handlers/send-email-order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'source-map-support/register'
import { SES } from 'aws-sdk'

import { response } from 'src/lib/http'
import { buildEmailParams } from '../email'

export const handler = async function (event: any) {
// TODO: Define interface
const { subscriptions, body, title } = event

try {
await sendEmailWithOrder({ table: '1233' })
} catch (e) {
console.log(e)
return response({ kind: 'NOT_FOUND' })
}

return response({ body: event })
}

function sendEmailWithOrder({ table }: { table: string }) {
const from = 'hi@menuo.app'
const to = ['hi@menuo.app']
const replyTo = to
const subject = `Nowe zamówienie (Solar Gliwice) od ${table}`
// TODO: Generate proper message
const message = `
kto kupił
Kod
Nazwa
Marka
ilość szt.
`

const ses = new SES()
return ses
.sendEmail(buildEmailParams({ from, to, replyTo, message, subject }))
.promise()
}
11 changes: 6 additions & 5 deletions packages/server/src/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ export const sendNotification = ({
}: {
title: string
body: string
}) => async (subscription: any) => {
return webPush
.sendNotification(subscription, JSON.stringify({ title, body }))
.catch(error => console.error(error))
}
}) =>
async function (subscription: any) {
return webPush
.sendNotification(subscription, JSON.stringify({ title, body }))
.catch(error => console.error(error))
}

export const sendNotifications = ({
functionName,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/interfaces/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface RestaurantConfig {
MENU_READ_ONLY: boolean
CHANGE_CALL_WAITER_TO_CONTACT: boolean
CHANGE_ORDER_TO_EMAIL: boolean
}