data.ts 897 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import prisma from '@/lib/prisma'
  2. export async function getPlans() {
  3. // Gets all active plans
  4. return await prisma.plan.findMany({
  5. where: {
  6. NOT: [
  7. { status: 'draft' },
  8. { status: 'pending' }
  9. ]
  10. },
  11. include: {
  12. subscriptions: true
  13. }
  14. })
  15. }
  16. export async function getPlan(variantId: number) {
  17. // Gets single active plan by ID
  18. return await prisma.plan.findFirst({
  19. where: {
  20. variantId: variantId,
  21. NOT: [
  22. { status: 'draft' },
  23. { status: 'pending' }
  24. ]
  25. },
  26. include: {
  27. subscriptions: true
  28. }
  29. })
  30. }
  31. export async function getSubscription(userId?: string) {
  32. // Gets the most recent subscription
  33. return await prisma.subscription.findFirst({
  34. where: {
  35. userId: userId
  36. },
  37. include: {
  38. plan: true,
  39. user: true
  40. },
  41. orderBy: {
  42. lemonSqueezyId: 'desc'
  43. }
  44. })
  45. }