File: /var/www/api.javaapp.co.uk/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;