UNPKG

2.1 kBJavaScriptView Raw
1
2const d = require("debug")("raptorjs:inventory")
3const Base = require("./Base")
4const Device = require("./model/Device")
5const Pager = require("./pager")
6
7class Inventory extends Base {
8
9 constructor(container) {
10 super(container)
11 }
12
13 Permission() {
14 return this.getContainer().Admin().getPermission("device")
15 }
16
17 list(query={}, paging={}) {
18 d("List devices")
19 const url = this.route("INVENTORY_LIST") + Pager.buildQuery(paging, query)
20 return this.getClient().get(url)
21 .then((list) => Promise.resolve(Pager.create(list, (raw) => new Device(raw))))
22 }
23
24 search(q, paging) {
25 d("Search for devices")
26 const url = this.route("INVENTORY_SEARCH") + Pager.buildQuery(paging)
27 return this.getClient().post(url, q)
28 .then((list) => Promise.resolve(Pager.create(list, (raw) => new Device(raw))))
29 }
30
31 read(dev) {
32 const id = (typeof dev === "string") ? dev : dev.id
33 d("Load device %s", id)
34 return this.getClient().get(this.route("INVENTORY_LOAD", id)).then((d) => new Device(d))
35 }
36
37 delete(dev) {
38 const id = (typeof dev === "string") ? dev : dev.id
39 d("Delete device %s", id)
40 return this.getClient().delete(this.route("INVENTORY_DELETE", id))
41 }
42
43 update(dev) {
44 dev = dev.toJSON ? dev.toJSON() : dev
45 d("Update device %s", dev.id)
46 return this.getClient().put(this.route("INVENTORY_UPDATE", dev.id), dev)
47 .then((d) => new Device(d))
48 }
49
50 create(dev) {
51 d("Create device")
52 dev = dev.toJSON ? dev.toJSON() : dev
53 return this.getClient().post(this.route("INVENTORY_CREATE"), dev)
54 .then((json) => new Device(json))
55 }
56
57 subscribe(device, fn) {
58 return this.getClient().subscribe("inventory/" + device.id, fn)
59 .then(() => Promise.resolve(device))
60 }
61
62 unsubscribe(device, fn) {
63 return this.getClient().unsubscribe("inventory/" + device.id, fn)
64 .then(() => Promise.resolve(device))
65 }
66
67}
68
69module.exports = Inventory