Created
March 28, 2017 02:48
-
-
Save iamblue/4c8e45f33b48d8880cb06c00264ccabe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Validator = require('jsonschema').Validator; | |
| var v = new Validator(); | |
| var schema = require('./schema'); | |
| var Rx = require('rxjs'); | |
| var R = require('ramda'); | |
| module.exports = function(datachannels, prototypes) { | |
| var datachannels$ = {}; | |
| function findthis (value) { | |
| console.log('3') | |
| return Rx.Observable.create(function(observer) { | |
| console.log('2') | |
| return datachannels.find(value, function(err, data) { | |
| if (err) throw (err); | |
| console.log(1) | |
| observer.next(data); | |
| observer.complete(); | |
| }) | |
| }); | |
| } | |
| Rx.Observable.findthis = findthis; | |
| // console.log(Rx.Observable.prototype) | |
| Rx.Observable | |
| .findthis({ isActive: true, datachannelId: 'pwm_display' }) | |
| .subscribe({ | |
| next: function(data) { | |
| // console.log(data); | |
| } | |
| }) | |
| // var datachannels$.insert = Rx.Observable.fromNodeCallback(datachannels.insert); | |
| // var datachannels$.update = Rx.Observable.fromNodeCallback(datachannels.update); | |
| return { | |
| test: function() { | |
| }, | |
| validateSchema: function(object) { | |
| return v.validate(object, schema); | |
| }, | |
| retrievDatachannel: function(field) { | |
| field.isActive = true; | |
| return new Promise(function(resolve, reject) { | |
| return datachannels.find(field, function(err, data) { | |
| if (err) return reject(); | |
| return resolve(data); | |
| }); | |
| }); | |
| }, | |
| addNewDatachannel: function(field) { | |
| field.updatedAt = new Date().getTime(); | |
| field.createdAt = new Date().getTime(); | |
| field.isActive = true; | |
| return new Promise(function(resolve, reject) { | |
| return prototypes.find({ | |
| prototypeId: field.prototypeId, | |
| }, function(err, data) { | |
| if (err) return reject(); | |
| if (data.length === 1) { | |
| return resolve(data[0]); | |
| } else { | |
| return reject({ error: 'Can not find this prototypeId.' }); | |
| } | |
| }) | |
| }) | |
| .then(function() { | |
| return new Promise(function(resolve, reject) { | |
| var validataSchema = v.validate(field, schema); | |
| if (validataSchema.errors.length === 0) { | |
| return resolve(); | |
| } else { | |
| return reject({ schema: validataSchema.errors }) | |
| } | |
| }); | |
| }) | |
| .then(function() { | |
| return new Promise(function(resolve, reject) { | |
| return datachannels.insert(field, function(err, data) { | |
| if (err) return reject(); | |
| return resolve(data); | |
| }); | |
| }); | |
| }); | |
| }, | |
| editDatachannel: function(query, update) { | |
| return new Promise(function(resolve, reject) { | |
| return datachannels.update(query, { $set: update }, {}, function(err, num) { | |
| if (err) return reject(); | |
| resolve({ message: 'success.' }); | |
| }); | |
| }); | |
| }, | |
| deleteDatachannel: function(query) { | |
| return new Promise(function(resolve, reject) { | |
| return datachannels.update(query, { $set: { updatedAt: new Date().getTime(), isActive: false } }, {}, function(err, num) { | |
| if (err) return reject(); | |
| resolve({ message: 'success.' }); | |
| }); | |
| }); | |
| }, | |
| cloneDatachannel: function(fromPrototypeId, toPrototypeId, userId) { | |
| var _this = this; | |
| return new Promise(function(resolve, reject) { | |
| return datachannels.find({ | |
| prototypeId: fromPrototypeId, | |
| isActive: true, | |
| }, function(err, data) { | |
| if (err) return reject(); | |
| resolve(data); | |
| }); | |
| }) | |
| .then(function(data) { | |
| var datachannelPromise = [] | |
| data.forEach(function(k, v) { | |
| k.prototypeId = toPrototypeId; | |
| k.createUserId = userId; | |
| delete k._id; | |
| datachannelPromise.push(_this.addNewDatachannel(k)); | |
| }); | |
| return Promise.all(datachannelPromise) | |
| }); | |
| }, | |
| importDataChannel: function(datachannel, userId, prototypeId, isMCSLite) { | |
| var _this = this; | |
| var channelPool = []; | |
| datachannel.forEach(function(key, index) { | |
| key.prototypeId = prototypeId; | |
| key.createUserId = userId; | |
| channelPool.push(_this.addNewDatachannel(key)); | |
| }); | |
| return Promise.all(channelPool) | |
| }, | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment