diff --git a/firestore-stripe-payments/functions/__tests__/helpers/utils.ts b/firestore-stripe-payments/functions/__tests__/helpers/utils.ts index bf2ef4d4..e25bf3d2 100644 --- a/firestore-stripe-payments/functions/__tests__/helpers/utils.ts +++ b/firestore-stripe-payments/functions/__tests__/helpers/utils.ts @@ -52,7 +52,7 @@ export const waitForDocumentUpdate = ( document: DocumentData, field: string | number, value: any, - timeout: number = 10_000 + timeout: number = 300000 // 5 minutes ): Promise => { return new Promise((resolve, reject) => { let timedOut = false; @@ -78,12 +78,15 @@ export const waitForDocumentUpdate = ( export const waitForDocumentToExistInCollection = ( query: Query, - field: string | number, + field: string, value: any, - timeout: number = 20_000 + timeout: number = 300000 // 5 minutes ): Promise => { return new Promise((resolve, reject) => { let timedOut = false; + + let unsubscribe: () => void; // Declare unsubscribe here + const timer = setTimeout(() => { timedOut = true; reject( @@ -91,21 +94,32 @@ export const waitForDocumentToExistInCollection = ( `Timeout waiting for firestore document to exist with field ${field} in collection` ) ); + if (unsubscribe) { + unsubscribe(); // Unsubscribe when timed out + } }, timeout); - const unsubscribe = query.onSnapshot(async (snapshot) => { - const docs = snapshot.docChanges(); + unsubscribe = query.onSnapshot(async (snapshot) => { + try { + const docs = snapshot.docChanges(); - const record: DocumentData = docs.filter( - ($) => $.doc.data()[field] === value - )[0]; + const record: DocumentData = docs.filter( + ($) => $.doc.data()[field] === value + )[0]; - if (record) { - unsubscribe(); - if (!timedOut) { - clearTimeout(timer); - resolve(record); + if (record) { + unsubscribe(); + if (!timedOut) { + clearTimeout(timer); + resolve(record); + } + } + } catch (error) { + if (unsubscribe) { + unsubscribe(); // Unsubscribe on error } + console.log('Error: ', error); + reject(error); } }); }); diff --git a/firestore-stripe-payments/functions/__tests__/helpers/webhooks.ts b/firestore-stripe-payments/functions/__tests__/helpers/webhooks.ts index 1c77bd11..e59facad 100644 --- a/firestore-stripe-payments/functions/__tests__/helpers/webhooks.ts +++ b/firestore-stripe-payments/functions/__tests__/helpers/webhooks.ts @@ -39,14 +39,20 @@ export const clearWebhooks = async (id) => { }; export const clearAllWebhooks = async () => { + console.log('Step 1 >>>>>'); const stripe = require('stripe')(process.env.STRIPE_API_KEY); + console.log('Step 2 >>>>>'); const webhooks = await stripe.webhookEndpoints.list(); + console.log('Step 3 >>>>>'); + + /** Log how weekbhooks have been found */ + console.log('Found webhooks: ', webhooks.data.length); + for await (const webhook of webhooks.data) { - if (webhook.url.includes('ngrok.io')) { - await stripe.webhookEndpoints.del(webhook.id); - } + console.log('Deleting webhook: ', webhook.id); + await stripe.webhookEndpoints.del(webhook.id); } return Promise.resolve(); diff --git a/firestore-stripe-payments/functions/__tests__/run-script.ts b/firestore-stripe-payments/functions/__tests__/run-script.ts index f311ff0a..408efc83 100644 --- a/firestore-stripe-payments/functions/__tests__/run-script.ts +++ b/firestore-stripe-payments/functions/__tests__/run-script.ts @@ -7,9 +7,12 @@ import { } from './helpers/setupProxy'; (async () => { + console.log('Starting tests...'); + /** Clear all webhooks with ngrok.io, * useful for clearing any failed ci testing */ + console.log('Clearing webhooks...'); await cleanupAllWebhooks(); const proxyId = await setupProxy(); diff --git a/firestore-stripe-payments/functions/__tests__/tests/webhookevents/handleWebhookEvents.test.ts b/firestore-stripe-payments/functions/__tests__/tests/webhookevents/handleWebhookEvents.test.ts index 9ed07117..5f5bfc53 100644 --- a/firestore-stripe-payments/functions/__tests__/tests/webhookevents/handleWebhookEvents.test.ts +++ b/firestore-stripe-payments/functions/__tests__/tests/webhookevents/handleWebhookEvents.test.ts @@ -25,6 +25,7 @@ describe('webhook events', () => { product = await createRandomProduct(); }); test('successfully creates a new product', async () => { + console.log('Checking product >>>>>>', product.name); const collection = firestore.collection('products'); const productDoc: DocumentData = await waitForDocumentToExistInCollection( collection, @@ -33,7 +34,7 @@ describe('webhook events', () => { ); expect(productDoc.doc.data().name).toBe(product.name); - }); + }, 300000); test('successfully updates an existing product', async () => { const updatedProduct: Product = await updateProduct(product.id, { @@ -49,6 +50,6 @@ describe('webhook events', () => { ); expect(updated.data().name).toBe(updatedProduct.name); - }); + }, 300000); }); }); diff --git a/firestore-stripe-payments/functions/src/index.ts b/firestore-stripe-payments/functions/src/index.ts index 44791c54..ff0609ef 100644 --- a/firestore-stripe-payments/functions/src/index.ts +++ b/firestore-stripe-payments/functions/src/index.ts @@ -393,6 +393,7 @@ const prefixMetadata = (metadata: object) => * Create a Product record in Firestore based on a Stripe Product object. */ const createProductRecord = async (product: Stripe.Product): Promise => { + console.log('Creating product record >>>>> ', product.id, product.name); const { firebaseRole, ...rawMetadata } = product.metadata; const productData: Product = { @@ -405,6 +406,14 @@ const createProductRecord = async (product: Stripe.Product): Promise => { tax_code: product.tax_code ?? null, ...prefixMetadata(rawMetadata), }; + + console.log( + 'Setting product record >>>>> ', + product.id, + product.name, + config.productsCollectionPath + ); + await admin .firestore() .collection(config.productsCollectionPath)