fix: archive / delete on product row toolbar
This commit is contained in:
@@ -6,7 +6,50 @@ import {
|
||||
Organization,
|
||||
products,
|
||||
} from "@autumn/shared";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
|
||||
const clearCustomersInBatches = async ({
|
||||
db,
|
||||
org,
|
||||
batchSize = 450,
|
||||
}: {
|
||||
db: DrizzleCli;
|
||||
org: Organization;
|
||||
batchSize?: number;
|
||||
}) => {
|
||||
let deletedCount = 0;
|
||||
|
||||
while (true) {
|
||||
// Get a batch of customer IDs to delete
|
||||
const customerBatch = await db
|
||||
.select({ internalId: customers.internal_id })
|
||||
.from(customers)
|
||||
.where(
|
||||
and(eq(customers.org_id, org.id), eq(customers.env, AppEnv.Sandbox))
|
||||
)
|
||||
.limit(batchSize);
|
||||
|
||||
if (customerBatch.length === 0) {
|
||||
break; // No more customers to delete
|
||||
}
|
||||
|
||||
// Delete the batch
|
||||
const customerIds = customerBatch
|
||||
.map((c) => c.internalId)
|
||||
.filter((id) => id !== null);
|
||||
|
||||
await db
|
||||
.delete(customers)
|
||||
.where(inArray(customers.internal_id, customerIds));
|
||||
|
||||
deletedCount += customerBatch.length;
|
||||
console.log(
|
||||
`Deleted ${customerBatch.length} customers (total: ${deletedCount})`
|
||||
);
|
||||
}
|
||||
|
||||
return deletedCount;
|
||||
};
|
||||
|
||||
export const clearOrg = async ({
|
||||
db,
|
||||
@@ -15,13 +58,8 @@ export const clearOrg = async ({
|
||||
db: DrizzleCli;
|
||||
org: Organization;
|
||||
}) => {
|
||||
await db
|
||||
.delete(customers)
|
||||
.where(
|
||||
and(eq(customers.org_id, org.id), eq(customers.env, AppEnv.Sandbox)),
|
||||
);
|
||||
|
||||
console.log("Cleared customers");
|
||||
const deletedCount = await clearCustomersInBatches({ db, org });
|
||||
console.log(`Cleared ${deletedCount} customers`);
|
||||
|
||||
await db
|
||||
.delete(products)
|
||||
|
||||
Reference in New Issue
Block a user