@@ -14,6 +14,7 @@ import (
1414 "github.com/raystack/frontier/pkg/utils"
1515
1616 "github.com/doug-martin/goqu/v9"
17+ "github.com/doug-martin/goqu/v9/exp"
1718 "github.com/jmoiron/sqlx/types"
1819 "github.com/raystack/frontier/billing/customer"
1920 "github.com/raystack/frontier/pkg/auditrecord"
@@ -497,16 +498,40 @@ func (r BillingCustomerRepository) UpdateDetailsByID(ctx context.Context, custom
497498}
498499
499500func (r BillingCustomerRepository ) Delete (ctx context.Context , id string ) error {
500- query , params , err := dialect .Delete (TABLE_BILLING_CUSTOMERS ).
501+ lockQuery , lockParams , err := fromLive (TABLE_BILLING_CUSTOMERS ).
502+ Select ("id" ).
503+ Where (goqu.Ex {"id" : id }).
504+ ForUpdate (exp .Wait ).
505+ ToSQL ()
506+ if err != nil {
507+ return fmt .Errorf ("%w: %w" , errParse , err )
508+ }
509+ query , params , err := softDelete (TABLE_BILLING_CUSTOMERS ).
501510 Where (goqu.Ex {"id" : id }).
502511 Returning (& Customer {}).ToSQL ()
503512 if err != nil {
504513 return fmt .Errorf ("%w: %w" , errParse , err )
505514 }
515+ liveRecordsQuery , liveRecordsParams , err := liveBillingRecordsQuery (id )
516+ if err != nil {
517+ return fmt .Errorf ("%w: %w" , errParse , err )
518+ }
506519
507520 var customerModel Customer
508521 if err = r .dbc .WithTxn (ctx , sql.TxOptions {}, func (tx * sqlx.Tx ) error {
509522 return r .dbc .WithTimeout (ctx , TABLE_BILLING_CUSTOMERS , "Delete" , func (ctx context.Context ) error {
523+ var lockedID string
524+ if err := tx .QueryRowxContext (ctx , lockQuery , lockParams ... ).Scan (& lockedID ); err != nil {
525+ return err
526+ }
527+
528+ var liveRecords bool
529+ if err := tx .QueryRowxContext (ctx , liveRecordsQuery , liveRecordsParams ... ).Scan (& liveRecords ); err != nil {
530+ return err
531+ }
532+ if liveRecords {
533+ return customer .ErrHasLiveRecords
534+ }
510535 if err := tx .QueryRowxContext (ctx , query , params ... ).StructScan (& customerModel ); err != nil {
511536 if errors .Is (err , sql .ErrNoRows ) {
512537 return customer .ErrNotFound
@@ -542,12 +567,22 @@ func (r BillingCustomerRepository) Delete(ctx context.Context, id string) error
542567 }); err != nil {
543568 err = checkPostgresError (err )
544569 switch {
545- case errors .Is (err , sql .ErrNoRows ):
570+ case errors .Is (err , customer . ErrNotFound ), errors . Is ( err , sql .ErrNoRows ):
546571 return customer .ErrNotFound
572+ case errors .Is (err , customer .ErrHasLiveRecords ):
573+ return customer .ErrHasLiveRecords
547574 default :
548575 return fmt .Errorf ("%w: %w" , errTxn , err )
549576 }
550577 }
551578
552579 return nil
553580}
581+
582+ func liveBillingRecordsQuery (customerID string ) (string , []any , error ) {
583+ return dialect .Select (goqu .Or (
584+ goqu .L ("EXISTS (SELECT 1 FROM billing_subscriptions WHERE customer_id = ? AND deleted_at IS NULL)" , customerID ),
585+ goqu .L ("EXISTS (SELECT 1 FROM billing_invoices WHERE customer_id = ? AND deleted_at IS NULL)" , customerID ),
586+ goqu .L ("EXISTS (SELECT 1 FROM billing_checkouts WHERE customer_id = ? AND deleted_at IS NULL)" , customerID ),
587+ )).ToSQL ()
588+ }
0 commit comments