forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.helper.ts
More file actions
56 lines (51 loc) · 1.71 KB
/
Copy pathcleanup.helper.ts
File metadata and controls
56 lines (51 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Logger } from "@nestjs/common";
export async function cleanupOldReportsHelper(
dataSource: any,
reportsRepository: any,
logger: Logger,
retentionDays = 30,
) {
const threshold = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000);
// Use raw query for date comparison to be efficient
const rows: any[] = await dataSource.query(
"SELECT id, file_path, file_name FROM analytics_reports WHERE status = $1 AND created_at < $2",
["completed", threshold.toISOString()],
);
for (const r of rows) {
try {
const id = r.id;
const filePath = r.file_path as string | null;
if (filePath) {
if (filePath.startsWith("s3://")) {
const [, bucket, ...rest] = filePath.split("/");
const key = rest.join("/");
const region = process.env.AWS_REGION;
const s3Client = new (require("@aws-sdk/client-s3").S3Client)({
region,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
const { DeleteObjectCommand } = require("@aws-sdk/client-s3");
await s3Client.send(
new DeleteObjectCommand({ Bucket: bucket, Key: key }),
);
} else {
try {
await require("fs").promises.unlink(filePath);
} catch (_) {
// ignore
}
}
}
await reportsRepository.update(
{ id },
{ status: "deleted", deletedAt: new Date(), filePath: null },
);
logger.debug(`Deleted old report ${id}`);
} catch (err) {
logger.error(`Failed to delete old report ${r.id}`, err as any);
}
}
}