Created
November 19, 2021 02:09
-
-
Save loloDawit/003299016d47efb7b38939ae227d9cc2 to your computer and use it in GitHub Desktop.
Dashboards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const axios = require("axios"); | |
const config = require("../common/config"); | |
const ErrorResponse = require("../common/error"); | |
const {account, getDashboardInfo, unDelete} = require("./queries"); | |
const MyAccount = require("../common/constant"); | |
let myAccount = MyAccount(); | |
/** | |
* fetchAllDashboards | |
* @returns all dashboard for a specific account | |
*/ | |
const fetchAllDashboards = async () => { | |
try { | |
const response = await axios(config(account(myAccount.accountName))); | |
if (!response) { | |
return; | |
} | |
return response.data.data.actor.entitySearch.results.entities; | |
} catch (error) { | |
new ErrorResponse(error.message, error.code); | |
} | |
}; | |
/** | |
* fetchDashboardGUID | |
* @returns array of GUIDs | |
*/ | |
const fetchDashboardGUID = async () => { | |
let GUID = []; | |
try { | |
let entities = await fetchAllDashboards(); | |
entities?.forEach((entitie) => { | |
GUID.push(entitie.guid); | |
}); | |
return GUID; | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
/** | |
* fetchDashboardDetails | |
* @returns an array of objects with dashboard details | |
*/ | |
const fetchDashboardDetails = async () => { | |
try { | |
let GUIDs = await fetchDashboardGUID(); | |
let dashboardWithDetails = []; | |
for (const GUID of GUIDs) { | |
const response = await axios(config(getDashboardInfo(GUID))); | |
if (!response) { | |
new ErrorResponse("No Response", 400); | |
} | |
let entities = response.data.data.actor.entity; | |
dashboardWithDetails.push(entities); | |
} | |
return dashboardWithDetails; | |
} catch (error) { | |
new ErrorResponse(error.message, error.code); | |
} | |
}; | |
const fetchSingleDashboardDetail = async (guid) => { | |
try { | |
let dashboardWithDetails = []; | |
const response = await axios(config(getDashboardInfo(guid))); | |
let entities = response.data.data.actor.entity; | |
if (entities.pages !== null) { | |
dashboardWithDetails.push(entities); | |
} else { | |
dashboardWithDetails.push({ | |
guid: entities.guid, | |
name: entities.name | |
}); | |
} | |
return dashboardWithDetails; | |
} catch (error) { | |
new ErrorResponse(error.message, error.code); | |
} | |
}; | |
/** | |
* unDeleteDashboard | |
* @param {*} guid dashboard id | |
* @returns object | |
*/ | |
const unDeleteDashboard = async (guid) => { | |
let success = true; | |
let message = ""; | |
if (!guid) { | |
return { | |
success: false, | |
message: "Provide GUID" | |
}; | |
} | |
try { | |
const response = await axios(config(unDelete(guid))); | |
let data = response.data; | |
let deleteReq = data.data.dashboardUndelete; | |
if (deleteReq === null) { | |
success = false; | |
message = data.errors[0].message; | |
} else if (deleteReq.errors) { | |
success = false; | |
message = deleteReq.errors[0].type; | |
} else { | |
message = "Success!!"; | |
} | |
console.log({ | |
success, | |
message | |
}); | |
return { | |
success, | |
message | |
}; | |
} catch (error) { | |
new ErrorResponse(error.message, error.code); | |
} | |
}; | |
module.exports = { | |
fetchAllDashboards, | |
fetchDashboardDetails, | |
fetchDashboardGUID, | |
unDeleteDashboard, | |
fetchSingleDashboardDetail | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment