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-nodeserver-hotfixes/src/helper/notification.js
import { Op } from "sequelize";
import { messages } from "../config/response.messages.js";
import User from "../models/users.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.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 his 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);
}

export default notification;