|
| 1 | +import { and, desc, eq, lte } from "@acme/db"; |
| 2 | +import { ModelPricing, zInsertModelPricing } from "@acme/db/schema"; |
| 3 | +import { TRPCError, type TRPCRouterRecord } from "@trpc/server"; |
| 4 | +import { z } from "zod/v4"; |
| 5 | +import { publicProcedure } from "../trpc.js"; |
| 6 | + |
| 7 | +export const modelPricingRouter = { |
| 8 | + getAllPricingForModel: publicProcedure |
| 9 | + .input( |
| 10 | + z.object({ |
| 11 | + model_id: z.string(), |
| 12 | + model_provider: z.string(), |
| 13 | + }), |
| 14 | + ) |
| 15 | + .query(async ({ ctx, input }) => { |
| 16 | + try { |
| 17 | + const pricing = await ctx.db.query.ModelPricing.findMany({ |
| 18 | + orderBy: [desc(ModelPricing.effective_date)], |
| 19 | + where: and( |
| 20 | + eq(ModelPricing.model_id, input.model_id), |
| 21 | + eq(ModelPricing.model_provider, input.model_provider), |
| 22 | + lte(ModelPricing.effective_date, new Date().toISOString()), |
| 23 | + ), |
| 24 | + }); |
| 25 | + |
| 26 | + return pricing; |
| 27 | + } catch (error) { |
| 28 | + console.error( |
| 29 | + "Database error in modelPricing.getAllPricingForModel:", |
| 30 | + error, |
| 31 | + ); |
| 32 | + throw new TRPCError({ |
| 33 | + code: "INTERNAL_SERVER_ERROR", |
| 34 | + message: "Failed to get model pricing", |
| 35 | + }); |
| 36 | + } |
| 37 | + }), |
| 38 | + getCurrentPricing: publicProcedure |
| 39 | + .input( |
| 40 | + z.object({ |
| 41 | + model_id: z.string(), |
| 42 | + model_provider: z.string(), |
| 43 | + unit_type: z.string(), |
| 44 | + }), |
| 45 | + ) |
| 46 | + .query(async ({ ctx, input }) => { |
| 47 | + try { |
| 48 | + const pricing = await ctx.db.query.ModelPricing.findFirst({ |
| 49 | + orderBy: [desc(ModelPricing.effective_date)], |
| 50 | + where: and( |
| 51 | + eq(ModelPricing.model_id, input.model_id), |
| 52 | + eq(ModelPricing.model_provider, input.model_provider), |
| 53 | + eq(ModelPricing.unit_type, input.unit_type), |
| 54 | + lte(ModelPricing.effective_date, new Date().toISOString()), |
| 55 | + ), |
| 56 | + }); |
| 57 | + |
| 58 | + return pricing; |
| 59 | + } catch (error) { |
| 60 | + console.error( |
| 61 | + "Database error in modelPricing.getCurrentPricing:", |
| 62 | + error, |
| 63 | + ); |
| 64 | + throw new TRPCError({ |
| 65 | + code: "INTERNAL_SERVER_ERROR", |
| 66 | + message: "Failed to get current pricing", |
| 67 | + }); |
| 68 | + } |
| 69 | + }), |
| 70 | + |
| 71 | + upsertPricing: publicProcedure |
| 72 | + .input(zInsertModelPricing) |
| 73 | + .mutation(async ({ ctx, input }) => { |
| 74 | + try { |
| 75 | + const [pricing] = await ctx.db |
| 76 | + .insert(ModelPricing) |
| 77 | + .values(input) |
| 78 | + .onConflictDoUpdate({ |
| 79 | + set: { |
| 80 | + price_per_unit: input.price_per_unit, |
| 81 | + }, |
| 82 | + target: [ |
| 83 | + ModelPricing.model_id, |
| 84 | + ModelPricing.model_provider, |
| 85 | + ModelPricing.unit_type, |
| 86 | + ModelPricing.effective_date, |
| 87 | + ], |
| 88 | + }) |
| 89 | + .returning(); |
| 90 | + |
| 91 | + return pricing; |
| 92 | + } catch (error) { |
| 93 | + console.error("Database error in modelPricing.upsertPricing:", error); |
| 94 | + throw new TRPCError({ |
| 95 | + code: "INTERNAL_SERVER_ERROR", |
| 96 | + message: "Failed to upsert pricing", |
| 97 | + }); |
| 98 | + } |
| 99 | + }), |
| 100 | +} satisfies TRPCRouterRecord; |
0 commit comments