This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/static/js/api.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

const Api = {
/**
* get lots id
* @returns get lots
*/
async get_lots() {
2022-04-20 10:04:53 +00:00
const request = await this.doRequest(API_URLS.lots, "GET", null);
if (request != undefined) {
request.items = request.items.filter(itm => !itm.trade) // Avoid show outgoing or incomming trades
return request.items;
}
throw request;
},
/**
* Get filtered devices info
* @param {number[]} ids devices ids
* @returns full detailed device list
*/
async get_devices(ids) {
2022-04-20 10:04:53 +00:00
const request = await this.doRequest(`${API_URLS.devices }?filter={"id": [${ ids.toString() }]}`, "GET", null);
if (request != undefined) return request.items;
throw request;
},
/**
* Get filtered devices info
* @param {number[]} ids devices ids
* @returns full detailed device list
*/
async search_device(id) {
2022-04-20 10:04:53 +00:00
const request = await this.doRequest(`${API_URLS.devices }?filter={"devicehub_id": ["${ id }"]}`, "GET", null)
if (request != undefined) return request.items
throw request
},
/**
* Add devices to lot
* @param {number} lotID lot id
* @param {number[]} listDevices list devices id
*/
async devices_add(lotID, listDevices) {
2022-04-20 10:04:53 +00:00
const queryURL = `${API_URLS.devices_modify.replace("UUID", lotID) }?${ listDevices.map(deviceID => `id=${ deviceID}`).join("&")}`;
2022-04-20 10:09:56 +00:00
return Api.doRequest(queryURL, "POST", null);
},
/**
* Remove devices from a lot
* @param {number} lotID lot id
* @param {number[]} listDevices list devices id
*/
async devices_remove(lotID, listDevices) {
2022-04-20 10:04:53 +00:00
const queryURL = `${API_URLS.devices_modify.replace("UUID", lotID) }?${ listDevices.map(deviceID => `id=${ deviceID}`).join("&")}`;
2022-04-20 10:09:56 +00:00
return Api.doRequest(queryURL, "DELETE", null);
},
/**
*
* @param {string} url URL to be requested
* @param {String} type Action type
* @param {String | Object} body body content
* @returns
*/
async doRequest(url, type, body) {
2022-04-20 10:04:53 +00:00
let result;
try {
result = await $.ajax({
2022-04-20 10:04:53 +00:00
url,
type,
headers: { "Authorization": API_URLS.Auth_Token },
2022-04-20 10:04:53 +00:00
body
});
return result;
} catch (error) {
console.error(error);
throw error;
}
}
}