const axios = require('axios'); module.exports = Self => { Self.remoteMethod('send', { description: 'Send notifications from queue', accessType: 'WRITE', returns: { type: 'object', root: true }, http: { path: `/send`, verb: 'POST' } }); Self.send = async options => { // const headers = ctx.req.headers; // const origin = headers.origin || 'http://' + headers.host; // const auth = ctx.req.accessToken; // console.log(origin); const models = Self.app.models; const status = 'pending'; const myOptions = {}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } const notificationQueue = await models.NotificationQueue.find({ where: {status: status}, include: [ { relation: 'notification', scope: { include: { relation: 'subscription', scope: { include: { relation: 'user', scope: { fields: ['name'] } } } } } } ] }, myOptions); const statusSent = 'sent'; const statusError = 'error'; for (const queue of notificationQueue) { // console.log(queue); // console.log(origin); // console.log(auth); // console.log(queue.notification().name); // console.log(queue.params); // const queueParams = Object.assign({}, JSON.parse(queue.params)); // queueParams.authorization = auth.id; try { // await print axios.get(`print`, queueParams) await queue.updateAttribute('status', statusSent); } catch (error) { await queue.updateAttribute('status', statusError); } } return notificationQueue; }; };