feat: add subscription repo funcs
This commit is contained in:
77
packages/db/src/repository/subscription.repo.ts
Normal file
77
packages/db/src/repository/subscription.repo.ts
Normal file
@@ -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),
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user