59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { z } from "zod";
|
|
import { protectedProcedure, router } from "../../init";
|
|
import { dataManager } from "../../../lib/data-manager-instance";
|
|
import {
|
|
ProductSchema,
|
|
CreateProductInput,
|
|
UpdateProductInput,
|
|
} from "@repo/shared";
|
|
|
|
export const productRouter = router({
|
|
list: protectedProcedure
|
|
.output(z.array(ProductSchema))
|
|
.query(({ ctx }) =>
|
|
dataManager.products.getProducts(ctx.staff.enterpriseId),
|
|
),
|
|
|
|
byId: protectedProcedure
|
|
.input(z.object({ id: z.number().int() }))
|
|
.output(ProductSchema.nullable())
|
|
.query(({ ctx, input }) =>
|
|
dataManager.products.getProductById(
|
|
input.id,
|
|
ctx.staff.enterpriseId,
|
|
),
|
|
),
|
|
|
|
create: protectedProcedure
|
|
.input(CreateProductInput)
|
|
.output(ProductSchema)
|
|
.mutation(({ ctx, input }) =>
|
|
dataManager.products.createProduct(
|
|
input,
|
|
ctx.staff.enterpriseId,
|
|
),
|
|
),
|
|
|
|
update: protectedProcedure
|
|
.input(UpdateProductInput)
|
|
.output(ProductSchema.nullable())
|
|
.mutation(({ ctx, input }) => {
|
|
const { id, ...patch } = input;
|
|
return dataManager.products.updateProduct(
|
|
id,
|
|
patch,
|
|
ctx.staff.enterpriseId,
|
|
);
|
|
}),
|
|
|
|
remove: protectedProcedure
|
|
.input(z.object({ id: z.number().int() }))
|
|
.output(z.object({ ok: z.boolean() }))
|
|
.mutation(async ({ ctx, input }) => {
|
|
const ok = await dataManager.products.deleteProduct(
|
|
input.id,
|
|
ctx.staff.enterpriseId,
|
|
);
|
|
return { ok };
|
|
}),
|
|
});
|