Skip to content

Instantly share code, notes, and snippets.

@JorgeJuarezM
Created October 22, 2024 17:52
Show Gist options
  • Select an option

  • Save JorgeJuarezM/ce3237333539f963c7383e2efed0f86e to your computer and use it in GitHub Desktop.

Select an option

Save JorgeJuarezM/ce3237333539f963c7383e2efed0f86e to your computer and use it in GitHub Desktop.
const API_URL = "https://api.evacenter.com/graphql/";
function getCookieValue(name) {
let matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
async function fetchAPI(url, headers, body) {
try {
const response = await fetch(url, {
headers,
body,
method: "POST",
});
return await response.json();
} catch (error) {
console.error("Error fetching API:", error);
}
}
async function getScreenDetailsAndSetType() {
const screensDetails = (await getScreenDetails()).screens.map((cs, index) => {
const width = cs.availWidth;
const height = cs.availHeight;
const top = cs.availTop;
const left = cs.availLeft;
const typeInput = prompt(`Enter type for monitor ${index + 1} (REPORT/VIEWER):`);
return {
type: typeInput.toUpperCase() === 'REPORT' ? 'REPORT' : 'VIEWER', // Default to VIEWER if not REPORT
args: `left=${left},top=${top},width=${width},height=${height},noopener,noreferrer`,
};
});
return { windows: screensDetails };
}
async function main() {
const cookieAccessValue = getCookieValue('access');
const JWT_TOKEN = `JWT ${cookieAccessValue}`;
console.log(JWT_TOKEN);
const userPreferenceKey = JSON.parse(localStorage.getItem("screenPreferencesMachineId"));
const userPreferenceValue = JSON.stringify(await getScreenDetailsAndSetType()).replace(/"/g, '\\"');
console.log(userPreferenceKey);
console.log(userPreferenceValue);
const meQueryResponseJSON = await fetchAPI(API_URL, {
"authorization": JWT_TOKEN,
"content-type": "application/json",
}, "{\"operationName\":\"me\",\"variables\":{},\"query\":\"query me {\\n me {\\n ...User\\n __typename\\n }\\n}\\n\\nfragment User on UserNodeCustom {\\n id\\n email\\n isMo\\n data {\\n practitioner {\\n id\\n firstSurname\\n lastSurname\\n fullName\\n phone\\n phoneCode\\n phoneCountry {\\n id\\n isoCode\\n __typename\\n }\\n facility {\\n id\\n name\\n country {\\n isoCode\\n phoneCode\\n name\\n id\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n organization {\\n id\\n name\\n hasPendingPayment\\n activeFlags {\\n id\\n identifier\\n __typename\\n }\\n __typename\\n }\\n productGroups {\\n id\\n isVisible\\n product {\\n id\\n name\\n slug\\n description\\n __typename\\n }\\n group {\\n id\\n name\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n visitant {\\n id\\n __typename\\n }\\n __typename\\n}\\n\"}");
const organizationId = getCookieValue("oid");
console.log(organizationId);
const userId = atob(meQueryResponseJSON.data.me.id).split(':')[1];
console.log(userId);
const userPreferenceQueryResponseJSON = await fetchAPI(API_URL, {
"authorization": JWT_TOKEN,
"content-type": "application/json",
"organization-id": organizationId,
}, `{\"operationName\":\"userPreferences\",\"variables\":{\"userId\":\"${userId}\",\"family\":\"SCREENS\"},\"query\":\"query userPreferences($userId: UUID!, $family: String, $limit: Int, $offset: Int, $ordering: String, $before: String, $after: String, $first: Int, $last: Int) {\\n userPreferences(\\n userId: $userId\\n family: $family\\n limit: $limit\\n offset: $offset\\n ordering: $ordering\\n before: $before\\n after: $after\\n first: $first\\n last: $last\\n ) {\\n pageInfo {\\n hasNextPage\\n hasPreviousPage\\n __typename\\n }\\n results {\\n id\\n deleted\\n createdAt\\n updatedAt\\n family\\n key\\n value\\n __typename\\n }\\n totalCount\\n __typename\\n }\\n}\\n\"}`);
console.log(userPreferenceQueryResponseJSON);
const matchingPreference = userPreferenceQueryResponseJSON.data.userPreferences.results.find(userPreference => userPreference.key === userPreferenceKey);
if (!matchingPreference) {
console.log("No user preference found!");
console.log("Create user preference");
const createUserPreferenceMutationResponseJSON = await fetchAPI(API_URL, {
"authorization": JWT_TOKEN,
"content-type": "application/json",
"organization-id": organizationId,
}, `{\"operationName\":\"createUserPreference\",\"variables\":{\"input\":{\"user\":\"${userId}\",\"family\":\"SCREENS\",\"key\":\"${userPreferenceKey}\",\"value\":\"${userPreferenceValue}\"}},\"query\":\"mutation createUserPreference($input: UserPreferenceInputType!) {\\n createUserPreference(input: $input) {\\n success\\n errors\\n userPreference {\\n user {\\n id\\n __typename\\n }\\n family\\n key\\n value\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}`);
console.log(createUserPreferenceMutationResponseJSON);
} else {
console.log("User preference found!");
console.log("Update user preference");
const updateUserPreferenceMutationResponseJSON = await fetchAPI(API_URL, {
"authorization": JWT_TOKEN,
"content-type": "application/json",
"organization-id": organizationId,
}, `{\"operationName\":\"updateUserPreference\",\"variables\":{\"id\":\"${matchingPreference.id}\",\"input\":{\"user\":\"${userId}\",\"family\":\"SCREENS\",\"key\":\"${userPreferenceKey}\",\"value\":\"${userPreferenceValue}\"}},\"query\":\"mutation updateUserPreference($id: UUID!, $input: UserPreferenceInputType!) {\\n updateUserPreference(id: $id, input: $input) {\\n success\\n errors\\n userPreference {\\n user {\\n id\\n __typename\\n }\\n family\\n key\\n value\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}`);
console.log(updateUserPreferenceMutationResponseJSON);
}
}
console.log("Running script...");
await main();
console.log("Script finished!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment