HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago-api-updates/src/helper/notification.js
import { Op } from "sequelize";
import { messages } from "../config/response.messages.js";
import User from "../models/users.model.js";
import Cafes from "../models/cafes.model.js";
import GroupCoffeeRunDB from "../models/group_coffee_run.model.js";
import firebaseAdmin from "./firebase/firebase_admin.js";

const notification = {};

// notification.createCoffeeRunNotification = async (params) => {
//   let title = messages.coffe_run_invite;
//   let body =
//     "Hey there, " +
//     params.loggedInUser +
//     " " +
//     messages.invited_to_join_group_coffee_run;

//   let userData = await User.findAll({
//     where: {
//       id: params.userIds,
//       // device_type: {
//       //     [Op.in]: [1, 2]
//       // },
//       notification_status: 1,
//     },
//     attributes: ["fcm_token"],
//   });

//   if (userData.length == 0) {
//     return;
//   }

//   let tokens = userData
//     .filter((e) => e && e.dataValues.fcm_token)
//     .map((e) => e.dataValues.fcm_token);

//   const payload = {
//     tokens: tokens,
//     title: title,
//     body: body,
//     data: {
//       title: title,
//       message: body,
//       notification_type: `2`,
//       cafe_id: `${params.cafe_id}`,
//       group_id: `${params.group_id}`,
//     },
//   };

//   firebaseAdmin.sendNotification(payload);
// };

notification.createCoffeeRunNotification = async (params) => {
  let title = messages.coffe_run_invite;

  const userData = await User.findAll({
    where: {
      id: params.userIds,
      notification_status: 1,
    },
    attributes: ["id", "fcm_token"],
  });

  if (userData.length === 0) return;

  // Fetch contributors
  const contributorData = await GroupCoffeeRunDB.findAll({
    where: {
      request_created_by: params.loggedInUserId, // Pass this extra param from caller
      request_unique_id: params.request_unique_id,
      is_contributor_person: 1,
      user_id: {
        [Op.ne]: params.loggedInUserId,
      },
    },
    attributes: ["user_id"],
  });

  const contributorIds = contributorData.map((e) => e.user_id);
  for (const user of userData) {
    const message = contributorIds.includes(user.id)
      ? `${params.loggedInUser} will be covering your order! Choose what you want.`
      : `Hey there, ${params.loggedInUser} ${messages.invited_to_join_group_coffee_run}`;

    const payload = {
      tokens: [user.fcm_token],
      title: title,
      body: message,
      data: {
        title: title,
        message: message,
        notification_type: `2`,
        cafe_id: `${params.cafe_id}`,
        group_id: `${params.group_id}`,
      },
    };

    await firebaseAdmin.sendNotification(payload);
  }
};

notification.payOrderNotification = async (params) => {
  let senderName = await User.findOne({
    where: {
      id: params.senderId,
    },
    attributes: ["name"],
  });

  let userData = await User.findOne({
    where: {
      id: params.loggedInUser,
      // device_type: {
      //     [Op.in]: [1, 2]
      // },
      notification_status: 1,
    },
    attributes: ["fcm_token"],
  });

  let title = `${senderName.name} has made his order for coffee run!`;
  let body = `Please pay their order amount.`;

  const payload = {
    tokens: [userData.dataValues.fcm_token],
    title: title,
    body: body,
    data: {
      title: title,
      message: body,
      notification_type: `5`,
      order_id: `${params.orderId}`,
      group_id: `${params.groupId}`,
    },
  };

  firebaseAdmin.sendNotification(payload);
};

notification.requestCoffeeRunNotification = async (params) => {
  let senderName = await User.findOne({
    where: {
      id: params.senderId,
    },
    attributes: ["name"],
  });

  let userData = await User.findAll({
    where: {
      id: params.userIds,
      // device_type: {
      //     [Op.in]: [1, 2]
      // },
      notification_status: 1,
    },
    attributes: ["fcm_token"],
  });

  if (userData.length == 0) {
    return;
  }

  let tokens = userData
    .filter((e) => e && e.dataValues.fcm_token)
    .map((e) => e.dataValues.fcm_token);

  let title = messages.coffe_run_request;
  let body = `${senderName.name} has requested a coffee run`;

  const payload = {
    tokens: tokens,
    title: title,
    body: body,
    data: {
      title: title,
      message: body,
      notification_type: `7`,
      group_id: `${params.groupId}`,
    },
  };

  firebaseAdmin.sendNotification(payload);
};

notification.newOrderNotification = async (params) => {
  let senderName = await User.findOne({
    where: {
      id: params.senderId,
    },
    attributes: ["name"],
  });

  let userData = await Cafes.findOne({
    where: {
      id: params.recieverId,
    },
    attributes: ["fcm_token"],
  });

  let title = `New Order`;
  let body = params.message;

  const payload = {
    tokens: [userData.dataValues.fcm_token],
    title,
    body,
    data: {
      title,
      message: body,
      notification_type: `20`,
      order_id: `${params.orderId}`,
      is_individual_order: `${params.is_individual_order}`,
    },
  };

  // If `estimated_arival_time` is present, add it to data
  if (
    params.estimated_arival_time == null &&
    params.estimated_arival_time == undefined
  ) {
    // Only add apns if estimated_arival_time is null or undefined
    payload.apns = {
      payload: {
        aps: {
          sound: "siren-alert-96052.wav",
        },
      },
      headers: {
        "apns-priority": "10",
      },
    };
  }

  const newOrderNotify = firebaseAdmin.sendNotification(payload);
  console.log("New order notify ", newOrderNotify);
  console.log("payload", payload);
};

export default notification;