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/api.javaapp.co.uk_old/src/helper/index.js
import UserDB from "../models/users.model.js"
import { Op } from "sequelize"
import moment from "moment"
import fs from "fs"
import FCM from "fcm-push"

const fcm = new FCM(process.env.FIREBASE_SERVER_KEY)

export async function getProfilePicture(filename, name = "SH") {
    if (filename === "" || filename === undefined || filename === null) {
        filename =
            "https://ui-avatars.com/api/?name=" +
            name +
            "&rounded=true&background=c39a56&color=fff";
    } else {
        filename = filename;
    }
    filename = filename.replace(/\\/g, "/");
    return filename;
}

export async function generateRandomString(length, isNumber = false) {
    var result = "";
    if (isNumber) {
        var characters = "0123456789";
    } else {
        var characters =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    }
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

export async function writeErrorLog(req, error) {
    const requestURL = req.protocol + "://" + req.get('host') + req.originalUrl;
    const requestBody = JSON.stringify(req.body);
    const date = moment().format('MMMM Do YYYY, h:mm:ss a');
    fs.appendFileSync('errorLog.log', "REQUEST DATE : " + date + "\n" + "API URL : " + requestURL + "\n" + "API PARAMETER : " + requestBody + "\n" + "Error : " + error + "\n\n");
}

export async function sendPushNotification(user_id, title, body, notification_type, otherData) {
    try {
        let userData = await UserDB.findOne({
            where: {
                id: parseInt(user_id),
                device_type: {
                    [Op.in]: [1, 2]
                }
            },
            attributes: [
                'fcm_token', 'device_type'
            ]
        })
        if (userData) {
            let notification_message
            notification_message = {
                registration_ids: [userData.fcm_token],
                data: {
                    title: title,
                    message: body,
                    notification_type: notification_type.toString(),
                    cafe_id: (notification_type == 2) ? (otherData.cafe_id).toString() : '',
                    group_id: (notification_type == 2) ? (otherData.group_id).toString() : ''
                },
                notification: {
                    title: title,
                    body: body,
                    sound: 'default',
                }
            }
            // if (userData.device_type == 1) { // android
            //     notification_message = {
            //         registration_ids: [userData.fcm_token],
            //         data: {
            //             title: title,
            //             message: body,
            //             notification_type: notification_type.toString(),
            //             time: moment.utc().format('h:mm a'),
            //             cafe_id: (notification_type == 2) ? (otherData.cafe_id).toString() : '',
            //             group_id: (notification_type == 2) ? (otherData.group_id).toString() : ''
            //         },
            //         notification: {
            //             title: title,
            //             body: body,
            //             sound: 'default'
            //         },
            //     }
            // } else if (userData.device_type == 2) { // IOS

            // }
            let result = await fcm.send(notification_message)
            console.log(result)
        }
    } catch (error) {
        console.log(error)
    }
}