infoalloggi-monorepo/apps/infoalloggi/src/server/controllers/flags.controller.ts
Marco Pedone 9c18ca37ab Refactor API structure and update flag handling
- Replaced references to `api.settings` with `api.etichette`, `api.flags`, and `api.strings` in various admin pages to align with new API structure.
- Removed the deprecated `settingsRouter` and introduced `flagsRouter`, `etichetteRouter`, and `stringsRouter` to handle respective functionalities.
- Updated the `Flags` schema to change the `value` field from string to boolean.
- Modified flag handling logic in the `SendMessage` and `NewMail` functions to check for boolean flags instead of string values.
- Added migration script to convert the `value` column in the `flags` table from string to boolean.
- Implemented new routers for banners, etichette, flags, revalidation, and strings to encapsulate related functionalities.
2025-11-20 12:38:42 +01:00

102 lines
2 KiB
TypeScript

import { TRPCError } from "@trpc/server";
import type { Flags, FlagsId } from "~/schemas/public/Flags";
import type { Querier } from "~/server/db";
export const EditFlagHandler = async ({
db,
input,
}: {
db: Querier;
input: Flags;
}) => {
try {
return await db
.updateTable("flags")
.set({
value: input.value,
})
.where("id", "=", input.id)
.returningAll()
.execute();
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error editing flag: ${(e as Error).message}`,
});
}
};
export const GetFlagHandler = async ({ db }: { db: Querier }) => {
try {
return await db.selectFrom("flags").selectAll().execute();
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error getting flags: ${(e as Error).message}`,
});
}
};
export const GetFlagValueHandler = async ({
db,
id,
}: {
db: Querier;
id: string;
}) => {
try {
const flag = await db
.selectFrom("flags")
.select("value")
.where("id", "=", id as FlagsId)
.executeTakeFirst();
return flag?.value ?? null;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error getting flag value: ${(e as Error).message}`,
});
}
};
export const RemoveFlagHandler = async ({
db,
id,
}: {
db: Querier;
id: FlagsId;
}) => {
try {
await db.deleteFrom("flags").where("id", "=", id).execute();
return true;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error removing flag: ${(e as Error).message}`,
});
}
};
export const AddFlagHandler = async ({
db,
input,
}: {
db: Querier;
input: Flags;
}) => {
try {
await db
.insertInto("flags")
.values({
id: input.id,
value: input.value,
})
.onConflict((oc) => oc.column("id").doNothing())
.returningAll()
.execute();
return true;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error adding flag: ${(e as Error).message}`,
});
}
};