Skip to content

Commit ec67a85

Browse files
committed
Update export_cvs.js
1 parent e6a3700 commit ec67a85

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

backend/scripts/export_cvs.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,31 @@ function decrypt(iv, encrypted, authTag) {
2323
return decrypted;
2424
}
2525

26-
async function exportEmails() {
26+
async function exportAndDeleteEmails() {
2727
const { rows } = await pool.query(
2828
"SELECT iv, encrypted, auth_tag FROM beta_emails"
2929
);
3030

31-
const emails = rows
32-
.map((row) => decrypt(row.iv, row.encrypted, row.auth_tag))
33-
.filter(Boolean);
31+
const emails = [];
32+
const validRows = [];
33+
rows.forEach((row) => {
34+
const email = decrypt(row.iv, row.encrypted, row.auth_tag);
35+
if (email) {
36+
emails.push(email);
37+
validRows.push(row);
38+
}
39+
});
3440

3541
fs.writeFileSync("emails.csv", emails.join("\n"), "utf8");
3642
console.log(`Exported ${emails.length} emails`);
43+
44+
for (const row of validRows) {
45+
await pool.query(
46+
"DELETE FROM beta_emails WHERE iv=$1 AND encrypted=$2 AND auth_tag=$3",
47+
[row.iv, row.encrypted, row.auth_tag]
48+
);
49+
}
50+
console.log(`Deleted ${validRows.length} rows from database`);
3751
}
3852

39-
exportEmails().catch(console.error);
53+
exportAndDeleteEmails().catch(console.error);

0 commit comments

Comments
 (0)