70 lines
1.3 KiB
TypeScript
70 lines
1.3 KiB
TypeScript
|
|
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,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|