Skip to content

Commit 72bea0b

Browse files
committed
Check ownership before checking if already sent
1 parent a247782 commit 72bea0b

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

broker/patron_request/api/api-handler.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,16 +534,6 @@ func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *
534534
api.AddBadRequestError(ctx, w, fmt.Errorf("patron request id does not match"))
535535
return
536536
}
537-
_, err = a.illRepo.GetIllTransactionByRequesterRequestId(ctx, pgtype.Text{String: id, Valid: true})
538-
if err == nil {
539-
// request already sent
540-
api.AddBadRequestError(ctx, w, fmt.Errorf("request already sent"))
541-
return
542-
}
543-
if !errors.Is(err, pgx.ErrNoRows) {
544-
api.AddInternalError(ctx, w, err)
545-
return
546-
}
547537
existingPr, err := a.prRepo.GetPatronRequestById(ctx, id)
548538
if err != nil {
549539
handleDbError(w, ctx, err)
@@ -556,6 +546,16 @@ func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *
556546
api.AddBadRequestError(ctx, w, fmt.Errorf("only borrower-side patron requests can be updated"))
557547
return
558548
}
549+
_, err = a.illRepo.GetIllTransactionByRequesterRequestId(ctx, pgtype.Text{String: id, Valid: true})
550+
if err == nil {
551+
// request already sent
552+
api.AddBadRequestError(ctx, w, fmt.Errorf("request already sent"))
553+
return
554+
}
555+
if !errors.Is(err, pgx.ErrNoRows) {
556+
api.AddInternalError(ctx, w, err)
557+
return
558+
}
559559
newPr.RequesterSymbol = &symbol
560560
creationTime := time.Now()
561561
if existingPr.CreatedAt.Valid {

broker/patron_request/api/api-handler_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,6 @@ func TestPutPatronRequestsIdMissingId(t *testing.T) {
14831483

14841484
func TestPutPatronRequestsIdIdMismatch(t *testing.T) {
14851485
handler := NewPrApiHandler(new(PrRepoError), mockEventBus, mockEventRepo, tenant.NewResolver(), nil, 10)
1486-
handler.SetIllRepo(new(illRepoNoTx))
14871486
otherId := "other-id"
14881487
body := proapi.CreatePatronRequest{Id: &otherId, RequesterSymbol: &symbol, IllRequest: validIllRequest()}
14891488
jsonBytes, _ := json.Marshal(body)
@@ -1515,7 +1514,6 @@ func TestPutPatronRequestsIdIllRepoError(t *testing.T) {
15151514

15161515
func TestPutPatronRequestsIdNotFound(t *testing.T) {
15171516
handler := NewPrApiHandler(new(PrRepoError), mockEventBus, mockEventRepo, tenant.NewResolver(), nil, 10)
1518-
handler.SetIllRepo(new(illRepoNoTx))
15191517
req, _ := http.NewRequest("PUT", "/", putBody(t, "2", validIllRequest()))
15201518
rr := httptest.NewRecorder()
15211519
handler.PutPatronRequestsId(rr, req, "2", proapi.PutPatronRequestsIdParams{})
@@ -1524,7 +1522,6 @@ func TestPutPatronRequestsIdNotFound(t *testing.T) {
15241522

15251523
func TestPutPatronRequestsIdPrRepoError(t *testing.T) {
15261524
handler := NewPrApiHandler(new(PrRepoError), mockEventBus, mockEventRepo, tenant.NewResolver(), nil, 10)
1527-
handler.SetIllRepo(new(illRepoNoTx))
15281525
req, _ := http.NewRequest("PUT", "/", putBody(t, "1", validIllRequest()))
15291526
rr := httptest.NewRecorder()
15301527
handler.PutPatronRequestsId(rr, req, "1", proapi.PutPatronRequestsIdParams{})
@@ -1537,7 +1534,6 @@ func TestPutPatronRequestsIdWrongOwner(t *testing.T) {
15371534
// without erroring; the mock returns no branch symbols for "ISIL:OTHER", producing 404.
15381535
tenantResolver := tenant.NewResolver().WithIllRepo(new(mocks.MockIllRepositorySuccess))
15391536
handler := NewPrApiHandler(new(PrRepoWrongOwner), mockEventBus, mockEventRepo, tenantResolver, nil, 10)
1540-
handler.SetIllRepo(new(illRepoNoTx))
15411537
req, _ := http.NewRequest("PUT", "/", putBody(t, "3", validIllRequest()))
15421538
rr := httptest.NewRecorder()
15431539
handler.PutPatronRequestsId(rr, req, "3", proapi.PutPatronRequestsIdParams{})
@@ -1556,7 +1552,6 @@ func TestPutPatronRequestsIdUpdateError(t *testing.T) {
15561552

15571553
func TestPutPatronRequestsIdNotBorrowingSide(t *testing.T) {
15581554
handler := NewPrApiHandler(new(PrRepoLendingSide), mockEventBus, mockEventRepo, tenant.NewResolver(), nil, 10)
1559-
handler.SetIllRepo(new(illRepoNoTx))
15601555
req, _ := http.NewRequest("PUT", "/", putBody(t, "3", validIllRequest()))
15611556
rr := httptest.NewRecorder()
15621557
handler.PutPatronRequestsId(rr, req, "3", proapi.PutPatronRequestsIdParams{})

0 commit comments

Comments
 (0)