Skip to content

Commit

Permalink
refactor: tweak logic for paid workspace checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerourke committed Jan 17, 2024
1 parent 05995af commit 09e7997
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
11 changes: 8 additions & 3 deletions src/api/apiRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,14 @@ export class ApiError extends Error {
public toJson(): Record<string, any> {
const headers: Record<string, string> = {};

// @ts-ignore
for (const [name, value] of this.#response.headers) {
headers[name] = value;
// We don't want this to blow up the app if it fails:
try {
// @ts-ignore
for (const [name, value] of this.#response.headers) {
headers[name] = value;
}
} catch {
// Do nothing.
}

return {
Expand Down
27 changes: 17 additions & 10 deletions src/redux/tasks/sagas/togglTasksSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
targetProjectsByIdSelector,
} from "~/redux/projects/projectsSelectors";
import { userIdToLinkedIdSelector } from "~/redux/users/usersSelectors";
import { allWorkspacesByIdSelector } from "~/redux/workspaces/workspacesSelectors";
import { allFreeWorkspaceIdsSelector } from "~/redux/workspaces/workspacesSelectors";
import { EntityGroup, ToolName, type Project, type Task } from "~/types";
import { validStringify } from "~/utilities/textTransforms";

Expand Down Expand Up @@ -74,19 +74,24 @@ export function* fetchTogglTasksSaga(): SagaIterator<Task[]> {
// prettier-ignore
const tableEntries = Object.entries(togglProjectsTable) as [string, Project[]][];

const allWorkspacesById = yield select(allWorkspacesByIdSelector);
const allFreeWorkspaceIds = yield select(allFreeWorkspaceIdsSelector);

const allTasks: Task[] = [];

const apiDelay = getApiDelayForTool(ToolName.Toggl);

for (const [workspaceId, projects] of tableEntries) {
const workspace = allWorkspacesById[workspaceId] ?? { isPaid: true };
const freeWorkspaceIds = new Set<string>();

for (const [workspaceId, projects] of tableEntries) {
// Toggl tasks can't be fetched for paid workspaces. Rather than make a
// bunch of requests that return 400, we skip them:
const isFreeAccount = !workspace.isPaid;
if (isFreeAccount) {
// bunch of requests that return 402, we skip them. We add it to a set
// rather than just continue out of the loop, so we can catch fetch errors
// as well and prevent further fetches:
if (allFreeWorkspaceIds.includes(workspaceId)) {
freeWorkspaceIds.add(workspaceId);
}

if (freeWorkspaceIds.has(workspaceId)) {
continue;
}

Expand All @@ -100,10 +105,12 @@ export function* fetchTogglTasksSaga(): SagaIterator<Task[]> {

allTasks.push(...tasks);
} catch (err: AnyValid) {
console.log(err instanceof ApiError);

// User can't create or fetch tasks because the workspace isn't paid:
if (err.status === 402) {
if (err instanceof ApiError && err.statusCode === 402) {
// We save the workspace ID, so we can _continue_ through the loop
// rather than break, and we don't want to hit this error again for
// the workspace in the next iteration of the loop:
freeWorkspaceIds.add(workspaceId);
continue;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/redux/workspaces/workspacesSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@ export const targetWorkspacesSelector = createSelector(
},
);

export const allWorkspacesByIdSelector = createSelector(
export const allFreeWorkspaceIdsSelector = createSelector(
sourceWorkspacesSelector,
targetWorkspacesSelector,
(sourceWorkspaces, targetWorkspaces): Record<string, Workspace> => {
const allWorkspacesById: Record<string, Workspace> = {};
(sourceWorkspaces, targetWorkspaces): string[] => {
const allFreeWorkspaceIds: string[] = [];

for (const workspace of [...sourceWorkspaces, ...targetWorkspaces]) {
allWorkspacesById[workspace.id] = workspace;
if (!workspace.isPaid) {
allFreeWorkspaceIds.push(workspace.id);
}
}

return allWorkspacesById;
return allFreeWorkspaceIds;
},
);

Expand Down

0 comments on commit 09e7997

Please sign in to comment.