2024-02-23 21:57:18 +01:00

264 lines
7.0 KiB
JavaScript

async function fetch_history() {
try {
const response = await fetch(`${currentUrl}app/datahistory`);
const rawData = await response.json();
var dateFormat = d3.timeParse("%a, %d %b %Y %H:%M:%S GMT");
// SELECT uuid, quantity, discount_percentage, price, currency, h_timestamp
let historyData = rawData.map(d => ({
uuid: d[0],
value: parseFloat(d[3].replace('$', '')),
currency: d[4],
date: dateFormat(d[5]),
}));
return historyData;
} catch (error) {
console.error('Error fetching data history: ', error);
throw error;
}
}
async function fetch_item() {
try {
// SELECT uuid, itemid, skuid, choice, attributes, image, show
const response = await fetch(`${currentUrl}app/dataitem`);
const rawData = await response.json();
const items = rawData.reduce((item, row) => {
const uuid = row[0];
const itemid = row[1];
const skuid = row[2];
const choice = row[3];
const attributes = row[4];
const image = row[5];
const show = row[6];
const values = {
itemid: itemid,
skuid: skuid,
choice: choice,
attributes: attributes,
image: image,
show: show,
};
item[uuid] = values;
return item;
}, {});
return items;
} catch (error) {
console.error('Error fetching data item: ', error);
throw error;
}
}
async function fetch_history_filtered(id_array) {
try {
// SELECT uuid, itemid, skuid, choice, attributes, image, show
const apiUrl = `${currentUrl}app/datahistory-filtered`;
filters_string = id_array.join('#');
const postData = {
filters: filters_string
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
const rawData = await response.json();
var dateFormat = d3.timeParse("%a, %d %b %Y %H:%M:%S GMT");
// SELECT uuid, quantity, discount_percentage, price, currency, h_timestamp
let historyData = rawData.map(d => ({
uuid: d[0],
value: parseFloat(d[3].replace('$', '')),
currency: d[4],
date: dateFormat(d[5]),
}));
return historyData;
} else {
throw new Error('Error in server response');
}
} catch (error) {
console.error('Error fetching data item: ', error);
throw error;
}
}
async function fetch_item_filtered(id_array) {
try {
// SELECT uuid, itemid, skuid, choice, attributes, image, show
const apiUrl = `${currentUrl}app/dataitem-filtered`;
filters_string = id_array.join('#');
const postData = {
filters: filters_string
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
const rawData = await response.json();
const items = rawData.reduce((item, row) => {
const uuid = row[0];
const itemid = row[1];
const skuid = row[2];
const choice = row[3];
const attributes = row[4];
const image = row[5];
const show = row[6];
const values = {
itemid: itemid,
skuid: skuid,
choice: choice,
attributes: attributes,
image: image,
show: show,
};
item[uuid] = values;
return item;
}, {});
return items;
} else {
throw new Error('Error in server response');
}
} catch (error) {
console.error('Error fetching data item: ', error);
throw error;
}
}
async function fetch_list() {
try {
const response = await fetch(`${currentUrl}app/datalist`);
const rawData = await response.json();
// SELECT uuid, name, description
let listData = rawData.map(d => ({
uuid: d[0],
name: d[1],
description: d[2],
}));
return listData;
} catch (error) {
console.error('Error fetching data history: ', error);
throw error;
}
}
async function get_data() {
items_data = await fetch_item();
history_data = await fetch_history();
return Array(items_data, history_data);
}
async function get_data_filtered(id_array) {
items_data = await fetch_item_filtered(id_array);
history_data = await fetch_history_filtered(id_array);
return Array(items_data, history_data);
}
async function get_list_details(list_uuid) {
try {
// SELECT name, description
const apiUrl = `${currentUrl}app/datalist`;
const postData = {
uuid: list_uuid
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
const rawData = await response.json();
const listData = {
name: rawData[0],
description: rawData[1],
};
return listData;
} else {
throw new Error('Error in server response');
}
} catch (error) {
console.error('Error fetching data item: ', error);
throw error;
}
}
async function fetch_all() {
try {
const response = await fetch(`${currentUrl}app/data-all`);
const rawData = await response.json();
// tables = ['item', 'itemlist', 'listcontent', 'history']
let data = Object();
// uuid, itemid, skuid, choice, attributes, image, show
data.item = rawData[0].map(d => ({
uuid: d[0],
itemid: d[1],
skuid: d[2],
choice: d[3],
attributes: d[4],
image: d[5],
show: d[6],
}));
// uuid, name, description
data.itemlist = rawData[1].map(d => ({
uuid: d[0],
name: d[1],
description: d[2],
}));
// list_uuid, item_uuid
data.listcontent = rawData[2].map(d => ({
list_uuid: d[0],
item_uuid: d[1],
}));
// uuid, quantity, discount_percentage, price, currency, h_timestamp
data.history = rawData[3].map(d => ({
uuid: d[0],
quantity: d[1],
discount_percentage: d[2],
price: d[3],
currency: d[4],
h_timestamp: d[5],
}));
return data;
} catch (error) {
console.error('Error fetching data history: ', error);
throw error;
}
}