UNPKG

fh-wfm-result

Version:

A result module for WFM, for working with the results of pushing a workorder through a workflow

59 lines (46 loc) 2.46 kB
'use strict'; var config = require('../config'); var shortid = require('shortid'); var ResultTopics = require('fh-wfm-mediator/lib/topics'); module.exports = function(mediator) { //Used for cloud data storage topics var resultCloudDataTopics = new ResultTopics(mediator); resultCloudDataTopics.prefix(config.cloudDataTopicPrefix).entity(config.datasetId); //Used for cloud topics var resultCloudTopics = new ResultTopics(mediator); resultCloudTopics.prefix(config.cloudTopicPrefix).entity(config.datasetId); /** * Subscribers to sync topics which publishes to a data storage topic, subscribed to by a storage module, * for CRUDL operations. Publishes the response received from the storage module back to sync */ resultCloudTopics.on('create', function(resultToCreate, mediatorTopicIdentifier) { // Adds an id field required by the new simple store module to the result object that will be created resultToCreate.id = shortid.generate(); resultCloudDataTopics.request('create', resultToCreate, {uid: resultToCreate.id}) .then(function(createdresult) { //This is using a timestamp as the mediatorTopicIdentifier generated by the sync module mediator.publish(resultCloudTopics.getTopic('create', 'done') + ':' + mediatorTopicIdentifier, createdresult); }); }); resultCloudTopics.on('list', function(listOptions) { var self = this; listOptions = listOptions || {}; listOptions.filter = listOptions.filter || {}; listOptions.filter.topicUid = listOptions.topicUid || shortid.generate(); resultCloudDataTopics.request('list', listOptions.filter, {uid: listOptions.filter.topicUid}).then(function(list) { self.mediator.publish(resultCloudTopics.getTopic('list', 'done', listOptions.topicUid), list); }).catch(function(err) { self.mediator.publish(resultCloudTopics.getTopic('list', 'error', listOptions.topicUid), err); }); }); resultCloudTopics.on('update', function(resultToUpdate) { //The server ID is not guaranteed to have been assigned yet, so the UID could be the _localuid return resultCloudDataTopics.request('update', resultToUpdate, {uid: resultToUpdate.id || resultToUpdate._localuid}); }); resultCloudTopics.on('read', function(uid) { return resultCloudDataTopics.request('read', uid); }); resultCloudTopics.on('delete', function(uid) { return resultCloudDataTopics.request('delete', uid); }); };