31 lines
802 B
TypeScript
31 lines
802 B
TypeScript
import { router, publicProcedure } from "./trpc";
|
|
import { db } from "./db";
|
|
import { z } from "zod";
|
|
|
|
export const infoRoute = router({
|
|
getAllProvinces: publicProcedure.query(getProvinces),
|
|
getAllGroups: publicProcedure.query(getGroups),
|
|
getAllZones: publicProcedure
|
|
.input(z.object({ provice_id: z.number().optional() }))
|
|
.query(async ({ input }) => await getZone(input.provice_id)),
|
|
});
|
|
|
|
async function getProvinces() {
|
|
return await db.query.province.findMany();
|
|
}
|
|
|
|
async function getZone(province?: number) {
|
|
return await db.query.zone.findMany({
|
|
where: (zone, { and, eq }) => {
|
|
if (province === undefined) {
|
|
return and();
|
|
}
|
|
return eq(zone.province, province);
|
|
},
|
|
});
|
|
}
|
|
|
|
async function getGroups() {
|
|
return await db.query.group.findMany();
|
|
}
|