From 6801f5c5ef549931297ec5f33c5a2046facd446c Mon Sep 17 00:00:00 2001 From: ifirmawan Date: Wed, 7 Feb 2024 22:24:57 +0700 Subject: [PATCH] [#1153] Add new lib: sync datapoints process --- app/src/lib/sync-datapoints.js | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/src/lib/sync-datapoints.js diff --git a/app/src/lib/sync-datapoints.js b/app/src/lib/sync-datapoints.js new file mode 100644 index 000000000..77f1e4437 --- /dev/null +++ b/app/src/lib/sync-datapoints.js @@ -0,0 +1,35 @@ +import { crudMonitoring } from '../database/crud'; +import api from './api'; + +export const fetchDatapoints = async (form, pageNumber = 1, allData = []) => { + try { + const response = await api.get(`/datapoint-list?page=${pageNumber}&form=${form}`); + const data = response.data.data; + + const updatedData = [...allData, ...data]; + + if (data.hasMorePages) { + return fetchDatapoints(form, pageNumber + 1, updatedData); + } else { + return updatedData; + } + } catch (error) { + return Promise.reject(error); + } +}; + +export const downloadDatapointsJson = async (formId, url) => { + try { + const response = await api.get(url); + if (response.status === 200) { + const jsonData = response.data; + const res = await crudMonitoring.syncForm({ + formId, + formJSON: jsonData, + }); + console.info('[SYNCED MONITORING]', res); + } + } catch (error) { + return Promise.reject(error); + } +};