• File: MassPusher.js
  • Full Path: /var/www/nodejs/Notifications_Imali_API/src/models/MassPusher.js
  • Date Modified: 02/26/2025 1:00 PM
  • File size: 1.12 KB
  • MIME-type: text/plain
  • Charset: utf-8

const async = require("async");
const triggerPush = require("../services/push.service");

class MassPusher{
    
    constructor(listOfTokens,pushData){
        this.pushData=pushData;
        this.success_tokens=[];
        this.failure_tokens=[];
        this.invokeOperation(listOfTokens);
    }
   
    invokeOperation = (listOfTokens) => {
        async.each(listOfTokens,this.sendMassPush,(err,info)=>{
            if(err)
                console.log(this.failure_tokens,'error');
            else
                console.log(this.success_tokens,'success');
        });
    }

    sendMassPush = (token, callback) =>{

        this.pushData.to = token;

        async.waterfall([
            (callback)=>{
                triggerPush(this.pushData)
                .then(()=>{
                    this.success_tokens.push(token);
                    callback();
                })
                .catch(()=>{
                    this.failure_tokens.push(token);
                    callback();
                })
            }
        ],
        function (){
            callback();
        })
        
    }
}

module.exports = MassPusher;