Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const Question = require('../../../models/question');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. const sgMail = require('@sendgrid/mail');
  4. async function handler(req, res) {
  5. const { method } = req;
  6. await dbConnect();
  7. switch (method) {
  8. case 'POST': {
  9. try {
  10. const question = await Question.create(req.body);
  11. sgMail.setApiKey(process.env.NEXT_PUBLIC_SEND_GRID);
  12. const msg = {
  13. to: 'nikola.tasic@dilig.net', //req.body.email, // Change to your recipient
  14. from: 'nikola.tasic@dilig.net', // Change to your verified sender
  15. subject: 'Question submitted',
  16. text: 'Your question was submitted successfully, we will contact you via email shortly. Thank you!',
  17. html: '<strong>Your question was submitted successfully, we will contact you via email shortly. Thank you!</strong>',
  18. };
  19. sgMail
  20. .send(msg)
  21. .then(() => {
  22. console.log('Email sent');
  23. })
  24. .catch((error) => {
  25. res.status(400).json({ message: error });
  26. });
  27. res.status(201).json({
  28. message:
  29. 'Your message/question was submitted successfully, check your mail for confirmation.',
  30. question,
  31. });
  32. } catch (error) {
  33. res.status(400).json({ message: error });
  34. }
  35. break;
  36. }
  37. default:
  38. res.status(405).json({ message: 'Method not allowed' });
  39. break;
  40. }
  41. }
  42. export default handler;