2
0

feat(bot): ️ Add attachment in emails

This commit is contained in:
Baptiste Arnaud
2022-06-24 16:18:07 +02:00
parent 2441abe015
commit 7c3345ab13
5 changed files with 33 additions and 7 deletions

View File

@@ -5,12 +5,14 @@ import {
Flex, Flex,
HStack, HStack,
Switch, Switch,
FormLabel,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { CodeEditor } from 'components/shared/CodeEditor' import { CodeEditor } from 'components/shared/CodeEditor'
import { CredentialsDropdown } from 'components/shared/CredentialsDropdown' import { CredentialsDropdown } from 'components/shared/CredentialsDropdown'
import { SwitchWithLabel } from 'components/shared/SwitchWithLabel' import { SwitchWithLabel } from 'components/shared/SwitchWithLabel'
import { Input, Textarea } from 'components/shared/Textbox' import { Input, Textarea } from 'components/shared/Textbox'
import { CredentialsType, SendEmailOptions } from 'models' import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { CredentialsType, SendEmailOptions, Variable } from 'models'
import React, { useState } from 'react' import React, { useState } from 'react'
import { env } from 'utils' import { env } from 'utils'
import { SmtpConfigModal } from './SmtpConfigModal' import { SmtpConfigModal } from './SmtpConfigModal'
@@ -88,6 +90,14 @@ export const SendEmailSettings = ({ options, onOptionsChange }: Props) => {
isBodyCode: options.isBodyCode ? !options.isBodyCode : true, isBodyCode: options.isBodyCode ? !options.isBodyCode : true,
}) })
const handleChangeAttachmentVariable = (
variable: Pick<Variable, 'id' | 'name'> | undefined
) =>
onOptionsChange({
...options,
attachmentsVariableId: variable?.id,
})
return ( return (
<Stack spacing={4}> <Stack spacing={4}>
<Stack> <Stack>
@@ -179,6 +189,15 @@ export const SendEmailSettings = ({ options, onOptionsChange }: Props) => {
)} )}
</Stack> </Stack>
)} )}
<Stack>
<FormLabel mb="0" htmlFor="variable">
Attachments:
</FormLabel>
<VariableSearchInput
initialVariableId={options.attachmentsVariableId}
onSelectVariable={handleChangeAttachmentVariable}
/>
</Stack>
<SmtpConfigModal <SmtpConfigModal
isOpen={isOpen} isOpen={isOpen}
onClose={onClose} onClose={onClose}

View File

@@ -1,9 +1,9 @@
import prisma from 'libs/prisma' import prisma from 'libs/prisma'
import { import {
PublicTypebot,
ResultValues, ResultValues,
SendEmailOptions, SendEmailOptions,
SmtpCredentialsData, SmtpCredentialsData,
Typebot,
} from 'models' } from 'models'
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { createTransport, getTestMessageUrl } from 'nodemailer' import { createTransport, getTestMessageUrl } from 'nodemailer'
@@ -60,9 +60,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
isBodyCode, isBodyCode,
isCustomBody, isCustomBody,
resultValues, resultValues,
fileUrls,
} = ( } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as SendEmailOptions & { resultValues: ResultValues } ) as SendEmailOptions & {
resultValues: ResultValues
fileUrls?: string
}
const { host, port, isTlsEnabled, username, password, from } = const { host, port, isTlsEnabled, username, password, from } =
(await getEmailInfo(credentialsId)) ?? {} (await getEmailInfo(credentialsId)) ?? {}
@@ -106,6 +110,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
to: recipients, to: recipients,
replyTo, replyTo,
subject, subject,
attachments: fileUrls?.split(', ').map((url) => ({ path: url })),
...emailBody, ...emailBody,
} }
try { try {
@@ -163,9 +168,9 @@ const getEmailBody = async ({
html: isBodyCode ? body : undefined, html: isBodyCode ? body : undefined,
text: !isBodyCode ? body : undefined, text: !isBodyCode ? body : undefined,
} }
const typebot = (await prisma.typebot.findUnique({ const typebot = (await prisma.publicTypebot.findUnique({
where: { id: typebotId }, where: { typebotId },
})) as unknown as Typebot })) as unknown as PublicTypebot
if (!typebot) return if (!typebot) return
const linkedTypebots = await getLinkedTypebots(typebot) const linkedTypebots = await getLinkedTypebots(typebot)
const answers = parseAnswers({ const answers = parseAnswers({

View File

@@ -61,7 +61,7 @@ export const FileUploadForm = ({
files: [ files: [
{ {
file, file,
path: `public/results/${resultId}/${id}`, path: `public/results/${resultId}/${id}/${file.name}`,
}, },
], ],
}) })

View File

@@ -306,6 +306,7 @@ const sendEmail = async (
cc: (options.cc ?? []).map(parseVariables(variables)), cc: (options.cc ?? []).map(parseVariables(variables)),
bcc: (options.bcc ?? []).map(parseVariables(variables)), bcc: (options.bcc ?? []).map(parseVariables(variables)),
replyTo: replyTo !== '' ? replyTo : undefined, replyTo: replyTo !== '' ? replyTo : undefined,
fileUrls: variables.find(byId(options.attachmentsVariableId))?.value,
isCustomBody: options.isCustomBody, isCustomBody: options.isCustomBody,
isBodyCode: options.isBodyCode, isBodyCode: options.isBodyCode,
resultValues, resultValues,

View File

@@ -11,6 +11,7 @@ export const sendEmailOptionsSchema = z.object({
replyTo: z.string().optional(), replyTo: z.string().optional(),
cc: z.array(z.string()).optional(), cc: z.array(z.string()).optional(),
bcc: z.array(z.string()).optional(), bcc: z.array(z.string()).optional(),
attachmentsVariableId: z.string().optional(),
}) })
export const sendEmailBlockSchema = blockBaseSchema.and( export const sendEmailBlockSchema = blockBaseSchema.and(