Merge pull request #17 from MR-medreport/B2B-65
B2B-65: send company offer email
This commit is contained in:
42
lib/services/mailer.service.ts
Normal file
42
lib/services/mailer.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
'use server';
|
||||
|
||||
import { getMailer } from '@kit/mailers';
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
|
||||
import { CompanySubmitData } from '../types/company';
|
||||
import { emailSchema } from '../validations/email.schema';
|
||||
|
||||
export const sendCompanyOfferEmail = async (
|
||||
data: CompanySubmitData,
|
||||
language: string,
|
||||
) => {
|
||||
const { renderCompanyOfferEmail } = await import('@kit/email-templates');
|
||||
const { html, subject, to } = await renderCompanyOfferEmail({
|
||||
language,
|
||||
companyData: data,
|
||||
});
|
||||
|
||||
await sendEmail({
|
||||
subject,
|
||||
html,
|
||||
to,
|
||||
});
|
||||
};
|
||||
|
||||
export const sendEmail = enhanceAction(
|
||||
async ({ subject, html, to }) => {
|
||||
const mailer = await getMailer();
|
||||
|
||||
await mailer.sendEmail({
|
||||
to,
|
||||
subject,
|
||||
html,
|
||||
});
|
||||
|
||||
return {};
|
||||
},
|
||||
{
|
||||
schema: emailSchema,
|
||||
auth: false,
|
||||
},
|
||||
);
|
||||
@@ -1,31 +1,26 @@
|
||||
"use server";
|
||||
'use server';
|
||||
|
||||
import * as yup from "yup";
|
||||
import { companySchema } from "@/lib/validations/companySchema";
|
||||
import { companySchema } from '@/lib/validations/companySchema';
|
||||
|
||||
export async function submitCompanyRegistration(formData: FormData) {
|
||||
const data = {
|
||||
companyName: formData.get("companyName")?.toString() || "",
|
||||
contactPerson: formData.get("contactPerson")?.toString() || "",
|
||||
email: formData.get("email")?.toString() || "",
|
||||
phone: formData.get("phone")?.toString() || "",
|
||||
companyName: formData.get('companyName')?.toString() || '',
|
||||
contactPerson: formData.get('contactPerson')?.toString() || '',
|
||||
email: formData.get('email')?.toString() || '',
|
||||
phone: formData.get('phone')?.toString() || '',
|
||||
};
|
||||
|
||||
try {
|
||||
await companySchema.validate(data, { abortEarly: false });
|
||||
const result = companySchema.safeParse(data);
|
||||
|
||||
console.log("Valid data:", data);
|
||||
} catch (validationError) {
|
||||
if (validationError instanceof yup.ValidationError) {
|
||||
const errors = validationError.inner.map((err) => ({
|
||||
path: err.path,
|
||||
message: err.message,
|
||||
}));
|
||||
throw new Error(
|
||||
"Validation failed: " +
|
||||
errors.map((e) => `${e.path}: ${e.message}`).join(", ")
|
||||
);
|
||||
}
|
||||
throw validationError;
|
||||
if (!result.success) {
|
||||
const errors = result.error.errors.map((err) => ({
|
||||
path: err.path.join('.'),
|
||||
message: err.message,
|
||||
}));
|
||||
|
||||
throw new Error(
|
||||
'Validation failed: ' +
|
||||
errors.map((e) => `${e.path}: ${e.message}`).join(', '),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import * as yup from "yup";
|
||||
import { z } from 'zod';
|
||||
|
||||
export const companySchema = yup.object({
|
||||
companyName: yup.string().required("Company name is required"),
|
||||
contactPerson: yup.string().required("Contact person is required"),
|
||||
email: yup.string().email("Invalid email").required("Email is required"),
|
||||
phone: yup.string().optional(),
|
||||
export const companySchema = z.object({
|
||||
companyName: z.string({
|
||||
required_error: 'Company name is required',
|
||||
}),
|
||||
contactPerson: z.string({
|
||||
required_error: 'Contact person is required',
|
||||
}),
|
||||
email: z
|
||||
.string({
|
||||
required_error: 'Email is required',
|
||||
})
|
||||
.email('Invalid email'),
|
||||
phone: z.string().optional(),
|
||||
});
|
||||
|
||||
7
lib/validations/email.schema.ts
Normal file
7
lib/validations/email.schema.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const emailSchema = z.object({
|
||||
to: z.string().email(),
|
||||
subject: z.string().min(1).max(200),
|
||||
html: z.string().min(1).max(5000),
|
||||
});
|
||||
Reference in New Issue
Block a user