import type { ComponentUpdate, GristRecord_PersonalGoals, HA_Update, HomeAssistantEntity, TidalGetCurrent, } from "@dpu/shared"; 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"; export async function privateHomepageRoutes( fastify: FastifyInstance, { hpService, gristService, haService, verifyAPIKey, }: { hpService: HomepageService; gristService: GristService; haService: HomeAssistantService; verifyAPIKey: ( request: FastifyRequest, reply: FastifyReply, ) => Promise; }, ) { 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", { 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 ha_update = request.body as HA_Update; const ha_entity: HomeAssistantEntity = { entity_id: ha_update.entity_id, state: ha_update.state, attributes: ha_update.attributes, last_changed: ha_update.timestamp, }; const updates: ComponentUpdate[] = []; switch (ha_update.entity_id) { case Config.homeassistant.id_sensor_desk_binary: { updates.push({ component: "desk", data: haService.convertPosResultToApiAnswer( haService.convertHaEntityToPosResult(ha_entity), ), }); break; } case Config.homeassistant.id_sensor_roomtemp: updates.push({ component: "desk", data: ha_entity.state, }); break; default: } const service_result = await hpService.updatePartial(updates); if (!service_result.successful) { reply.code(418); return { error: service_result.result }; } return service_result.result; }, ); fastify.post( "/homepage/update/tidal", { 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 TidalGetCurrent; const service_result = await hpService.updatePartial([ { component: "tidal", data: update, }, ]); if (!service_result.successful) { reply.code(418); return { error: service_result.result }; } return service_result.result; }, ); }