const moment = require('moment');
const { Expo } = require('expo-server-sdk');
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
const models = require('../../core/models');

const extraMethods = {
    saveNotificationDetails: async function (messages, tickets) {
        return new Promise(function (resolve) {
            messages.forEach(async function (message, index) {            
                let notification = models.NotificationDetail.build({
                    ...message,
                    token: message.to,
                    ticket: tickets[index].id ? tickets[index].id : undefined,
                    deliveryDate: moment(),
                    deliveryStatus: (tickets[index].status === 'error') ? tickets[index].details.error : tickets[index].status,
                    notificationId: message.notificationId,
                });                
                await notification.save();
            });

            resolve(true);
        });
    }    
     
};

module.exports = generateService(models.NotificationDetail, extraMethods);
