| 14 | } |
| 15 | |
| 16 | async function promptForInput(polar: Polar) { |
| 17 | // Get all organizations first |
| 18 | const organizations = await db.organization.findMany({ |
| 19 | select: { |
| 20 | id: true, |
| 21 | name: true, |
| 22 | }, |
| 23 | }); |
| 24 | |
| 25 | // Fetch all products from Polar |
| 26 | let products: any[] = []; |
| 27 | try { |
| 28 | const productsResponse = await polar.products.list({ |
| 29 | limit: 100, |
| 30 | isArchived: false, |
| 31 | sorting: ['price_amount'], |
| 32 | }); |
| 33 | products = productsResponse.result.items; |
| 34 | |
| 35 | if (products.length === 0) { |
| 36 | console.warn('Warning: No products found in Polar'); |
| 37 | } |
| 38 | } catch (error) { |
| 39 | console.error('Error fetching products from Polar:', error); |
| 40 | throw new Error('Failed to fetch products. Please check your API key and try again.'); |
| 41 | } |
| 42 | |
| 43 | const answers = await inquirer.prompt<Answers>([ |
| 44 | { |
| 45 | type: 'autocomplete', |
| 46 | name: 'productId', |
| 47 | message: 'Select product:', |
| 48 | source: (answersSoFar: any, input = '') => { |
| 49 | return products |
| 50 | .filter( |
| 51 | (product) => |
| 52 | product.name.toLowerCase().includes(input.toLowerCase()) || |
| 53 | product.id.toLowerCase().includes(input.toLowerCase()), |
| 54 | ) |
| 55 | .map((product) => { |
| 56 | const price = product.prices[0]; |
| 57 | const priceDisplay = price |
| 58 | ? `$${(price.priceAmount / 100).toFixed(2)}` |
| 59 | : 'Free'; |
| 60 | return { |
| 61 | name: `${product.name} - ${priceDisplay} (${product.id})`, |
| 62 | value: product.id, |
| 63 | }; |
| 64 | }); |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | type: 'autocomplete', |
| 69 | name: 'organizationId', |
| 70 | message: 'Select organization:', |
| 71 | source: (answersSoFar: any, input = '') => { |
| 72 | return organizations |
| 73 | .filter( |