page.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import prisma from '@/lib/prisma'
  2. import LemonSqueezy from '@lemonsqueezy/lemonsqueezy.js'
  3. const ls = new LemonSqueezy(process.env.LEMON_SQUEEZY_API_KEY as string)
  4. export const dynamic = 'force-dynamic' // Don't cache API results
  5. async function getPlans() {
  6. const params = { include: ['product'] as Array<'product' | 'files'>, perPage: 50 }
  7. let hasNextPage = true;
  8. let page = 1;
  9. let variants = [] as {}[]
  10. let products = [] as Record<string, any>
  11. while (hasNextPage) {
  12. const resp = await ls.getVariants(params);
  13. variants = variants.concat(resp['data'])
  14. products = products.concat(resp['included'])
  15. if (resp['meta']['page']['lastPage'] > page) {
  16. page += 1
  17. params['page'] = page
  18. } else {
  19. hasNextPage = false
  20. }
  21. }
  22. // Nest products inside variants
  23. const prods = {};
  24. for (let i = 0; i < products.length; i++) {
  25. prods[products[i]['id']] = products[i]['attributes']
  26. }
  27. for (let i = 0; i < variants.length; i++) {
  28. variants[i]['product'] = prods[variants[i]['attributes']['product_id']]
  29. }
  30. // Save locally
  31. let variantId,
  32. variant,
  33. product,
  34. productId
  35. for (let i = 0; i < variants.length; i++) {
  36. variant = variants[i]
  37. if ( !variant['attributes']['is_subscription'] ) {
  38. console.log('Not a subscription')
  39. continue
  40. }
  41. if ( String(variant['product']['store_id']) !== process.env.LEMON_SQUEEZY_STORE_ID ) {
  42. console.log(`Store ID ${variant['product']['store_id']} does not match (${process.env.LEMON_SQUEEZY_STORE_ID})`)
  43. continue
  44. }
  45. variantId = parseInt(variant['id'])
  46. product = variant['product']
  47. productId = parseInt(variant['attributes']['product_id'])
  48. // Get variant's Price objects
  49. let prices = await ls.getPrices({ variantId: variantId, perPage: 100 })
  50. // The first object is the latest/current price
  51. let variant_price = prices['data'][0]['attributes']['unit_price']
  52. variant = variant['attributes']
  53. const updateData = {
  54. productId: productId,
  55. name: product['name'],
  56. variantName: variant['name'],
  57. status: variant['status'],
  58. sort: variant['sort'],
  59. description: variant['description'],
  60. price: variant_price, // display price in the app matches current Price object in LS
  61. interval: variant['interval'],
  62. intervalCount: variant['interval_count'],
  63. }
  64. const createData = { ...updateData, variantId}
  65. try {
  66. await prisma.plan.upsert({
  67. where: {
  68. variantId: variantId
  69. },
  70. update: updateData,
  71. create: createData
  72. })
  73. } catch (error) {
  74. console.log(variant)
  75. console.log(error)
  76. }
  77. }
  78. }
  79. export default async function Page() {
  80. await getPlans()
  81. return (
  82. <p>
  83. Done!
  84. </p>
  85. )
  86. }