2016-03-14 10:20:24 -04:00
|
|
|
import { md5Hash, newObjectId } from './cryptoUtils';
|
2016-06-26 20:50:40 -07:00
|
|
|
import { logger } from './logger';
|
2016-03-13 23:34:44 -04:00
|
|
|
|
2016-04-14 19:24:56 -04:00
|
|
|
const PUSH_STATUS_COLLECTION = '_PushStatus';
|
2016-08-30 07:19:21 -04:00
|
|
|
const JOB_STATUS_COLLECTION = '_JobStatus';
|
2016-04-14 19:24:56 -04:00
|
|
|
|
2016-03-26 11:02:26 -04:00
|
|
|
export function flatten(array) {
|
2016-11-24 15:47:41 -05:00
|
|
|
return array.reduce((memo, element) => {
|
2016-03-26 11:02:26 -04:00
|
|
|
if (Array.isArray(element)) {
|
|
|
|
|
memo = memo.concat(flatten(element));
|
|
|
|
|
} else {
|
|
|
|
|
memo = memo.concat(element);
|
|
|
|
|
}
|
|
|
|
|
return memo;
|
|
|
|
|
}, []);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 07:19:21 -04:00
|
|
|
function statusHandler(className, database) {
|
|
|
|
|
let lastPromise = Promise.resolve();
|
|
|
|
|
|
|
|
|
|
function create(object) {
|
2016-11-24 15:47:41 -05:00
|
|
|
lastPromise = lastPromise.then(() => {
|
|
|
|
|
return database.create(className, object).then(() => {
|
2016-08-30 07:19:21 -04:00
|
|
|
return Promise.resolve(object);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return lastPromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function update(where, object) {
|
2016-11-24 15:47:41 -05:00
|
|
|
lastPromise = lastPromise.then(() => {
|
2016-08-30 07:19:21 -04:00
|
|
|
return database.update(className, where, object);
|
|
|
|
|
});
|
|
|
|
|
return lastPromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.freeze({
|
|
|
|
|
create,
|
|
|
|
|
update
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function jobStatusHandler(config) {
|
|
|
|
|
let jobStatus;
|
2016-12-07 15:17:05 -08:00
|
|
|
const objectId = newObjectId();
|
|
|
|
|
const database = config.database;
|
|
|
|
|
const handler = statusHandler(JOB_STATUS_COLLECTION, database);
|
|
|
|
|
const setRunning = function(jobName, params) {
|
|
|
|
|
const now = new Date();
|
2016-08-30 07:19:21 -04:00
|
|
|
jobStatus = {
|
|
|
|
|
objectId,
|
|
|
|
|
jobName,
|
|
|
|
|
params,
|
|
|
|
|
status: 'running',
|
|
|
|
|
source: 'api',
|
|
|
|
|
createdAt: now,
|
|
|
|
|
// lockdown!
|
|
|
|
|
ACL: {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handler.create(jobStatus);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const setMessage = function(message) {
|
2016-08-30 07:19:21 -04:00
|
|
|
if (!message || typeof message !== 'string') {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
return handler.update({ objectId }, { message });
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const setSucceeded = function(message) {
|
2016-08-30 07:19:21 -04:00
|
|
|
return setFinalStatus('succeeded', message);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const setFailed = function(message) {
|
2016-08-30 07:19:21 -04:00
|
|
|
return setFinalStatus('failed', message);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const setFinalStatus = function(status, message = undefined) {
|
|
|
|
|
const finishedAt = new Date();
|
|
|
|
|
const update = { status, finishedAt };
|
2016-08-30 07:19:21 -04:00
|
|
|
if (message && typeof message === 'string') {
|
|
|
|
|
update.message = message;
|
|
|
|
|
}
|
|
|
|
|
return handler.update({ objectId }, update);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.freeze({
|
|
|
|
|
setRunning,
|
|
|
|
|
setSucceeded,
|
|
|
|
|
setMessage,
|
|
|
|
|
setFailed
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function pushStatusHandler(config) {
|
2016-03-13 23:34:44 -04:00
|
|
|
|
|
|
|
|
let pushStatus;
|
2016-12-07 15:17:05 -08:00
|
|
|
const objectId = newObjectId();
|
|
|
|
|
const database = config.database;
|
|
|
|
|
const handler = statusHandler(PUSH_STATUS_COLLECTION, database);
|
|
|
|
|
const setInitial = function(body = {}, where, options = {source: 'rest'}) {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const data = body.data || {};
|
|
|
|
|
const payloadString = JSON.stringify(data);
|
2016-08-09 18:00:56 +02:00
|
|
|
let pushHash;
|
|
|
|
|
if (typeof data.alert === 'string') {
|
|
|
|
|
pushHash = md5Hash(data.alert);
|
|
|
|
|
} else if (typeof data.alert === 'object') {
|
|
|
|
|
pushHash = md5Hash(JSON.stringify(data.alert));
|
|
|
|
|
} else {
|
|
|
|
|
pushHash = 'd41d8cd98f00b204e9800998ecf8427e';
|
|
|
|
|
}
|
2016-12-07 15:17:05 -08:00
|
|
|
const object = {
|
2016-04-21 21:36:15 -04:00
|
|
|
objectId,
|
|
|
|
|
createdAt: now,
|
2016-03-14 10:20:24 -04:00
|
|
|
pushTime: now.toISOString(),
|
2016-03-13 23:34:44 -04:00
|
|
|
query: JSON.stringify(where),
|
2016-04-12 17:43:46 -04:00
|
|
|
payload: payloadString,
|
2016-03-13 23:34:44 -04:00
|
|
|
source: options.source,
|
|
|
|
|
title: options.title,
|
|
|
|
|
expiry: body.expiration_time,
|
|
|
|
|
status: "pending",
|
|
|
|
|
numSent: 0,
|
2016-08-09 18:00:56 +02:00
|
|
|
pushHash,
|
2016-03-14 10:20:24 -04:00
|
|
|
// lockdown!
|
2016-04-21 21:36:15 -04:00
|
|
|
ACL: {}
|
2016-03-13 23:34:44 -04:00
|
|
|
}
|
2016-08-30 07:19:21 -04:00
|
|
|
|
|
|
|
|
return handler.create(object).then(() => {
|
|
|
|
|
pushStatus = {
|
|
|
|
|
objectId
|
|
|
|
|
};
|
|
|
|
|
return Promise.resolve(pushStatus);
|
2016-04-14 19:24:56 -04:00
|
|
|
});
|
2016-03-13 23:34:44 -04:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const setRunning = function(installations) {
|
2016-03-29 22:42:37 -04:00
|
|
|
logger.verbose('sending push to %d installations', installations.length);
|
2016-11-24 15:47:41 -05:00
|
|
|
return handler.update({status:"pending", objectId: objectId},
|
2016-07-23 21:20:04 +02:00
|
|
|
{status: "running", updatedAt: new Date() });
|
2016-03-13 23:34:44 -04:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const complete = function(results) {
|
|
|
|
|
const update = {
|
2016-03-13 23:34:44 -04:00
|
|
|
status: 'succeeded',
|
2016-04-21 21:36:15 -04:00
|
|
|
updatedAt: new Date(),
|
2016-03-13 23:34:44 -04:00
|
|
|
numSent: 0,
|
|
|
|
|
numFailed: 0,
|
|
|
|
|
};
|
|
|
|
|
if (Array.isArray(results)) {
|
2016-03-26 11:02:26 -04:00
|
|
|
results = flatten(results);
|
2016-11-24 15:47:41 -05:00
|
|
|
results.reduce((memo, result) => {
|
2016-03-13 23:34:44 -04:00
|
|
|
// Cannot handle that
|
2016-04-18 22:30:19 -04:00
|
|
|
if (!result || !result.device || !result.device.deviceType) {
|
2016-03-13 23:34:44 -04:00
|
|
|
return memo;
|
|
|
|
|
}
|
2016-12-07 15:17:05 -08:00
|
|
|
const deviceType = result.device.deviceType;
|
2016-03-13 23:34:44 -04:00
|
|
|
if (result.transmitted)
|
|
|
|
|
{
|
|
|
|
|
memo.numSent++;
|
|
|
|
|
memo.sentPerType = memo.sentPerType || {};
|
|
|
|
|
memo.sentPerType[deviceType] = memo.sentPerType[deviceType] || 0;
|
|
|
|
|
memo.sentPerType[deviceType]++;
|
|
|
|
|
} else {
|
|
|
|
|
memo.numFailed++;
|
|
|
|
|
memo.failedPerType = memo.failedPerType || {};
|
|
|
|
|
memo.failedPerType[deviceType] = memo.failedPerType[deviceType] || 0;
|
|
|
|
|
memo.failedPerType[deviceType]++;
|
|
|
|
|
}
|
|
|
|
|
return memo;
|
|
|
|
|
}, update);
|
|
|
|
|
}
|
2016-03-29 22:42:37 -04:00
|
|
|
logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed);
|
2016-08-30 07:19:21 -04:00
|
|
|
return handler.update({status:"running", objectId }, update);
|
2016-03-13 23:34:44 -04:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const fail = function(err) {
|
|
|
|
|
const update = {
|
2016-03-29 22:42:37 -04:00
|
|
|
errorMessage: JSON.stringify(err),
|
2016-04-21 21:36:15 -04:00
|
|
|
status: 'failed',
|
|
|
|
|
updatedAt: new Date()
|
2016-03-29 22:42:37 -04:00
|
|
|
}
|
2016-05-19 18:52:44 +02:00
|
|
|
logger.info('warning: error while sending push', err);
|
2016-08-30 07:19:21 -04:00
|
|
|
return handler.update({ objectId }, update);
|
2016-03-29 22:42:37 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-13 23:34:44 -04:00
|
|
|
return Object.freeze({
|
2016-04-07 18:08:09 -04:00
|
|
|
objectId,
|
2016-03-13 23:34:44 -04:00
|
|
|
setInitial,
|
|
|
|
|
setRunning,
|
2016-03-29 22:42:37 -04:00
|
|
|
complete,
|
|
|
|
|
fail
|
2016-03-13 23:34:44 -04:00
|
|
|
})
|
|
|
|
|
}
|