Files
payments/internal/api/services/payment_initiations_delete.go

38 lines
1.1 KiB
Go

package services
import (
"context"
"fmt"
"github.com/hanzo-fi/go-libs/v5/pkg/storage/bun/paginate"
"github.com/hanzo-fi/payments/pkg/domain/models"
"github.com/hanzo-fi/payments/internal/storage"
"github.com/pkg/errors"
)
func (s *Service) PaymentInitiationsDelete(ctx context.Context, id models.PaymentInitiationID) error {
cursor, err := s.storage.PaymentInitiationAdjustmentsList(
ctx,
id,
storage.NewListPaymentInitiationAdjustmentsQuery(
paginate.NewPaginatedQueryOptions(storage.PaymentInitiationAdjustmentsQuery{}).
WithPageSize(1),
),
)
if err != nil {
return newStorageError(err, "cannot list payment initiation adjustments")
}
if len(cursor.Data) == 0 {
return errors.New("payment initiation adjustments not found")
}
lastAdjustment := cursor.Data[0]
if lastAdjustment.Status != models.PAYMENT_INITIATION_ADJUSTMENT_STATUS_WAITING_FOR_VALIDATION {
return fmt.Errorf("cannot delete an already approved payment initiation: %w", ErrValidation)
}
return newStorageError(s.storage.PaymentInitiationsDelete(ctx, id), "cannot delete payment initiation")
}