grist updates
This commit is contained in:
@@ -43,7 +43,7 @@ export class GristService extends BaseService<GristClient> {
|
|||||||
return Math.ceil(diff / 86400000);
|
return Math.ceil(diff / 86400000);
|
||||||
}
|
}
|
||||||
|
|
||||||
private transformToPersonalGoals(
|
transformToPersonalGoals(
|
||||||
obj: Record<string, unknown>,
|
obj: Record<string, unknown>,
|
||||||
): GristRecord_PersonalGoals {
|
): GristRecord_PersonalGoals {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import type { FastifyInstance, FastifyReply } from "fastify";
|
|||||||
import type { FastifyRequest } from "fastify/types/request.js";
|
import type { FastifyRequest } from "fastify/types/request.js";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { Config } from "../config.js";
|
import { Config } from "../config.js";
|
||||||
|
import type { GristService } from "../grist/service.js";
|
||||||
import type { HomeAssistantService } from "../homeassistant/service.js";
|
import type { HomeAssistantService } from "../homeassistant/service.js";
|
||||||
import type { HomepageService } from "./service.js";
|
import type { HomepageService } from "./service.js";
|
||||||
|
|
||||||
@@ -17,10 +18,12 @@ export async function homepageRoutes(
|
|||||||
fastify: FastifyInstance,
|
fastify: FastifyInstance,
|
||||||
{
|
{
|
||||||
hpService,
|
hpService,
|
||||||
|
gristService,
|
||||||
haService,
|
haService,
|
||||||
verifyAPIKey,
|
verifyAPIKey,
|
||||||
}: {
|
}: {
|
||||||
hpService: HomepageService;
|
hpService: HomepageService;
|
||||||
|
gristService: GristService;
|
||||||
haService: HomeAssistantService;
|
haService: HomeAssistantService;
|
||||||
verifyAPIKey: (
|
verifyAPIKey: (
|
||||||
request: FastifyRequest,
|
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(
|
fastify.post(
|
||||||
"/homepage/update/homeassistant",
|
"/homepage/update/homeassistant",
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ async function verifyAPIKey(
|
|||||||
request: FastifyRequest,
|
request: FastifyRequest,
|
||||||
reply: FastifyReply,
|
reply: FastifyReply,
|
||||||
): Promise<void> {
|
): 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) {
|
if (!apiKey || apiKey !== Config.api_key) {
|
||||||
logError("POST Request with wrong API key received");
|
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(homeAssistantRoutes, { haService, verifyAPIKey });
|
||||||
await fastify.register(tidalRoutes, { tidalService, verifyAPIKey });
|
await fastify.register(tidalRoutes, { tidalService, verifyAPIKey });
|
||||||
await fastify.register(wsRoutes, { wsService });
|
await fastify.register(wsRoutes, { wsService });
|
||||||
await fastify.register(homepageRoutes, { hpService, haService, verifyAPIKey });
|
await fastify.register(homepageRoutes, {
|
||||||
|
hpService,
|
||||||
|
gristService,
|
||||||
|
haService,
|
||||||
|
verifyAPIKey,
|
||||||
|
});
|
||||||
|
|
||||||
fastify.get(
|
fastify.get(
|
||||||
"/ping",
|
"/ping",
|
||||||
|
|||||||
Reference in New Issue
Block a user