Skip to content

Commit

Permalink
Update Service.DeleteOrderCreds to support multiple items
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrm committed Oct 20, 2023
1 parent 48d76c5 commit f1bb40a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
5 changes: 3 additions & 2 deletions services/skus/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,13 @@ func DeleteOrderCreds(service *Service) handlers.AppHandler {
)
}

if err := service.validateOrderMerchantAndCaveats(ctx, *orderID.UUID()); err != nil {
id := *orderID.UUID()
if err := service.validateOrderMerchantAndCaveats(ctx, id); err != nil {
return handlers.WrapError(err, "Error validating auth merchant and caveats", http.StatusForbidden)
}

isSigned := r.URL.Query().Get("isSigned") == "true"
if err := service.DeleteOrderCreds(ctx, *orderID.UUID(), isSigned); err != nil {
if err := service.DeleteOrderCreds(ctx, id, isSigned); err != nil {
return handlers.WrapError(err, "Error deleting credentials", http.StatusBadRequest)
}

Expand Down
67 changes: 43 additions & 24 deletions services/skus/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const (
)

var (
ErrOrderUnpaid = errors.New("order not paid")
ErrOrderHasNoItems = errors.New("order has no items")
ErrOrderUnpaid = errors.New("order not paid")
ErrOrderHasNoItems model.Error = "order has no items"

errInvalidIssuerResp model.Error = "invalid issuer response"

Expand Down Expand Up @@ -600,50 +600,69 @@ func (s *SigningOrderResultErrorHandler) Handle(ctx context.Context, message kaf
return nil
}

// DeleteOrderCreds performs a hard delete of all the order credentials associated with the given OrderID.
// This includes both time limited v2 and single use credentials.
// The isSigned param only applies to single use and will always be false for time limited v2.
// DeleteOrderCreds hard-deletes all the order credentials associated with the given orderID.
//
// This includes both time-limited-v2 and single-use credentials.
// The isSigned param only applies to single use and will always be false for time-limited-v2.
// Credentials cannot be deleted when an order is in the process of being signed.
func (s *Service) DeleteOrderCreds(ctx context.Context, orderID uuid.UUID, isSigned bool) error {
order, err := s.Datastore.GetOrder(orderID)
if err != nil {
return err
}

if len(order.Items) == 0 {
nitems := len(order.Items)
if nitems == 0 {
return ErrOrderHasNoItems
}

if order.Items[0].CredentialType == timeLimited {
// Exit early in this special case.
if nitems == 1 && order.Items[0].CredentialType == timeLimited {
return nil
}

ctx, tx, rollback, commit, err := datastore.GetTx(ctx, s.Datastore)
tx, err := s.Datastore.RawDB().BeginTxx(ctx, nil)
if err != nil {
return fmt.Errorf("error retrieveing txn delete order cred")
return err
}
defer rollback()
defer s.Datastore.RollbackTx(tx)

switch order.Items[0].CredentialType {
case singleUse:
err = s.Datastore.DeleteSingleUseOrderCredsByOrderTx(ctx, tx, orderID, isSigned)
if err != nil {
return fmt.Errorf("error deleting single use order creds: %w", err)
}
case timeLimitedV2:
err = s.Datastore.DeleteTimeLimitedV2OrderCredsByOrderTx(ctx, tx, orderID)
if err != nil {
return fmt.Errorf("error deleting time limited v2 order creds: %w", err)
var didSingleUse, didTlv2 bool

// TODO(pavelb):
// - create repos for credentials;
// - move the corresponding methods there;
// - make those methods work on per-item basis.
for i := range order.Items {
item := order.Items[i]

switch item.CredentialType {
case timeLimited:
continue
case singleUse:
if !didSingleUse {
if err := s.Datastore.DeleteSingleUseOrderCredsByOrderTx(ctx, tx, orderID, isSigned); err != nil {
return fmt.Errorf("error deleting single use order creds: %w", err)
}
}

didSingleUse = true
case timeLimitedV2:
if !didTlv2 {
if err := s.Datastore.DeleteTimeLimitedV2OrderCredsByOrderTx(ctx, tx, orderID); err != nil {
return fmt.Errorf("error deleting time limited v2 order creds: %w", err)
}
}

didTlv2 = true
}
}

err = s.Datastore.DeleteSigningOrderRequestOutboxByOrderTx(ctx, tx, orderID)
if err != nil {
if err := s.Datastore.DeleteSigningOrderRequestOutboxByOrderTx(ctx, tx, orderID); err != nil {
return fmt.Errorf("error deleting order creds signing in progress")
}

err = commit()
if err != nil {
if err := tx.Commit(); err != nil {
return fmt.Errorf("error commiting delete order creds: %w", err)
}

Expand Down
9 changes: 3 additions & 6 deletions services/skus/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,11 @@ func getEmailFromCheckoutSession(stripeSession *stripe.CheckoutSession) string {
return email
}

// RenewOrder updates the orders status to paid and paid at time, inserts record of this order
// RenewOrder updates the order status to paid and records payment history.
//
// Status should either be one of pending, paid, fulfilled, or canceled.
func (s *Service) RenewOrder(ctx context.Context, orderID uuid.UUID) error {

// renew order is an update order with paid status
// and an update order expires at with the new expiry time of the order
err := s.Datastore.UpdateOrder(orderID, OrderStatusPaid) // this performs a record order payment
if err != nil {
if err := s.Datastore.UpdateOrder(orderID, OrderStatusPaid); err != nil {
return fmt.Errorf("failed to set order status to paid: %w", err)
}

Expand Down

0 comments on commit f1bb40a

Please sign in to comment.