2025-08-04 17:45:44 +02:00
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
import type { FlagsId } from "~/schemas/public/Flags";
|
|
|
|
|
import type { Querier } from "~/server/db";
|
|
|
|
|
|
|
|
|
|
export const EditFlagHandler = async ({
|
2025-08-28 18:27:07 +02:00
|
|
|
db,
|
|
|
|
|
input,
|
2025-08-04 17:45:44 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
db: Querier;
|
|
|
|
|
input: {
|
|
|
|
|
id: FlagsId;
|
|
|
|
|
value: string;
|
|
|
|
|
};
|
2025-08-04 17:45:44 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
export const GetFlagHandler = async ({ db }: { db: Querier }) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const GetFlagValueHandler = async ({
|
2025-08-28 18:27:07 +02:00
|
|
|
db,
|
|
|
|
|
id,
|
2025-08-04 17:45:44 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
db: Querier;
|
|
|
|
|
id: string;
|
2025-08-04 17:45:44 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const RemoveFlagHandler = async ({
|
2025-08-28 18:27:07 +02:00
|
|
|
db,
|
|
|
|
|
id,
|
2025-08-04 17:45:44 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
db: Querier;
|
|
|
|
|
id: FlagsId;
|
2025-08-04 17:45:44 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
export const AddFlagHandler = async ({
|
2025-08-28 18:27:07 +02:00
|
|
|
db,
|
|
|
|
|
input,
|
2025-08-04 17:45:44 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
db: Querier;
|
|
|
|
|
input: {
|
|
|
|
|
id: FlagsId;
|
|
|
|
|
value: string;
|
|
|
|
|
};
|
2025-08-04 17:45:44 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|