|
| 1 | +import { parseArgs } from "node:util"; |
| 2 | + |
| 3 | +import { DeletePartitionCommand } from "dynamodb-toolbox/table/actions/deletePartition"; |
| 4 | +import { saleorApp } from "saleor-app"; |
| 5 | + |
| 6 | +import { env } from "@/env"; |
| 7 | +import { |
| 8 | + createLogsDocumentClient, |
| 9 | + createLogsDynamoClient, |
| 10 | +} from "@/modules/client-logs/dynamo-client"; |
| 11 | +import { ClientLogDynamoEntityFactory, LogsTable } from "@/modules/client-logs/dynamo-schema"; |
| 12 | + |
| 13 | +const { values } = parseArgs({ |
| 14 | + options: { |
| 15 | + "dry-run": { type: "boolean", short: "d" }, |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +const startDate = new Date(2024, 1, 1); |
| 20 | +const endDate = new Date(2025, 2, 24); // 14 days from today |
| 21 | + |
| 22 | +const main = async () => { |
| 23 | + const dynamoClient = createLogsDynamoClient(); |
| 24 | + |
| 25 | + const logsTable = LogsTable.create({ |
| 26 | + documentClient: createLogsDocumentClient(dynamoClient), |
| 27 | + tableName: env.DYNAMODB_LOGS_TABLE_NAME, |
| 28 | + }); |
| 29 | + |
| 30 | + const logsByCheckoutOrOrderId = |
| 31 | + ClientLogDynamoEntityFactory.createLogByCheckoutOrOrderId(logsTable); |
| 32 | + const logsByDateEntity = ClientLogDynamoEntityFactory.createLogByDate(logsTable); |
| 33 | + |
| 34 | + const appInstallations = await saleorApp.apl.getAll().catch(() => { |
| 35 | + console.error("Could not fetch instances from the APL"); |
| 36 | + |
| 37 | + process.exit(1); |
| 38 | + }); |
| 39 | + |
| 40 | + for (const { saleorApiUrl, appId } of appInstallations) { |
| 41 | + const command = logsTable |
| 42 | + .build(DeletePartitionCommand) |
| 43 | + .entities(logsByCheckoutOrOrderId, logsByDateEntity) |
| 44 | + .query({ |
| 45 | + partition: LogsTable.getPrimaryKey({ saleorApiUrl, appId }), |
| 46 | + range: { |
| 47 | + between: [startDate.toISOString(), endDate.toISOString()], |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + try { |
| 52 | + if (values["dry-run"]) { |
| 53 | + console.log(`Would delete logs for ${saleorApiUrl}#${appId}`); |
| 54 | + continue; |
| 55 | + } |
| 56 | + console.log(`Deleting logs for ${saleorApiUrl}#${appId}`); |
| 57 | + await command.send(); |
| 58 | + console.log(`Logs for ${saleorApiUrl}#${appId} deleted`); |
| 59 | + } catch (error) { |
| 60 | + console.error(`Could not delete logs for ${saleorApiUrl}#${appId}`, error); |
| 61 | + } |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +main(); |
0 commit comments