From 277f3285f7443a7d3476373418e0357e4ead74bf Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 7 Sep 2025 22:07:19 +0100 Subject: [PATCH] feat: add subscription repo funcs --- .../db/src/repository/subscription.repo.ts | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 packages/db/src/repository/subscription.repo.ts diff --git a/packages/db/src/repository/subscription.repo.ts b/packages/db/src/repository/subscription.repo.ts new file mode 100644 index 00000000..a763f048 --- /dev/null +++ b/packages/db/src/repository/subscription.repo.ts @@ -0,0 +1,77 @@ +import { eq } from "drizzle-orm"; + +import type { dbClient } from "@kan/db/client"; +import { subscription } from "@kan/db/schema"; + +export const updateById = async ( + db: dbClient, + subscriptionId: number, + updates: { + unlimitedSeats?: boolean; + status?: string; + seats?: number | null; + periodStart?: Date | null; + periodEnd?: Date | null; + cancelAtPeriodEnd?: boolean | null; + }, +) => { + const [result] = await db + .update(subscription) + .set({ + ...updates, + updatedAt: new Date(), + }) + .where(eq(subscription.id, subscriptionId)) + .returning({ + id: subscription.id, + plan: subscription.plan, + status: subscription.status, + unlimitedSeats: subscription.unlimitedSeats, + }); + + return result; +}; + +export const updateByStripeSubscriptionId = async ( + db: dbClient, + stripeSubscriptionId: string, + updates: { + unlimitedSeats?: boolean; + status?: string; + seats?: number | null; + periodStart?: Date | null; + periodEnd?: Date | null; + cancelAtPeriodEnd?: boolean | null; + }, +) => { + const [result] = await db + .update(subscription) + .set({ + ...updates, + updatedAt: new Date(), + }) + .where(eq(subscription.stripeSubscriptionId, stripeSubscriptionId)) + .returning({ + id: subscription.id, + plan: subscription.plan, + status: subscription.status, + unlimitedSeats: subscription.unlimitedSeats, + }); + + return result; +}; + +export const getByStripeSubscriptionId = async ( + db: dbClient, + stripeSubscriptionId: string, +) => { + return await db.query.subscription.findFirst({ + where: eq(subscription.stripeSubscriptionId, stripeSubscriptionId), + }); +}; + +export const getByReferenceId = async (db: dbClient, referenceId: string) => { + return await db.query.subscription.findMany({ + where: eq(subscription.referenceId, referenceId), + }); +};