-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpublishService.js
43 lines (41 loc) · 1.56 KB
/
unpublishService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";
var deviceRepository = require('./repositories/cloudDeviceRepository');
var cloudDeviceManager = require('./cloudDeviceManager');
var cloudProductDescriptorManager = require('./cloudProductDescriptorManager');
var async = require('async');
var _ = require('underscore');
module.exports.unpublish = function removeProductDescriptorAndDevices(productDescriptorId, callback) {
async.waterfall([
function findExistingDevices(iterCallback) {
deviceRepository.filterByProductDescriptorId(productDescriptorId, function (err, data) {
if (err) {
iterCallback(err);
} else if (data) {
iterCallback(null, data);
} else {
iterCallback(null, []);
}
});
},
function removeExistingDevices(devices, iterCallback) {
async.each(_.pluck(devices, 'serialNumber'), function removeCloudDevice(serialNumber, jterCallback) {
cloudDeviceManager.remove(productDescriptorId, serialNumber, jterCallback);
}, function (err) {
if (err) {
iterCallback(err);
} else {
iterCallback();
}
});
},
function removeProductDescriptor(iterCallback) {
cloudProductDescriptorManager.remove(productDescriptorId, iterCallback);
}
], function (err) {
if (err) {
callback(err);
} else {
callback(null, true);
}
});
};