diff --git a/src/grist/service.ts b/src/grist/service.ts index 4db2bc9..c910802 100644 --- a/src/grist/service.ts +++ b/src/grist/service.ts @@ -43,7 +43,7 @@ export class GristService extends BaseService { return Math.ceil(diff / 86400000); } - private transformToPersonalGoals( + transformToPersonalGoals( obj: Record, ): GristRecord_PersonalGoals { return { diff --git a/src/homepage/routes.ts b/src/homepage/routes.ts index 86866cc..b63f1a7 100644 --- a/src/homepage/routes.ts +++ b/src/homepage/routes.ts @@ -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(), + response: { + 200: z.string(), + 418: z.object({ + error: z.string(), + }), + }, + hide: true, + }, + }, + async (request, reply) => { + const update = request.body as Record[]; + 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", { diff --git a/src/index.ts b/src/index.ts index 7f4fe35..41157a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,7 +114,7 @@ async function verifyAPIKey( request: FastifyRequest, reply: FastifyReply, ): Promise { - 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",