|
| 1 | +import app from "../../stripe.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "stripe-search-customers", |
| 5 | + name: "Search Customers", |
| 6 | + type: "action", |
| 7 | + version: "0.0.1", |
| 8 | + description: "Search customers by various attributes like email domain, created date, etc. [See the docs](https://stripe.com/docs/api/customers/search) for more information", |
| 9 | + props: { |
| 10 | + app, |
| 11 | + query: { |
| 12 | + type: "string", |
| 13 | + label: "Search Query", |
| 14 | + description: "Search query using Stripe's [search query language](https://stripe.com/docs/search#search-query-language). Example: `email~\"example.com\" AND created>1609459200`", |
| 15 | + optional: true, |
| 16 | + }, |
| 17 | + email: { |
| 18 | + type: "string", |
| 19 | + label: "Email", |
| 20 | + description: "Search by customer email address (e.g.,`user@example.com`)", |
| 21 | + optional: true, |
| 22 | + }, |
| 23 | + emailDomain: { |
| 24 | + type: "string", |
| 25 | + label: "Email Domain", |
| 26 | + description: "Search by email **domain** (e.g., `example.com` to find all emails from that domain)", |
| 27 | + optional: true, |
| 28 | + }, |
| 29 | + createdAfter: { |
| 30 | + type: "string", |
| 31 | + label: "Created After", |
| 32 | + description: "Filter customers created after this date (format: `YYYY-MM-DD`)", |
| 33 | + optional: true, |
| 34 | + }, |
| 35 | + createdBefore: { |
| 36 | + type: "string", |
| 37 | + label: "Created Before", |
| 38 | + description: "Filter customers created before this date (format: `YYYY-MM-DD`)", |
| 39 | + optional: true, |
| 40 | + }, |
| 41 | + limit: { |
| 42 | + propDefinition: [ |
| 43 | + app, |
| 44 | + "limit", |
| 45 | + ], |
| 46 | + description: "Maximum number of customers to return", |
| 47 | + default: 25, |
| 48 | + }, |
| 49 | + }, |
| 50 | + methods: { |
| 51 | + convertDateToTimestamp(dateStr) { |
| 52 | + if (!dateStr) return null; |
| 53 | + const date = new Date(`${dateStr}T00:00:00Z`); |
| 54 | + return Math.floor(date.getTime() / 1000); |
| 55 | + }, |
| 56 | + |
| 57 | + buildSearchQuery() { |
| 58 | + const queryParts = []; |
| 59 | + |
| 60 | + if (this.query) { |
| 61 | + return this.query; |
| 62 | + } |
| 63 | + |
| 64 | + if (this.email) { |
| 65 | + // Escape quotes in the email value to prevent query syntax errors |
| 66 | + const escapedEmail = this.email.replace(/"/g, "\\\""); |
| 67 | + queryParts.push(`email="${escapedEmail}"`); |
| 68 | + } |
| 69 | + |
| 70 | + if (this.emailDomain) { |
| 71 | + // Escape quotes in the domain value to prevent query syntax errors |
| 72 | + const escapedDomain = this.emailDomain.replace(/"/g, "\\\""); |
| 73 | + queryParts.push(`email~"${escapedDomain}"`); |
| 74 | + } |
| 75 | + |
| 76 | + const afterTimestamp = this.convertDateToTimestamp(this.createdAfter); |
| 77 | + if (afterTimestamp) { |
| 78 | + queryParts.push(`created>${afterTimestamp}`); |
| 79 | + } |
| 80 | + |
| 81 | + const beforeTimestamp = this.convertDateToTimestamp(this.createdBefore); |
| 82 | + if (beforeTimestamp) { |
| 83 | + queryParts.push(`created<${beforeTimestamp}`); |
| 84 | + } |
| 85 | + |
| 86 | + return queryParts.join(" AND "); |
| 87 | + }, |
| 88 | + }, |
| 89 | + async run({ $ }) { |
| 90 | + const query = this.buildSearchQuery(); |
| 91 | + |
| 92 | + if (!query) { |
| 93 | + throw new Error("Please provide at least one search parameter"); |
| 94 | + } |
| 95 | + |
| 96 | + // Use a specific API version for search functionality |
| 97 | + const results = await this.app.sdk("2023-10-16").customers.search({ |
| 98 | + query, |
| 99 | + limit: this.limit, |
| 100 | + }); |
| 101 | + |
| 102 | + const resultCount = results.data.length; |
| 103 | + $.export("$summary", `Found ${resultCount} customer${resultCount === 1 |
| 104 | + ? "" |
| 105 | + : "s"}`); |
| 106 | + |
| 107 | + return results.data; |
| 108 | + }, |
| 109 | +}; |
0 commit comments