diff options
author | Lizzy Hunt <logan.hunt@usu.edu> | 2023-02-15 18:03:46 -0700 |
---|---|---|
committer | Lizzy Hunt <logan.hunt@usu.edu> | 2023-02-15 18:05:55 -0700 |
commit | 32803c441678cd640e46153688d26c4c0746d7b3 (patch) | |
tree | f2f186df72073be9ca712d98dff7d180eaa34371 /src/aggietime.js | |
parent | 30cbc219e68ef5fc7da56e322e1aeca102bdb479 (diff) | |
download | aggietimed-32803c441678cd640e46153688d26c4c0746d7b3.tar.gz aggietimed-32803c441678cd640e46153688d26c4c0746d7b3.zip |
We do a little logging, but cringe OpenAPI errors be making me want to shoot myself. We have some shit working though.
Diffstat (limited to 'src/aggietime.js')
-rw-r--r-- | src/aggietime.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/aggietime.js b/src/aggietime.js new file mode 100644 index 0000000..793481c --- /dev/null +++ b/src/aggietime.js @@ -0,0 +1,62 @@ +import { + AGGIETIME_URI, + AGGIETIME_DOMAIN, + USER_PATH, + USER_CACHE_EXP_SEC, + CLOCKIN_PATH, +} from "./constants.js"; + +import { client } from "./axios_client.js"; + +import expireCache from "expire-cache"; + +const replace_path_args = (path, map) => + path.replaceAll(/:([a-zA-Z0-9_]+)/g, (_, key) => map[key]); + +const get_user_position_or_specified = async (position) => { + const { positions } = await get_user_info(); + + if (!position && positions.length != 1) { + throw "Must specify a position when there's not only one to choose from"; + } else if (!position) { + position = positions[0]; + } + + return position; +}; + +export const get_user_info = async () => { + if (!expireCache.get("user")) { + const user = await client + .get(`${AGGIETIME_URI}/${USER_PATH}`) + .then(({ data, config }) => { + const csrf_token = config.jar + .toJSON() + .cookies.find( + ({ domain, key }) => + domain === AGGIETIME_DOMAIN && key === "XSRF-TOKEN" + ).value; + expireCache.set("aggietime-csrf", csrf_token); + return data; + }); + + expireCache.set("user", user, USER_CACHE_EXP_SEC); + } + return expireCache.get("user"); +}; + +export const clock_in = async ({ position } = {}) => { + position = await get_user_position_or_specified(position); + + return await client.post( + `${AGGIETIME_URI}/${replace_path_args(CLOCKIN_PATH, { position })}`, + { + comment: "", + }, + { + headers: { + "X-XSRF-TOKEN": expireCache.get("aggietime-csrf"), + }, + } + ); +}; |