mirror of
https://github.com/IT4Change/IT4C.dev.git
synced 2025-12-13 09:25:49 +00:00
* backend - mail api for it4c Implements an fastify backend service with an email service. This allows to send us emails received via contact form on the website. * optional telephone * missing text delimiter * start command and correct build method to classicjs * deploy for backend & adjust README.md * debug deploy [1] * debug deploy [2] * debug deploy [3] * debug deploy [4] * debug deploy [5] * finish deploy script * watch when running npm run dev * fix format validation * debug sendmail[1] * debug sendmail[2] * debug sendmail[3] * debug sendmail[4] * debug sendmail[5] * env for MAIL_HOST * referece name in email subject * fix format string * eslint * backend build & lint workflows * order comments * unit tests * unit test workflow * prettier * alias paths * fix esm support * 100% tests * corrected nodejs version * use beforeEach to clearAllMocks This simplifies the code and reduces redundancy * fix wrong import
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import util from 'node:util'
|
|
|
|
import { FormatRegistry, Type, TypeBoxValidatorCompiler } from '@fastify/type-provider-typebox'
|
|
import Fastify from 'fastify'
|
|
import { createTransport } from 'nodemailer'
|
|
|
|
import { IsEmail } from './formats'
|
|
|
|
import type { Env } from './env'
|
|
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
|
|
|
|
function createServer(env: Env) {
|
|
// Register EMail format
|
|
FormatRegistry.Set('email', (value) => IsEmail(value))
|
|
|
|
// Nodemailer
|
|
const mailService = createTransport({
|
|
host: env.MAIL_HOST,
|
|
port: 465,
|
|
secure: true,
|
|
tls: {
|
|
rejectUnauthorized: false,
|
|
},
|
|
})
|
|
|
|
// Fatify
|
|
const fastify = Fastify({
|
|
logger: true,
|
|
}).setValidatorCompiler(TypeBoxValidatorCompiler)
|
|
|
|
// Mail
|
|
const schema = {
|
|
body: Type.Object({
|
|
name: Type.String({ minLength: 2, maxLength: 35 }),
|
|
email: Type.String({ format: 'email' }),
|
|
telephone: Type.Optional(Type.String({ minLength: 8 })),
|
|
text: Type.String({ minLength: 5 }),
|
|
}),
|
|
response: {
|
|
200: Type.Object({ success: Type.Boolean({ default: true }) }),
|
|
400: Type.Object({
|
|
success: Type.Boolean({ default: false }),
|
|
error: Type.String(),
|
|
}),
|
|
},
|
|
}
|
|
|
|
fastify
|
|
.withTypeProvider<TypeBoxTypeProvider>()
|
|
.post('/mail', { schema }, async (request, reply) => {
|
|
try {
|
|
await mailService.sendMail({
|
|
to: env.EMAIL_RECEIVER,
|
|
from: `"${request.body.name}" <${request.body.email}>`,
|
|
subject: util.format(env.EMAIL_SUBJECT, request.body.name),
|
|
text: `${request.body.text}${request.body.telephone ? `\n\nTelephone: ${request.body.telephone}` : ''}`,
|
|
})
|
|
return reply.status(200).send({ success: true })
|
|
// eslint-disable-next-line no-catch-all/no-catch-all
|
|
} catch (error) {
|
|
return reply.status(400).send({ success: false, error: error as string })
|
|
}
|
|
})
|
|
return fastify
|
|
}
|
|
|
|
export { createServer }
|