1- import db from ". ./db.js" ;
2- import logger from ".. /logger.js" ; // Assuming a pino logger exists
1+ import { pool } from "./db.js" ;
2+ import logger from "./logger.js" ; // Assuming a pino logger exists
33
44/**
55 * Archives payment intents from the 'payments' table that are older than 90 days.
@@ -12,50 +12,66 @@ export async function archiveOldPaymentIntents() {
1212 ninetyDaysAgo . setDate ( ninetyDaysAgo . getDate ( ) - 90 ) ;
1313
1414 let archivedCount = 0 ;
15+ const client = await pool . connect ( ) ;
1516
1617 try {
17- await db . transaction ( async ( trx ) => {
18- // 1. Select old payments
19- const oldPayments = await trx ( "payments" )
20- . where ( "created_at" , "<" , ninetyDaysAgo )
21- . select ( "*" ) ;
22-
23- if ( oldPayments . length === 0 ) {
24- return ; // Nothing to archive
25- }
18+ await client . query ( "BEGIN" ) ;
19+
20+ // 1. Select old payments
21+ const { rows : oldPayments } = await client . query (
22+ "SELECT * FROM payments WHERE created_at < $1" ,
23+ [ ninetyDaysAgo ]
24+ ) ;
2625
27- // 2. Insert into archived_payments
28- // We map the records to Ensure archived_at gets set by default (or explicitly if needed)
29- const recordsToInsert = oldPayments . map ( p => {
30- // We clone the object to avoid modifying the original
31- const record = { ...p } ;
32- // Clean up fields that are not in the archived schema (none right now, but good practice)
33- return record ;
34- } ) ;
26+ if ( oldPayments . length === 0 ) {
27+ await client . query ( "ROLLBACK" ) ;
28+ client . release ( ) ;
29+ return { archivedCount : 0 } ;
30+ }
3531
36- await trx ( "archived_payments" ) . insert ( recordsToInsert ) ;
32+ // 2. Insert into archived_payments using bulk copy
33+ // We strictly use INSERT INTO ... SELECT
34+ await client . query (
35+ `INSERT INTO archived_payments (
36+ id, merchant_id, amount, asset, asset_issuer, recipient, description,
37+ memo, memo_type, webhook_url, status, tx_id, metadata,
38+ completion_duration_seconds, created_at, updated_at, deleted_at
39+ )
40+ SELECT
41+ id, merchant_id, amount, asset, asset_issuer, recipient, description,
42+ memo, memo_type, webhook_url, status, tx_id, metadata,
43+ completion_duration_seconds, created_at, updated_at, deleted_at
44+ FROM payments
45+ WHERE created_at < $1` ,
46+ [ ninetyDaysAgo ]
47+ ) ;
3748
38- // 3. Delete from payments
39- const deletedCount = await trx ( "payments" )
40- . whereIn ( "id" , oldPayments . map ( p => p . id ) )
41- . delete ( ) ;
49+ // 3. Delete from payments
50+ const { rowCount : deletedCount } = await client . query (
51+ "DELETE FROM payments WHERE created_at < $1" ,
52+ [ ninetyDaysAgo ]
53+ ) ;
4254
43- archivedCount = deletedCount ;
44- } ) ;
55+ archivedCount = deletedCount ;
56+
57+ await client . query ( "COMMIT" ) ;
4558
4659 if ( archivedCount > 0 ) {
4760 if ( logger && typeof logger . info === 'function' ) {
4861 logger . info ( { archivedCount } , "Successfully archived old payments" ) ;
4962 }
5063 }
51-
52- return { archivedCount } ;
5364 } catch ( error ) {
65+ await client . query ( "ROLLBACK" ) ;
5466 if ( logger && typeof logger . error === 'function' ) {
5567 logger . error ( { error } , "Failed to archive old payments" ) ;
5668 } else {
5769 console . error ( "Failed to archive old payments:" , error ) ;
5870 }
5971 throw error ;
72+ } finally {
73+ client . release ( ) ;
6074 }
75+
76+ return { archivedCount } ;
6177}
0 commit comments