Add past week action

This commit is contained in:
Lizzy Hunt 2023-03-24 16:34:55 -06:00
parent 3bda8ced7d
commit 633965afaa
No known key found for this signature in database
GPG Key ID: 8AC6A4B840C0EC49
3 changed files with 33 additions and 0 deletions

View File

@ -6,6 +6,7 @@ const ACTIONS = {
"current-shift": aggietime.current_shift,
"current-user": aggietime.get_user_info,
"status-line": aggietime.get_status_line,
"past-week": aggietime.last_week,
};
export const do_action = async (body) => {

View File

@ -5,8 +5,10 @@ import {
USER_CACHE_EXP_SEC,
CLOCKIN_PATH,
CLOCKOUT_PATH,
SUMMARY_PATH,
OPEN_SHIFT_PATH,
OPEN_SHIFT_EXP_SEC,
PAST_WEEK_EXP_SEC,
} from "./constants.js";
import { with_exponential_retry } from "./exponential_retry.js";
@ -119,3 +121,30 @@ export const get_status_line = async () => {
}
return { status: expireCache.get("status_line") };
};
export const last_week = async ({ position_id }) => {
position_id = await get_user_position_or_specified(position_id);
const [start, end] = [
((d) => {
const day = d.getDay();
const diff = d.getDate() - day + (day == 0 ? -6 : 1);
return new Date(d.setDate(diff));
})(new Date()),
new Date(),
].map((d) => d.toISOString().split("T")[0]);
if (!expireCache.get("past_week")) {
const { anumber } = await get_user_info();
const hours = await aggietime
.get(replace_path_args(SUMMARY_PATH, { position_id, start, end }))
.then(({ data: { undisputed_hours } }) => undisputed_hours);
expireCache.set(
"past_week",
`${anumber} - ${hours} hours`,
PAST_WEEK_EXP_SEC
);
}
return { status: expireCache.get("past_week") };
};

View File

@ -11,8 +11,11 @@ export const LOGIN_PATH = "api/v1/auth/login";
export const CLOCKIN_PATH = "api/v1/positions/:position_id/clock_in";
export const CLOCKOUT_PATH = "api/v1/positions/:position_id/clock_out";
export const USER_PATH = "api/v1/auth/get_user_info";
export const SUMMARY_PATH =
"api/v1/positions/:position_id/summary?start=:start&end=:end";
export const OPEN_SHIFT_PATH = "api/v1/users/:anumber/open_shift";
export const OPEN_SHIFT_EXP_SEC = 60;
export const PAST_WEEK_EXP_SEC = 180;
export const USER_CACHE_EXP_SEC = 30;