grist updates

This commit is contained in:
Darius
2026-02-08 16:31:12 +01:00
parent b7daaab9cf
commit 12e7611267
3 changed files with 46 additions and 3 deletions

View File

@@ -43,7 +43,7 @@ export class GristService extends BaseService<GristClient> {
return Math.ceil(diff / 86400000);
}
private transformToPersonalGoals(
transformToPersonalGoals(
obj: Record<string, unknown>,
): GristRecord_PersonalGoals {
return {

View File

@@ -10,6 +10,7 @@ import type { FastifyInstance, FastifyReply } from "fastify";
import type { FastifyRequest } from "fastify/types/request.js";
import { z } from "zod";
import { Config } from "../config.js";
import type { GristService } from "../grist/service.js";
import type { HomeAssistantService } from "../homeassistant/service.js";
import type { HomepageService } from "./service.js";
@@ -17,10 +18,12 @@ export async function homepageRoutes(
fastify: FastifyInstance,
{
hpService,
gristService,
haService,
verifyAPIKey,
}: {
hpService: HomepageService;
gristService: GristService;
haService: HomeAssistantService;
verifyAPIKey: (
request: FastifyRequest,
@@ -61,6 +64,41 @@ export async function homepageRoutes(
},
);
fastify.post(
"/homepage/update/grist",
{
preHandler: verifyAPIKey,
schema: {
description: "Update information for component on dpu status page",
tags: ["homepage"],
body: z.custom<GristRecord_PersonalGoals>(),
response: {
200: z.string(),
418: z.object({
error: z.string(),
}),
},
hide: true,
},
},
async (request, reply) => {
const update = request.body as Record<string, unknown>[];
const service_result = await hpService.updatePartial([
{
component: "grist",
data: gristService.transformToPersonalGoals(update[0]),
},
]);
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return service_result.result;
},
);
fastify.post(
"/homepage/update/homeassistant",
{

View File

@@ -114,7 +114,7 @@ async function verifyAPIKey(
request: FastifyRequest,
reply: FastifyReply,
): Promise<void> {
const apiKey = request.headers["x-api-key"];
const apiKey = request.headers["x-api-key"] ?? request.headers.authorization;
if (!apiKey || apiKey !== Config.api_key) {
logError("POST Request with wrong API key received");
@@ -129,7 +129,12 @@ await fastify.register(gristRoutes, { gristService });
await fastify.register(homeAssistantRoutes, { haService, verifyAPIKey });
await fastify.register(tidalRoutes, { tidalService, verifyAPIKey });
await fastify.register(wsRoutes, { wsService });
await fastify.register(homepageRoutes, { hpService, haService, verifyAPIKey });
await fastify.register(homepageRoutes, {
hpService,
gristService,
haService,
verifyAPIKey,
});
fastify.get(
"/ping",