formatting and grist

This commit is contained in:
Darius
2026-02-06 18:59:34 +01:00
parent a8280ce27f
commit e1ef661a7a
9 changed files with 141 additions and 16 deletions

15
src/grist/client.ts Normal file
View File

@@ -0,0 +1,15 @@
import { BaseClient } from "@dpu/shared";
import { printNetworkError } from "@dpu/shared/dist/logger.js";
export class GristClient extends BaseClient {
async get<T>(endpoint: string): Promise<T> {
try {
const response = await this.getAxios().get<T>(`${endpoint}`);
return response.data;
} catch (error) {
printNetworkError(error);
throw error;
}
}
}

39
src/grist/routes.ts Normal file
View File

@@ -0,0 +1,39 @@
import type { GristRecord_PersonalGoals } from "@dpu/shared";
import type { FastifyInstance } from "fastify";
import { z } from "zod";
import type { GristService } from "./service";
export async function gristRoutes(
fastify: FastifyInstance,
{
gristService,
}: {
gristService: GristService;
},
) {
fastify.get(
"/grist/today",
{
schema: {
description: "Get goals for today",
tags: ["grist"],
response: {
200: z.custom<GristRecord_PersonalGoals>(),
418: z.object({
error: z.string(),
}),
},
},
},
async (_request, reply) => {
const service_result = await gristService.getToday();
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
}

53
src/grist/service.ts Normal file
View File

@@ -0,0 +1,53 @@
import {
BaseService,
type GristRecord_PersonalGoals,
logWarning,
type ServiceResult,
} from "@dpu/shared";
import { Config } from "../config.js";
import type { GristClient } from "./client.js";
export class GristService extends BaseService<GristClient> {
async getToday(): Promise<ServiceResult<GristRecord_PersonalGoals | string>> {
try {
const filter_string = encodeURIComponent(
`{ "id": [${this.getTodayAsId()}]}`,
);
const query = `${Config.grist.table_personal_goals_path}?filter=${filter_string}`;
const response =
await this.getClient().get<Record<string, unknown>>(query);
return this.getSuccessfulResult(this.transformToPersonalGoals(response));
} catch {
const error_message = "error getting record from grist";
logWarning(error_message);
return this.getErrorResult(error_message);
}
}
getTodayAsId(date = new Date()) {
const start = new Date(date.getFullYear(), 0, 0);
const diff = date.getTime() - start.getTime();
return Math.floor(diff / 86400000);
}
transformToPersonalGoals(
obj: Record<string, unknown>,
): GristRecord_PersonalGoals {
return {
went_outside: (obj.went_outside as boolean) ?? false,
standing: (obj.standing as boolean) ?? false,
standing_goal: (obj.standing_goal as number) ?? 0,
steps: (obj.steps as boolean) ?? false,
steps_goal: (obj.steps_goal as number) ?? 0,
pushups: (obj.pushups as boolean) ?? false,
squats: (obj.squats as boolean) ?? false,
leg_raises: (obj.leg_raises as boolean) ?? false,
reps_goal: (obj.reps_goal as number) ?? 0,
stairs: (obj.stairs as boolean) ?? false,
stairs_goal: (obj.stairs_goal as number) ?? 0,
is_workday: (obj.WeekdayHelper as number) === 1,
};
}
}