Refactor to vertical slice
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { db } from '@/lib/prisma';
|
||||
import type { AdminCreatePlanInput } from '@repo/api-contract';
|
||||
import { v7 as uuidv7 } from 'uuid';
|
||||
|
||||
export async function createPlan(input: AdminCreatePlanInput) {
|
||||
const existing = await db.plan.findUnique({
|
||||
where: { code: input.code },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return { ok: false as const, message: 'Ya existe un plan con ese código.' };
|
||||
}
|
||||
|
||||
const plan = await db.plan.create({
|
||||
data: {
|
||||
code: input.code,
|
||||
name: input.name,
|
||||
price: input.price,
|
||||
rules: input.rules,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
ok: true as const,
|
||||
data: {
|
||||
code: plan.code,
|
||||
name: plan.name,
|
||||
price: Number(plan.price),
|
||||
rules: plan.rules,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { AppContext } from '@/types/hono';
|
||||
import type { AdminCreatePlanInput } from '@repo/api-contract';
|
||||
import { createPlan } from './create-plan.business';
|
||||
|
||||
export async function createPlanHandler(c: AppContext) {
|
||||
const body = c.req.valid('json' as never) as AdminCreatePlanInput;
|
||||
|
||||
const result = await createPlan(body);
|
||||
|
||||
if (!result.ok) {
|
||||
return c.json({ message: result.message }, 409);
|
||||
}
|
||||
|
||||
return c.json(result.data, 201);
|
||||
}
|
||||
Reference in New Issue
Block a user