Skip to content

Commit

Permalink
chore(repo): Cleanup orgs during test cleanup (#5033)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacek Radko <[email protected]>
  • Loading branch information
brkalow and jacekradko authored Jan 29, 2025
1 parent fc43915 commit 5214d06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .changeset/angry-crabs-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
34 changes: 26 additions & 8 deletions integration/cleanup/cleanup.setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { User } from '@clerk/backend';
import { createClerkClient } from '@clerk/backend';
import { test as setup } from '@playwright/test';

Expand All @@ -24,8 +23,16 @@ setup('cleanup instances ', async () => {
limit: 150,
});

const batches = batchElements(skipUsersThatWereCreatedWithinTheLast10Minutes(users), 5);
for (const batch of batches) {
const { data: orgs } = await clerkClient.organizations
.getOrganizationList({
limit: 150,
})
.catch(() => ({ data: [] }));

const usersToDelete = batchElements(skipObjectsThatWereCreatedWithinTheLast10Minutes(users), 5);
const orgsToDelete = batchElements(skipObjectsThatWereCreatedWithinTheLast10Minutes(orgs), 5);

for (const batch of usersToDelete) {
console.log(`Starting batch...`);
await Promise.all(
batch.map(user => {
Expand All @@ -39,18 +46,29 @@ setup('cleanup instances ', async () => {
);
await new Promise(r => setTimeout(r, 1000));
}

for (const batch of orgsToDelete) {
console.log(`Starting batch...`);
await Promise.all(
batch.map(org => {
console.log(`Cleaning up org ${org.id} (${org.name}) (${new Date(org.createdAt).toISOString()})`);
return clerkClient.organizations.deleteOrganization(org.id);
}),
);
await new Promise(r => setTimeout(r, 1000));
}
}
});

const skipUsersThatWereCreatedWithinTheLast10Minutes = (users: User[]): User[] => {
const skipObjectsThatWereCreatedWithinTheLast10Minutes = <T extends { createdAt: string }>(objects: T[]): T[] => {
const tenMinutesAgo = new Date(Date.now() - 10 * 60 * 1000);
return users.filter(user => new Date(user.createdAt) < tenMinutesAgo);
return objects.filter(object => new Date(object.createdAt) < tenMinutesAgo);
};

function batchElements<T>(users: T[], batchSize = 5): T[][] {
function batchElements<T>(objects: T[], batchSize = 5): T[][] {
const batches = [];
for (let i = 0; i < users.length; i += batchSize) {
batches.push(users.slice(i, i + batchSize));
for (let i = 0; i < objects.length; i += batchSize) {
batches.push(objects.slice(i, i + batchSize));
}
return batches;
}

0 comments on commit 5214d06

Please sign in to comment.