better-auth/packages/database/prisma/queries/purchases.ts

70 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2025-10-29 13:43:43 +05:00
import type { z } from "zod";
import { db } from "../client";
import type {
PurchaseUncheckedCreateInputSchema,
PurchaseUncheckedUpdateInputSchema,
} from "../zod";
export async function getPurchaseById(id: string) {
return db.purchase.findUnique({
where: { id },
});
}
export async function getPurchasesByOrganizationId(organizationId: string) {
return db.purchase.findMany({
where: {
organizationId,
},
});
}
export async function getPurchasesByUserId(userId: string) {
return db.purchase.findMany({
where: {
userId,
},
});
}
export async function getPurchaseBySubscriptionId(subscriptionId: string) {
return db.purchase.findFirst({
where: {
subscriptionId,
},
});
}
export async function createPurchase(
purchase: z.infer<typeof PurchaseUncheckedCreateInputSchema>,
) {
const created = await db.purchase.create({
data: purchase,
});
return getPurchaseById(created.id);
}
export async function updatePurchase(
purchase: z.infer<typeof PurchaseUncheckedUpdateInputSchema> & {
id: string;
},
) {
const updated = await db.purchase.update({
where: {
id: purchase.id,
},
data: purchase,
});
return getPurchaseById(updated.id);
}
export async function deletePurchaseBySubscriptionId(subscriptionId: string) {
await db.purchase.delete({
where: {
subscriptionId,
},
});
}