Skip to content

Commit

Permalink
chore: run webhook cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dackers86 committed Aug 30, 2023
1 parent 2f91d8d commit 7c22f09
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
40 changes: 27 additions & 13 deletions firestore-stripe-payments/functions/__tests__/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const waitForDocumentUpdate = (
document: DocumentData,
field: string | number,
value: any,
timeout: number = 10_000
timeout: number = 300000 // 5 minutes
): Promise<FirebaseFirestore.DocumentData> => {
return new Promise((resolve, reject) => {
let timedOut = false;
Expand All @@ -78,34 +78,48 @@ 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<DocumentData> => {
return new Promise((resolve, reject) => {
let timedOut = false;

let unsubscribe: () => void; // Declare unsubscribe here

const timer = setTimeout(() => {
timedOut = true;
reject(
new Error(
`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);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions firestore-stripe-payments/functions/__tests__/run-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7c22f09

Please sign in to comment.