-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinterCreate.js
More file actions
64 lines (61 loc) · 2.31 KB
/
printerCreate.js
File metadata and controls
64 lines (61 loc) · 2.31 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
exports.action = {
name: 'printerCreate',
description: 'Creates a Printer',
version: 1,
inputs: {
required: [ 'name', 'organizationId', 'ipAddress' ],
optional: [ 'location', 'manufacturer', 'model', 'serial' ]
},
outputExample: {
name: 'My Color Printer',
id: '123',
location: 'Room 201',
manufacturer: 'Ricoh',
model: 'ABC123',
ipAddress: '192.168.100.20',
serial: '12345ABCDE'
},
run: function (api, connection, next) {
var id = api.mongoose.Types.ObjectId(connection.params.organizationId);
var Printer = api.mongoose.model('Printer');
var ipArray = connection.params.ipAddress.split('.');
for(var i=0; i<ipArray.length; i++)
{
var pad = '000';
ipArray[i] = pad.substring(0, pad.length - ipArray[i].length) + ipArray[i];
}
var paddedIP = ipArray.join('.');
var newPrinter = new Printer({
name: connection.params.name,
location: connection.params.location,
manufacturer: connection.params.manufacturer,
model: connection.params.model,
ipAddress: paddedIP,
serial: connection.params.serial
}).save(function (err, printer) {
if(err)
{
connection.error = "A Printer with IP Address '" + connection.params.ipAddress + "' already exists.";
connection.response.details = err;
next(connection, true);
}
else if(printer)
{
connection.response.name = printer.name;
connection.response.location = printer.location;
connection.response.manufacturer = printer.manufacturer;
connection.response.model = printer.model;
connection.response.ipAddress = printer.ipAddress;
connection.response.serial = printer.serial;
connection.response.id = printer._id;
api.mongoose.model('Organization').findByIdAndUpdate(id, {
$push: {
printers: printer._id
}
}, function (err) {
next(connection, true);
});
}
});
}
};