Skip to content

Commit f20e38e

Browse files
committed
feat: add transaction_id to refund type and update related database operations
1 parent 2ec1725 commit f20e38e

File tree

1 file changed

+87
-44
lines changed

1 file changed

+87
-44
lines changed

cloudflare-worker/src/db/d1-db.ts

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type d1_refund = {
8484
date: string; // Date of the refund map to `date`
8585
refund_date: string; // Refund date map to `refundDate`
8686
amount: number; // Amount of the refund map to `amount`
87+
transaction_id?: string; // Transaction ID of the refund map to `transactionId`
8788
};
8889
type d1_internal = {
8990
d1_testers: d1_tester[];
@@ -1242,6 +1243,7 @@ export class CloudflareD1DB implements FeedbackFlowDB {
12421243
description: row.description as string,
12431244
amount: row.amount as number,
12441245
screenshot: row.screenshot as string,
1246+
screenshotSummary: row.screenshot_summary as string,
12451247
refunded: Boolean(row.refunded),
12461248
}));
12471249
const feedbacks: Feedback[] = db_feedbacks.map((row) => ({
@@ -1259,6 +1261,7 @@ export class CloudflareD1DB implements FeedbackFlowDB {
12591261
date: row.date as string,
12601262
refundDate: row.refund_date as string,
12611263
amount: row.amount as number,
1264+
transactionId: row.transaction_id as string,
12621265
}));
12631266
// Create a backup object
12641267
const backup = {
@@ -1312,64 +1315,104 @@ export class CloudflareD1DB implements FeedbackFlowDB {
13121315
}
13131316
const { testers, ids, purchases, feedbacks, publications, refunds } =
13141317
backup;
1315-
const dbCleanupStatements = [
1316-
"DELETE FROM testers",
1317-
"DELETE FROM id_mappings",
1318-
"DELETE FROM purchases",
1319-
"DELETE FROM feedbacks",
1320-
"DELETE FROM publications",
1321-
"DELETE FROM refunds",
1318+
const dbCleanupStatements:D1PreparedStatement[] = [
1319+
this.db.prepare("DELETE FROM testers"),
1320+
this.db.prepare("DELETE FROM id_mappings"),
1321+
this.db.prepare("DELETE FROM purchases"),
1322+
this.db.prepare("DELETE FROM feedbacks"),
1323+
this.db.prepare("DELETE FROM publications"),
1324+
this.db.prepare("DELETE FROM refunds"),
13221325
];
13231326

13241327
// Insert the data back into the database
1328+
const dbTestersInsertStatement:D1PreparedStatement[] = [];
1329+
testers.forEach((tester: Tester) => {
1330+
dbTestersInsertStatement.push(
1331+
this.db.prepare("INSERT INTO testers (uuid, name) VALUES (?, ?)").bind(tester.uuid, tester.name),
1332+
);
1333+
tester.ids.forEach((id) => {
1334+
dbTestersInsertStatement.push(
1335+
this.db.prepare(
1336+
"INSERT INTO id_mappings (id, tester_uuid) VALUES (?, ?)").bind(id, tester.uuid)
1337+
);
1338+
});
1339+
});
1340+
const dbPurchasesStatement:D1PreparedStatement[] = [];
1341+
purchases.forEach((purchase: Purchase) => {
1342+
dbPurchasesStatement.push(
1343+
this.db.prepare(
1344+
`INSERT INTO purchases (id, tester_uuid, date, order_number, description, amount, screenshot, screenshot_summary, refunded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1345+
).bind(
1346+
purchase.id,
1347+
purchase.testerUuid,
1348+
purchase.date,
1349+
purchase.order,
1350+
purchase.description,
1351+
purchase.amount,
1352+
purchase.screenshot,
1353+
purchase.screenshotSummary || null,
1354+
purchase.refunded ? 1 : 0,
1355+
),
1356+
);
1357+
});
1358+
const dbFeedbacksStatement:D1PreparedStatement[] = [];
1359+
feedbacks.forEach((feedback: Feedback) => {
1360+
dbFeedbacksStatement.push(
1361+
this.db.prepare(
1362+
`INSERT INTO feedbacks (purchase_id, date, feedback) VALUES (?, ?, ?)`,
1363+
).bind(feedback.purchase, feedback.date, feedback.feedback),
1364+
);
1365+
});
1366+
const dbPublicationsStatement:D1PreparedStatement[] = [];
1367+
publications.forEach((publication: Publication) => {
1368+
dbPublicationsStatement.push(
1369+
this.db.prepare(
1370+
`INSERT INTO publications (purchase_id, date, screenshot) VALUES (?, ?, ?)`,
1371+
).bind(publication.purchase, publication.date, publication.screenshot),
1372+
);
1373+
});
1374+
1375+
const dbRefundsStatement:D1PreparedStatement[]= [];
1376+
refunds.forEach((refund: Refund) => {
1377+
dbRefundsStatement.push(
1378+
this.db.prepare(
1379+
`INSERT INTO refunds (purchase_id, date, refund_date, amount, transaction_id) VALUES (?, ?, ?, ?, ?)`,
1380+
).bind(
1381+
refund.purchase,
1382+
refund.date,
1383+
refund.refundDate,
1384+
refund.amount,
1385+
refund.transactionId || null,
1386+
),
1387+
);
1388+
});
13251389
const dbInsertStatements = [
1326-
`INSERT INTO testers (uuid, name) VALUES ${testers
1327-
.map((tester: Tester) => `('${tester.uuid}', '${tester.name}')`)
1328-
.join(", ")}`,
1329-
`INSERT INTO id_mappings (id, tester_uuid) VALUES ${ids
1330-
.map((id: IdMapping) => `('${id.id}', '${id.testerUuid}')`)
1331-
.join(", ")}`,
1332-
`INSERT INTO purchases (id, tester_uuid, date, order_number, description, amount, screenshot, refunded) VALUES ${purchases
1333-
.map(
1334-
(purchase: Purchase) =>
1335-
`('${purchase.id}', '${purchase.testerUuid}', '${purchase.date}', '${purchase.order}', '${purchase.description}', ${purchase.amount}, '${purchase.screenshot}', ${purchase.refunded ? 1 : 0
1336-
})`,
1337-
)
1338-
.join(", ")}`,
1339-
`INSERT INTO feedbacks (purchase_id, date, feedback) VALUES ${feedbacks
1340-
.map(
1341-
(feedback: Feedback) =>
1342-
`('${feedback.purchase}', '${feedback.date}', '${feedback.feedback}')`,
1343-
)
1344-
.join(", ")}`,
1345-
`INSERT INTO publications (purchase_id, date, screenshot) VALUES ${publications
1346-
.map(
1347-
(publication: Publication) =>
1348-
`('${publication.purchase}', '${publication.date}', '${publication.screenshot}')`,
1349-
)
1350-
.join(", ")}`,
1351-
`INSERT INTO refunds (purchase_id, date, refund_date, amount) VALUES ${refunds
1352-
.map(
1353-
(refund: Refund) =>
1354-
`('${refund.purchase}', '${refund.date}', '${refund.refundDate}', ${refund.amount})`,
1355-
)
1356-
.join(", ")}`,
1357-
];
1390+
dbTestersInsertStatement,
1391+
dbPurchasesStatement,
1392+
dbFeedbacksStatement,
1393+
dbPublicationsStatement,
1394+
dbRefundsStatement,
1395+
].flat();
13581396
// Execute global statements
13591397
const globalStatements = [
13601398
dbCleanupStatements,
1361-
...dbInsertStatements,
1399+
dbInsertStatements,
13621400
].flat();
1363-
const preparedStatements = globalStatements.map((stmt) =>
1364-
this.db.prepare(stmt),
1365-
);
13661401

13671402
try {
13681403
console.log("Executing batch insert...");
1404+
console.log(globalStatements);
13691405
const result = await this.db.batch(
1370-
preparedStatements.map((stmt) => stmt.bind()),
1406+
globalStatements
13711407
);
13721408

1409+
// Code for executing each statement in the batch useful for debugging
1410+
// const result:D1Result[] = [];
1411+
// for (const statement of globalStatements) {
1412+
// console.log("Executing statement:", statement);
1413+
// const res = await statement.run();
1414+
// result.push(res);
1415+
// }
13731416
return {
13741417
success: result.map((r) => r.success).every((r) => r),
13751418
message: JSON.stringify(result),

0 commit comments

Comments
 (0)