Skip to content

Commit

Permalink
Add time entries request
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesschaletzky committed Dec 15, 2023
1 parent 29a2b55 commit c4c969b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
22 changes: 7 additions & 15 deletions app/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import type {
CalculationPosition,
CalenderEvent,
TimeEntry,
} from "troi-library";
import type { CalculationPosition, CalenderEvent } from "troi-library";

// how to invalidate:
// 1. add a refresh button to the ui -> we do not need this if we use stale-while-revalidate as a normal refresh has the same effect
// 2. shortish ttl for session-cookie
// 3. revalidate after request was send (stale-while-revalidate)
// 1. shortish ttl for session-cookie
// 2. revalidate after request was send (stale-while-revalidate)

// split login & troi data sessions in two and add a ttl to the session data cookie? or save it as a session cookie?

import { createCookie, createFileSessionStorage } from "@remix-run/node";

type ClientId = number;
type EmployeeId = number;
type TimeEntryId = number;
import type { TimeEntries } from "./troi/TimeEntry";

export type SessionData = {
username: string;
troiPassword: string;
troiClientId: ClientId;
troiEmployeeId: EmployeeId;
troiClientId: number;
troiEmployeeId: number;
troiCalculationPositions: CalculationPosition[];
troiTimeEntries: Map<TimeEntryId, TimeEntry>;
troiTimeEntries: TimeEntries;
// POST -> timeEntries.set(asd, new TimeEntry())
// DELETE -> timeEntries.remove()
// PUT -> timeEntries.set(asd, entry)
Expand Down
9 changes: 9 additions & 0 deletions app/troi/TimeEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type TimeEntry = {
id: number;
date: string;
hours: number;
description: string;
calculationPosition: number;
};

export type TimeEntries = { [key: number]: TimeEntry };
60 changes: 60 additions & 0 deletions app/troi/troiControllerServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import TroiApiService from "troi-library";
import type { SessionData } from "~/sessions";
import { commitSession, getSession } from "~/sessions";
import { addDaysToDate, formatDateToYYYYMMDD } from "~/utils/dateUtils";
import type { TimeEntries, TimeEntry } from "./TimeEntry";

const BASE_URL = "https://digitalservice.troi.software/api/v2/rest";
const CLIENT_NAME = "DigitalService GmbH des Bundes";
Expand Down Expand Up @@ -139,6 +140,65 @@ export async function getCalenderEvents(
);
}

async function fetchTimeEntriesAndSaveToSession(request: Request) {
const cookieHeader = request.headers.get("Cookie");
const session = await getSession(cookieHeader);

const clientId = await getClientId(request);
const employeeId = await getEmployeeId(request);

const troiApi = await getTroiApi(
session.get("username"),
session.get("troiPassword"),
);

const calculationPositions = await getCalculationPositions(request);
const entries: TimeEntry[] = (
await Promise.all(
calculationPositions.map((calcPos) =>
troiApi.makeRequest({
url: "/billings/hours",
params: {
clientId: clientId.toString(),
employeeId: employeeId.toString(),
calculationPositionId: calcPos.id.toString(),
startDate: formatDateToYYYYMMDD(addDaysToDate(new Date(), -366)),
endDate: formatDateToYYYYMMDD(addDaysToDate(new Date(), 366)),
},
}),
),
)
)
.flat()
.map((e: any) => {
return {
id: e.id,
date: e.Date,
hours: e.Quantity,
description: e.Remark,
calculationPosition: e.CalculationPosition.id,
};
});

const entriesById: TimeEntries = {};
for (const entry of entries) {
entriesById[entry.id] = entry;
}

session.set("troiTimeEntries", entriesById);
await commitSession(session);

return entriesById;
}

export async function getTimeEntries(request: Request): Promise<TimeEntries> {
return staleWhileRevalidate(
request,
fetchTimeEntriesAndSaveToSession,
"troiTimeEntries",
);
}

/**
* Return data from the session cache and revalidate cached data in the background.
*
Expand Down

0 comments on commit c4c969b

Please sign in to comment.