2016-03-14 10:20:24 -04:00
|
|
|
|
import { md5Hash, newObjectId } from './cryptoUtils';
|
2016-03-13 23:34:44 -04:00
|
|
|
|
|
2016-03-26 11:02:26 -04:00
|
|
|
|
export function flatten(array) {
|
|
|
|
|
|
return array.reduce((memo, element) => {
|
|
|
|
|
|
if (Array.isArray(element)) {
|
|
|
|
|
|
memo = memo.concat(flatten(element));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
memo = memo.concat(element);
|
|
|
|
|
|
}
|
|
|
|
|
|
return memo;
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-13 23:34:44 -04:00
|
|
|
|
export default function pushStatusHandler(config) {
|
|
|
|
|
|
|
|
|
|
|
|
let initialPromise;
|
|
|
|
|
|
let pushStatus;
|
2016-03-14 10:20:24 -04:00
|
|
|
|
|
|
|
|
|
|
let collection = function() {
|
|
|
|
|
|
return config.database.adaptiveCollection('_PushStatus');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-13 23:34:44 -04:00
|
|
|
|
let setInitial = function(body, where, options = {source: 'rest'}) {
|
2016-03-14 10:20:24 -04:00
|
|
|
|
let now = new Date();
|
2016-03-13 23:34:44 -04:00
|
|
|
|
let object = {
|
2016-03-14 10:20:24 -04:00
|
|
|
|
objectId: newObjectId(),
|
|
|
|
|
|
pushTime: now.toISOString(),
|
|
|
|
|
|
_created_at: now,
|
2016-03-13 23:34:44 -04:00
|
|
|
|
query: JSON.stringify(where),
|
|
|
|
|
|
payload: body.data,
|
|
|
|
|
|
source: options.source,
|
|
|
|
|
|
title: options.title,
|
|
|
|
|
|
expiry: body.expiration_time,
|
|
|
|
|
|
status: "pending",
|
|
|
|
|
|
numSent: 0,
|
|
|
|
|
|
pushHash: md5Hash(JSON.stringify(body.data)),
|
2016-03-14 10:20:24 -04:00
|
|
|
|
// lockdown!
|
|
|
|
|
|
_wperm: [],
|
|
|
|
|
|
_rperm: []
|
2016-03-13 23:34:44 -04:00
|
|
|
|
}
|
2016-03-14 10:20:24 -04:00
|
|
|
|
initialPromise = collection().then((collection) => {
|
|
|
|
|
|
return collection.insertOne(object);
|
|
|
|
|
|
}).then((res) => {
|
|
|
|
|
|
pushStatus = {
|
|
|
|
|
|
objectId: object.objectId
|
|
|
|
|
|
};
|
2016-03-13 23:34:44 -04:00
|
|
|
|
return Promise.resolve(pushStatus);
|
2016-03-14 10:20:24 -04:00
|
|
|
|
})
|
2016-03-13 23:34:44 -04:00
|
|
|
|
return initialPromise;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let setRunning = function() {
|
|
|
|
|
|
return initialPromise.then(() => {
|
2016-03-14 10:20:24 -04:00
|
|
|
|
return collection();
|
|
|
|
|
|
}).then((collection) => {
|
|
|
|
|
|
return collection.updateOne({status:"pending", objectId: pushStatus.objectId}, {$set: {status: "running"}});
|
|
|
|
|
|
});
|
2016-03-13 23:34:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let complete = function(results) {
|
|
|
|
|
|
let update = {
|
|
|
|
|
|
status: 'succeeded',
|
|
|
|
|
|
numSent: 0,
|
|
|
|
|
|
numFailed: 0,
|
|
|
|
|
|
};
|
|
|
|
|
|
if (Array.isArray(results)) {
|
2016-03-26 11:02:26 -04:00
|
|
|
|
results = flatten(results);
|
2016-03-13 23:34:44 -04:00
|
|
|
|
results.reduce((memo, result) => {
|
|
|
|
|
|
// Cannot handle that
|
|
|
|
|
|
if (!result.device || !result.device.deviceType) {
|
|
|
|
|
|
return memo;
|
|
|
|
|
|
}
|
|
|
|
|
|
let deviceType = result.device.deviceType;
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return initialPromise.then(() => {
|
2016-03-14 10:20:24 -04:00
|
|
|
|
return collection();
|
|
|
|
|
|
}).then((collection) => {
|
|
|
|
|
|
return collection.updateOne({status:"running", objectId: pushStatus.objectId}, {$set: update});
|
|
|
|
|
|
});
|
2016-03-13 23:34:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Object.freeze({
|
|
|
|
|
|
setInitial,
|
|
|
|
|
|
setRunning,
|
|
|
|
|
|
complete
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|