import isUUID from 'validator/lib/isUUID';
import { API } from '../../LocalConfiguration';
import { handleErrors, errors} from "./RestModuleAPI";
const zonePath = API + '/areas/';
/**
* Zone Rest API
* @module ZoneRestModule
*/
/**
*
* @param areaId
* @returns {Promise<{ok} | never>}
*/
export function fetchZones(areaId) {
return fetch(zonePath + areaId + "/zones")
.then(response => handleErrors(response))
.then(response => response.json());
}
/**
*
* @param areaId
* @param zoneId
* @returns {Promise<{ok} | never>}
*/
export function fetchZone(areaId, zoneId) {
if (!isUUID(areaId)) {
throw new Error(errors.invalidUUI);
}
return fetch(zonePath + areaId + "/zones/" + zoneId)
.then(response => handleErrors(response))
.then(response => response.json());
}
/**
*
* @param areaId
* @param data
* @returns {Promise<Response>}
*/
export function createZone(areaId, data) {
return fetch(zonePath + areaId + "/zones", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
}
/**
*
* @param areaId
* @param zone
* @returns {Promise<Response>}
*/
export function updateZone(areaId, zone) {
return fetch(zonePath + areaId + "/zones/" + zone.zoneNumber, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(zone)
});
// .then(response => response.json());
}
/**
*
* @param areaId
* @param zoneId
* @returns {Promise<Response>}
*/
export function deleteZone(areaId, zoneId) {
return fetch(zonePath + areaId + "/zones/" + zoneId, {
method: 'DELETE',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
}