-
Notifications
You must be signed in to change notification settings - Fork 3
/
HardwareManager.ts
260 lines (239 loc) · 7.02 KB
/
HardwareManager.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import fetch from 'node-fetch';
import { Manager } from '../Manager';
import { getApiURL } from '../Util';
import {
Response,
IHardware,
HardwareOptions,
CheckoutOptions,
HardwarePostOptions,
HardwareUpdateOptions
} from '../Interfaces';
import { Hardware } from './Hardware';
export class HardwareManager extends Manager {
// GET
/**
* Return list of assets.
* @param options Options - Options to pass to the API.
*/
async get(options?: HardwareOptions): Promise<Hardware[]> {
const parsedOptions = { limit: 50, ...(options || {}) };
const res = await fetch(getApiURL(this.snipeURL, '/hardware', parsedOptions), {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const result = await res.json();
const json: Response<IHardware> = result;
return json.rows.map((hardware) => new Hardware(hardware));
}
/**
* Get specific asset by Asset ID.
* @param id Asset ID.
*/
async getID(id: number): Promise<Hardware> {
const res = await fetch(getApiURL(this.snipeURL, `/hardware/${id}`), {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const result = await res.json();
const json: IHardware = result;
return new Hardware(json);
}
/**
* Get specific asset by Asset Tag.
* @param asset_tag Asset tag.
*/
async getAssetTag(asset_tag: string): Promise<Hardware> {
const res = await fetch(getApiURL(this.snipeURL, `/hardware/bytag/${asset_tag}`), {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const result = await res.json();
const json: IHardware = result;
return new Hardware(json);
}
/**
* Get specific asset by Serial.
* @param serial Asset tag.
*/
async getSerial(serial: string): Promise<Hardware[]> {
const res = await fetch(getApiURL(this.snipeURL, `/hardware/byserial/${serial}`), {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const result = await res.json();
const json: Response<IHardware> = result;
return json.rows.map((hardware) => new Hardware(hardware));
}
/**
* Checkin a asset.
* @param id Asset ID.
* @param note Note.
* @param location_id Location ID.
*/
async checkin(id: number, note?: string, location_id?: number): Promise<Hardware> {
const data = {
note,
location_id
};
const res = await fetch(getApiURL(this.snipeURL, `/hardware/${id}/checkin`), {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
const result = await res.json();
return result;
}
/**
* Checkout a asset.
* @param id Asset ID to checkin.
* @param options Options to pass to the API.
*/
async checkout(id: number, options: CheckoutOptions): Promise<Hardware> {
const res = await fetch(getApiURL(this.snipeURL, `/hardware/${id}/checkout`), {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(options)
});
const result = await res.json();
return result;
}
/**
* Return a list of assets that are due for audit soon.
*/
async getAuditDue(): Promise<Hardware[]> {
const res = await fetch(getApiURL(this.snipeURL, '/hardware/audit/due'), {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const result = await res.json();
const json: Response<IHardware> = result;
return json.rows.map((hardware) => new Hardware(hardware));
}
/**
* Return a list of assets that are overdue for audit.
*/
async getOverdueAudit(): Promise<Hardware[]> {
const res = await fetch(getApiURL(this.snipeURL, '/hardware/audit/overdue'), {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const result = await res.json();
const json: Response<IHardware> = result;
return json.rows.map((hardware) => new Hardware(hardware));
}
// DELETE
/**
* Delete specific asset by ID.
* @param id Asset ID.
*/
async delete(id: number): Promise<void> {
await fetch(getApiURL(this.snipeURL, `/hardware/${id}`), {
method: 'DELETE',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
}
// POST, PATCH, PUT
/**
* Make new asset.
* @param options
*/
async new(options: HardwarePostOptions): Promise<Hardware> {
// if(!options.asset_tag || !options.model_id || !options.status_id) return "missing fields!";
const res = await fetch(getApiURL(this.snipeURL, '/hardware'), {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(options)
});
const result = await res.json();
if (result.status === 'success') {
const json: IHardware = result.payload;
return new Hardware(json);
}
return result;
}
/**
* Update an asset.
* @param id
* @param options Options to pass to the API.
*/
async update(id: number, options: HardwareUpdateOptions): Promise<Hardware> {
const res = await fetch(getApiURL(this.snipeURL, `/hardware/${id}`), {
method: 'PATCH',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(options)
});
const result = await res.json();
if (result.status === 'success') {
const json: IHardware = result.payload;
return new Hardware(json);
}
return result;
}
/**
* Mark an asset as audited.
* @param asset_tag Asset tag of asset to audit.
* @param location_id ID of location.
*/
async audit(asset_tag: string, location_id?: number): Promise<Hardware> {
const data = {
asset_tag,
location_id
};
const res = await fetch(getApiURL(this.snipeURL, '/hardware/audit'), {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiToken}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
const result = await res.json();
return result;
}
}