| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- const Question = require('../../../models/question');
- import dbConnect from '../../../utils/helpers/dbHelpers';
- const sgMail = require('@sendgrid/mail');
- import type { NextApiRequest, NextApiResponse } from 'next';
- import {
- QuestionDataDB,
- QuestionResponse,
- } from '../../../utils/interface/questionInterface';
-
- async function handler(
- req: NextApiRequest,
- res: NextApiResponse<QuestionResponse>
- ) {
- const { method } = req;
- await dbConnect();
-
- switch (method) {
- case 'POST': {
- try {
- const question: QuestionDataDB = await Question.create(req.body);
-
- sgMail.setApiKey(process.env.NEXT_PUBLIC_SEND_GRID);
-
- const msg = {
- to: '[email protected]', //req.body.email, // Change to your recipient
- from: '[email protected]', // Change to your verified sender
- subject: 'Question submitted',
- text: 'Your question was submitted successfully, we will contact you via email shortly. Thank you!',
- html: '<strong>Your question was submitted successfully, we will contact you via email shortly. Thank you!</strong>',
- };
-
- sgMail.send(msg).then(() => {
- console.log('Email sent');
- });
-
- res.status(201).json({
- message:
- 'Your message/question was submitted successfully, check your mail for confirmation.',
- question,
- });
- } catch (error) {
- if (error instanceof Error)
- res.status(400).json({ message: error.message });
- else res.status(400).json({ message: 'Unexpected error' + error });
- }
- break;
- }
- default:
- res.status(405).json({ message: 'Method not allowed' });
- break;
- }
- }
-
- export default handler;
|