Skip to content

Commit

Permalink
refactor: delete no longer needed code
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrm committed Dec 19, 2024
1 parent bfe46a2 commit a7c4680
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 645 deletions.
31 changes: 3 additions & 28 deletions services/skus/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Router(
r.Method(
http.MethodDelete,
"/{orderID}",
metricsMwr("CancelOrder", NewCORSMwr(copts, http.MethodDelete)(authMwr(CancelOrder(svc)))),
metricsMwr("CancelOrder", NewCORSMwr(copts, http.MethodDelete)(authMwr(handleCancelOrderLegacy(svc)))),
)

r.Method(
Expand Down Expand Up @@ -340,30 +340,9 @@ func handleSetOrderTrialDays(svc *Service) handlers.AppHandler {
})
}

// CancelOrder handles requests for cancelling orders.
func CancelOrder(service *Service) handlers.AppHandler {
func handleCancelOrderLegacy(service *Service) handlers.AppHandler {
return handlers.AppHandler(func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
ctx := r.Context()
orderID := &inputs.ID{}

if err := inputs.DecodeAndValidateString(ctx, orderID, chi.URLParam(r, "orderID")); err != nil {
return handlers.ValidationError(
"Error validating request url parameter",
map[string]interface{}{"orderID": err.Error()},
)
}

oid := *orderID.UUID()

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

if err := service.CancelOrderLegacy(oid); err != nil {
return handlers.WrapError(err, "Error retrieving the order", http.StatusInternalServerError)
}

return handlers.RenderContent(ctx, struct{}{}, w, http.StatusOK)
return handlers.WrapError(model.Error("not implemented"), "not implemented", http.StatusNotImplemented)
})
}

Expand Down Expand Up @@ -730,10 +709,6 @@ func deleteOrderCreds(service *Service) handlers.AppHandler {
return handlers.ValidationError("orderID", map[string]interface{}{"orderID": err.Error()})
}

if err := service.validateOrderMerchantAndCaveats(ctx, orderID); 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, isSigned); err != nil {
switch {
Expand Down
118 changes: 0 additions & 118 deletions services/skus/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ type Datastore interface {

// GetOrder by ID
GetOrder(orderID uuid.UUID) (*Order, error)
// GetOrderByExternalID by the external id from the purchase vendor
GetOrderByExternalID(externalID string) (*Order, error)
// UpdateOrder updates an order when it has been paid
UpdateOrder(orderID uuid.UUID, status string) error
// UpdateOrderMetadata adds a key value pair to an order's metadata
UpdateOrderMetadata(orderID uuid.UUID, key string, value string) error
// CreateTransaction creates a transaction
CreateTransaction(orderID uuid.UUID, externalTransactionID string, status string, currency string, kind string, amount decimal.Decimal) (*Transaction, error)
// UpdateTransaction creates a transaction
Expand All @@ -71,9 +67,6 @@ type Datastore interface {
CommitVote(ctx context.Context, vr VoteRecord, tx *sqlx.Tx) error
MarkVoteErrored(ctx context.Context, vr VoteRecord, tx *sqlx.Tx) error
InsertVote(ctx context.Context, vr VoteRecord) error
CheckExpiredCheckoutSession(uuid.UUID) (bool, string, error)
IsStripeSub(uuid.UUID) (bool, string, error)
GetOrderItem(ctx context.Context, itemID uuid.UUID) (*OrderItem, error)
InsertOrderCredsTx(ctx context.Context, tx *sqlx.Tx, creds *OrderCreds) error
GetOrderCreds(orderID uuid.UUID, isSigned bool) ([]OrderCreds, error)
SendSigningRequest(ctx context.Context, signingRequestWriter SigningRequestWriter) error
Expand All @@ -89,7 +82,6 @@ type Datastore interface {
GetSigningOrderRequestOutboxByOrderItem(ctx context.Context, orderID, itemID uuid.UUID) ([]SigningOrderRequestOutbox, error)
DeleteSigningOrderRequestOutboxByOrderTx(ctx context.Context, tx *sqlx.Tx, orderID uuid.UUID) error
UpdateSigningOrderRequestOutboxTx(ctx context.Context, tx *sqlx.Tx, requestID uuid.UUID, completedAt time.Time) error
AppendOrderMetadata(context.Context, *uuid.UUID, string, string) error
GetOutboxMovAvgDurationSeconds() (int64, error)
}

Expand Down Expand Up @@ -288,30 +280,6 @@ func (pg *Postgres) CreateOrder(ctx context.Context, dbi sqlx.ExtContext, oreq *
return result, nil
}

// GetOrderByExternalID returns an order by the external id from the purchase vendor.
func (pg *Postgres) GetOrderByExternalID(externalID string) (*Order, error) {
ctx := context.TODO()
dbi := pg.RawDB()

result, err := pg.orderRepo.GetByExternalID(ctx, dbi, externalID)
if err != nil {
// Preserve the legacy behaviour.
// TODO: Propagate the sentinel error, and handle in the business logic properly.
if errors.Is(err, model.ErrOrderNotFound) {
return nil, nil
}

return nil, err
}

result.Items, err = pg.orderItemRepo.FindByOrderID(ctx, dbi, result.ID)
if err != nil {
return nil, err
}

return result, nil
}

// GetOutboxMovAvgDurationSeconds - get the number of seconds it takes to clear the last 20 outbox messages
func (pg *Postgres) GetOutboxMovAvgDurationSeconds() (int64, error) {
statement := `
Expand Down Expand Up @@ -357,24 +325,6 @@ func (pg *Postgres) GetOrder(orderID uuid.UUID) (*Order, error) {
return result, nil
}

// GetOrderItem retrieves the order item for the given identifier.
//
// It returns sql.ErrNoRows if the item is not found.
func (pg *Postgres) GetOrderItem(ctx context.Context, itemID uuid.UUID) (*OrderItem, error) {
result, err := pg.orderItemRepo.Get(ctx, pg.RawDB(), itemID)
if err != nil {
// Preserve the legacy behaviour.
// TODO: Propagate the sentinel error, and handle in the business logic properly.
if errors.Is(err, model.ErrOrderItemNotFound) {
return nil, sql.ErrNoRows
}

return nil, err
}

return result, nil
}

// GetPagedMerchantTransactions - get a paginated list of transactions for a merchant
func (pg *Postgres) GetPagedMerchantTransactions(
ctx context.Context, merchantID uuid.UUID, pagination *inputs.Pagination) (*[]Transaction, int, error) {
Expand Down Expand Up @@ -476,50 +426,6 @@ func (pg *Postgres) GetTransaction(externalTransactionID string) (*Transaction,
return &transaction, nil
}

// CheckExpiredCheckoutSession indicates whether a Stripe checkout session is expired with its id for the given orderID.
//
// TODO(pavelb): The boolean return value is unnecessary, and can be removed.
// If there is experied session, the session id is present.
// If there is no session, or it has not expired, the result is the same – no session id.
// It's the caller's responsibility (the business logic layer) to interpret the result.
func (pg *Postgres) CheckExpiredCheckoutSession(orderID uuid.UUID) (bool, string, error) {
ctx := context.TODO()

sessID, err := pg.orderRepo.GetExpiredStripeCheckoutSessionID(ctx, pg.RawDB(), orderID)
if err != nil {
if errors.Is(err, model.ErrExpiredStripeCheckoutSessionIDNotFound) {
return false, "", nil
}

return false, "", fmt.Errorf("failed to check expired state of checkout session: %w", err)
}

if sessID == "" {
return false, "", nil
}

return true, sessID, nil
}

// IsStripeSub reports whether the order is associated with a stripe subscription, if true, subscription id is returned.
//
// TODO(pavelb): This is a piece of business logic that leaked to the storage layer.
// Also, it unsuccessfully mimics the Go comma, ok idiom – bool and string should be swapped.
// But that's not necessary.
// If metadata was found, but there was no stripeSubscriptionId, it's known not to be a Stripe order.
func (pg *Postgres) IsStripeSub(orderID uuid.UUID) (bool, string, error) {
ctx := context.TODO()

data, err := pg.orderRepo.GetMetadata(ctx, pg.RawDB(), orderID)
if err != nil {
return false, "", err
}

sid, ok := data["stripeSubscriptionId"].(string)

return ok, sid, nil
}

// UpdateOrder updates the orders status.
//
// Deprecated: This method MUST NOT be used for Premium orders.
Expand Down Expand Up @@ -890,15 +796,6 @@ func (pg *Postgres) InsertVote(ctx context.Context, vr VoteRecord) error {
return nil
}

// UpdateOrderMetadata sets the order's metadata to the key and value.
//
// Deprecated: This method is no longer used and should be deleted.
//
// TODO(pavelb): Remove this method as it's dangerous and must not be used.
func (pg *Postgres) UpdateOrderMetadata(orderID uuid.UUID, key string, value string) error {
return model.Error("UpdateOrderMetadata must not be used")
}

// TimeLimitedV2Creds represent all the
type TimeLimitedV2Creds struct {
OrderID uuid.UUID `json:"orderId"`
Expand Down Expand Up @@ -1292,21 +1189,6 @@ func (pg *Postgres) InsertSignedOrderCredentialsTx(ctx context.Context, tx *sqlx
return nil
}

// AppendOrderMetadata appends the key and string value to an order's metadata.
func (pg *Postgres) AppendOrderMetadata(ctx context.Context, orderID *uuid.UUID, key, value string) error {
_, tx, rollback, commit, err := datastore.GetTx(ctx, pg)
if err != nil {
return err
}
defer rollback()

if err := pg.orderRepo.AppendMetadata(ctx, tx, *orderID, key, value); err != nil {
return fmt.Errorf("error updating order metadata %s: %w", orderID, err)
}

return commit()
}

// recordOrderPayment records payments for Auto Contribute and Search Captcha.
//
// Deprecated: This method MUST NOT be used for Premium orders.
Expand Down
22 changes: 0 additions & 22 deletions services/skus/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,6 @@ func TestGetPagedMerchantTransactions(t *testing.T) {
}
}

func (suite *PostgresTestSuite) TestGetOrderByExternalID() {
ctx := context.Background()
defer ctx.Done()

// create an issuer and a paid order with one order item and a time limited v2 credential type.
ctx = context.WithValue(context.Background(), appctx.WhitelistSKUsCTXKey, []string{devFreeTimeLimitedV2})
o1, _ := createOrderAndIssuer(suite.T(), ctx, suite.storage, devFreeTimeLimitedV2)

// add the external id to metadata
err := suite.storage.AppendOrderMetadata(ctx, &o1.ID, "externalID", "my external id")
suite.Require().NoError(err)

// test out get by external id
o2, err := suite.storage.GetOrderByExternalID("my external id")
suite.Require().NoError(err)
suite.Assert().NotNil(o2)

if o2 != nil {
suite.Assert().Equal(o2.ID.String(), o1.ID.String())
}
}

func (suite *PostgresTestSuite) TestGetTimeLimitedV2OrderCredsByOrder_Success() {
env := os.Getenv("ENV")
ctx := context.WithValue(context.Background(), appctx.EnvironmentCTXKey, env)
Expand Down
84 changes: 0 additions & 84 deletions services/skus/instrumented_datastore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a7c4680

Please sign in to comment.