<commit_before>import env from 'dotenv';
import Jusibe from 'jusibe';
import nodemailer from 'nodemailer';
import Messages from '../models/message';
import Group from '../models/group';
import { getUsersPhoneEmail } from '../controllers/helper/getUsersPhoneEmail';

env.config();

/**
 * @class ApiController
 */
export default class MessageController {
  /**
 *
 * @param {obj} req
 * @param {obj} res
 * @param {obj} next
 * @return {obj} Returns success message with data or failure message
 */
  static postMessage(req, res) {
    const message = req.body.message,
      priority = req.body.priority,
      groupId = req.params.groupId,
      userId = req.decoded.userId,
      username = req.decoded.username;
    return Messages.sync({ force: false }).then(() => {
      Messages.create({ userId, groupId, message, priority, username }).then((content) => {
        if (priority === 'Critical') {
          // Get users emails and phone numbers
          getUsersPhoneEmail(groupId, (result) => {
            const { phoneNumbers, emails } = result;
            const jusibe = new Jusibe(process.env.PUBLIC_KEY, process.env.ACCESS_TOKEN);
            phoneNumbers.forEach((number) => {
              const payload = {
                to: number,
                from: req.decoded.username,
                message: content.message
              };
              jusibe.sendSMS(payload)
            .then(() => {
            })
            .catch((error) => {
              if (error) {
                throw error;
              }
            });
            });
          // Send emails to users
            const transporter = nodemailer.createTransport({
              service: 'gmail',
              port: 25,
              secure: false, // secure:true for port 465, secure:false for port 587
              auth: {
                user: '<EMAIL>',
                pass: <PASSWORD>
              },
              tls: {
                rejectUnauthorized: false
              }
            });
            const mailOptions = {
              from: `${req.decoded.name} <${req.decoded.email}>`, // sender address
              to: emails.toString(), // list of receivers
              subject: `${priority} message`, // Subject line
              // text: content.message, // plain text body
              html: `<b>${content.message}\n\n This message was sent through PostIt app.</b>`
            };
            transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                throw error;
              }
            });
          });
        } else if (priority === 'Urgent') {
          // Fetch users emails and phone numbers
          getUsersPhoneEmail(groupId, (result) => {
            const { emails } = result;
          // Send emails to users
            const transporter = nodemailer.createTransport({
              service: 'gmail',
              port: 25,
              secure: false, // secure:true for port 465, secure:false for port 587
              auth: {
                user: '<EMAIL>',
                pass: <PASSWORD>
              },
              tls: {
                rejectUnauthorized: false
              }
            });
            const mailOptions = {
              from: `${req.decoded.username} <${req.decoded.email}>`, // sender address
              to: emails.toString(), // list of receivers
              subject: `${priority} message`, // Subject line
              // text: content.message, // plain text body
              html: `<b>${content.message}\n\n This message was sent through PostIt app.</b>`
            };
            transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                throw error;
              }
            });
          });<commit_msg>bug fix<commit_after>import env from 'dotenv';
import Jusibe from 'jusibe';
import nodemailer from 'nodemailer';
import Messages from '../models/message';
import Group from '../models/group';
import { getUsersPhoneEmail } from '../controllers/helper/getUsersPhoneEmail';

env.config();

/**
 * @class ApiController
 */
export default class MessageController {
  /**
 *
 * @param {obj} req
 * @param {obj} res
 * @param {obj} next
 * @return {obj} Returns success message with data or failure message
 */
  static postMessage(req, res) {
    const message = req.body.message,
      priority = req.body.priority,
      groupId = req.params.groupId,
      userId = req.decoded.userId,
      username = req.decoded.username;
    return Messages.sync({ force: false }).then(() => {
      Messages.create({ userId, groupId, message, priority, username }).then((content) => {
        if (priority === 'Critical') {
          // Get users emails and phone numbers
          getUsersPhoneEmail(groupId, (result) => {
            const { phoneNumbers, emails } = result;
            const jusibe = new Jusibe(process.env.PUBLIC_KEY, process.env.ACCESS_TOKEN);
            phoneNumbers.forEach((number) => {
              const payload = {
                to: number,
                from: req.decoded.username,
                message: content.message
              };
              jusibe.sendSMS(payload)
            .then(() => {
            })
            .catch((error) => {
              if (error) {
                throw error;
              }
            });
            });
          // Send emails to users
            const transporter = nodemailer.createTransport({
              service: 'gmail',
              port: 25,
              secure: false, // secure:true for port 465, secure:false for port 587
              auth: {
                user: '<EMAIL>',
                pass: <PASSWORD>
              },
              tls: {
                rejectUnauthorized: false
              }
            });
            const mailOptions = {
              from: `${req.decoded.name} <${req.decoded.email}>`, // sender address
              to: emails.toString(), // list of receivers
              subject: `${priority} message`, // Subject line
              // text: content.message, // plain text body
              html: `<b>${content.message}\n\n This message was sent through PostIt app.</b>`
            };
            transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                throw error;
              }
            });
          });
        } else if (priority === 'Urgent') {
          // Fetch users emails and phone numbers
          getUsersPhoneEmail(groupId, async (result) => {
            const { emails } = await result;
          // Send emails to users
            const transporter = nodemailer.createTransport({
              service: 'gmail',
              port: 25,
              secure: false, // secure:true for port 465, secure:false for port 587
              auth: {
                user: '<EMAIL>',
                pass: <PASSWORD>
              },
              tls: {
                rejectUnauthorized: false
              }
            });
            const mailOptions = {
              from: `${req.decoded.username} <${req.decoded.email}>`, // sender address
              to: emails.toString(), // list of receivers
              subject: `${priority} message`, // Subject line
              // text: content.message, // plain text body
              html: `<b>${content.message}\n\n This message was sent through PostIt app.</b>`
            };
            transporter.sendMail(mailOptions, (error, info) => {
              if (error) {
                throw error;
              }
            });
          });
{'fraction_non_alphanumeric': 0.12226036440454185, 'fraction_numerical': 0.004224980195405334, 'mean_word_length': 1.4983509234828496, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '809110', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1881, 'n_words': 525}