diff --git a/broker/client/client_test.go b/broker/client/client_test.go index 877e72fc..7c357893 100644 --- a/broker/client/client_test.go +++ b/broker/client/client_test.go @@ -870,3 +870,7 @@ type MockPrRepo struct { func (r *MockPrRepo) GetPatronRequestById(ctx common.ExtendedContext, id string) (pr_db.PatronRequest, error) { return pr_db.PatronRequest{}, errors.New("searching pr with id=" + id) } + +func (r *MockPrRepo) GetPatronRequestByIdAndSide(ctx common.ExtendedContext, id string, side pr_db.PatronRequestSide) (pr_db.PatronRequest, error) { + return pr_db.PatronRequest{}, errors.New("searching pr with id=" + id) +} diff --git a/broker/oapi/open-api.yaml b/broker/oapi/open-api.yaml index b15d2b9e..c459b56d 100644 --- a/broker/oapi/open-api.yaml +++ b/broker/oapi/open-api.yaml @@ -500,9 +500,6 @@ components: requesterSymbol: type: string description: Requester symbol - supplierSymbol: - type: string - description: Supplier symbol required: - illRequest @@ -740,7 +737,7 @@ components: PrNotification: type: object - title: Notification + title: Notification description: Patron request notification properties: id: @@ -748,7 +745,7 @@ components: description: Notification id fromSymbol: type: string - description: Symbol of notification sender + description: Symbol of notification sender toSymbol: type: string description: Symbol of notification receiver @@ -1455,7 +1452,7 @@ paths: application/json: schema: $ref: '#/components/schemas/SseResult' - + /state_model/models/{model}: get: summary: Retrieve a state model by name @@ -1464,7 +1461,7 @@ paths: parameters: - in: path name: model - schema: + schema: type: string required: true description: The name of the statemodel to retrieve diff --git a/broker/patron_request/api/api-handler.go b/broker/patron_request/api/api-handler.go index 6bed3168..0cacbf12 100644 --- a/broker/patron_request/api/api-handler.go +++ b/broker/patron_request/api/api-handler.go @@ -590,9 +590,10 @@ func (a *PatronRequestApiHandler) toDbPatronRequest(ctx common.ExtendedContext, Side: prservice.SideBorrowing, Patron: getDbText(request.Patron), RequesterSymbol: getDbText(request.RequesterSymbol), - SupplierSymbol: getDbText(request.SupplierSymbol), + SupplierSymbol: getDbText(nil), IllRequest: illRequest, Tenant: getDbText(tenant), + RequesterReqID: getDbText(&id), }, nil } diff --git a/broker/patron_request/api/api-handler_test.go b/broker/patron_request/api/api-handler_test.go index 554fe01c..491943a1 100644 --- a/broker/patron_request/api/api-handler_test.go +++ b/broker/patron_request/api/api-handler_test.go @@ -350,6 +350,9 @@ func TestToDbPatronRequest(t *testing.T) { assert.NoError(t, err) assert.Equal(t, id, pr.ID) assert.True(t, pr.Timestamp.Valid) + assert.True(t, pr.RequesterReqID.Valid) + assert.Equal(t, id, pr.RequesterReqID.String) + assert.False(t, pr.SupplierSymbol.Valid) pr, err = handler.toDbPatronRequest(ctx, proapi.CreatePatronRequest{RequesterSymbol: &symbol}, nil) assert.NoError(t, err) diff --git a/broker/patron_request/db/prrepo.go b/broker/patron_request/db/prrepo.go index c59220e0..94bd5b7b 100644 --- a/broker/patron_request/db/prrepo.go +++ b/broker/patron_request/db/prrepo.go @@ -5,17 +5,19 @@ import ( "github.com/indexdata/crosslink/broker/common" "github.com/indexdata/crosslink/broker/repo" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" ) type PrRepo interface { repo.Transactional[PrRepo] GetPatronRequestById(ctx common.ExtendedContext, id string) (PatronRequest, error) + GetPatronRequestByIdAndSide(ctx common.ExtendedContext, id string, side PatronRequestSide) (PatronRequest, error) ListPatronRequests(ctx common.ExtendedContext, args ListPatronRequestsParams, cql *string) ([]PatronRequest, int64, error) UpdatePatronRequest(ctx common.ExtendedContext, params UpdatePatronRequestParams) (PatronRequest, error) CreatePatronRequest(ctx common.ExtendedContext, params CreatePatronRequestParams) (PatronRequest, error) DeletePatronRequest(ctx common.ExtendedContext, id string) error - GetPatronRequestBySupplierSymbolAndRequesterReqId(ctx common.ExtendedContext, supplierSymbol string, requesterReId string) (PatronRequest, error) + GetLendingRequestBySupplierSymbolAndRequesterReqId(ctx common.ExtendedContext, supplierSymbol string, requesterReId string) (PatronRequest, error) GetNextHrid(ctx common.ExtendedContext, prefix string) (string, error) SaveItem(ctx common.ExtendedContext, params SaveItemParams) (Item, error) GetItemById(ctx common.ExtendedContext, id string) (Item, error) @@ -47,6 +49,17 @@ func (r *PgPrRepo) GetPatronRequestById(ctx common.ExtendedContext, id string) ( return row.PatronRequest, err } +func (r *PgPrRepo) GetPatronRequestByIdAndSide(ctx common.ExtendedContext, id string, side PatronRequestSide) (PatronRequest, error) { + pr, err := r.GetPatronRequestById(ctx, id) + if err != nil { + return PatronRequest{}, err + } + if pr.Side != side { + return PatronRequest{}, pgx.ErrNoRows + } + return pr, nil +} + func (r *PgPrRepo) ListPatronRequests(ctx common.ExtendedContext, params ListPatronRequestsParams, cql *string) ([]PatronRequest, int64, error) { rows, err := r.queries.ListPatronRequestsCql(ctx, r.GetConnOrTx(), params, cql) var list []PatronRequest @@ -83,8 +96,8 @@ func (r *PgPrRepo) DeletePatronRequest(ctx common.ExtendedContext, id string) er return r.queries.DeletePatronRequest(ctx, r.GetConnOrTx(), id) } -func (r *PgPrRepo) GetPatronRequestBySupplierSymbolAndRequesterReqId(ctx common.ExtendedContext, supplierSymbol string, requesterReId string) (PatronRequest, error) { - row, err := r.queries.GetPatronRequestBySupplierSymbolAndRequesterReqId(ctx, r.GetConnOrTx(), GetPatronRequestBySupplierSymbolAndRequesterReqIdParams{ +func (r *PgPrRepo) GetLendingRequestBySupplierSymbolAndRequesterReqId(ctx common.ExtendedContext, supplierSymbol string, requesterReId string) (PatronRequest, error) { + row, err := r.queries.GetLendingRequestBySupplierSymbolAndRequesterReqId(ctx, r.GetConnOrTx(), GetLendingRequestBySupplierSymbolAndRequesterReqIdParams{ SupplierSymbol: pgtype.Text{ String: supplierSymbol, Valid: true, diff --git a/broker/patron_request/service/action_test.go b/broker/patron_request/service/action_test.go index 70096cab..37126d34 100644 --- a/broker/patron_request/service/action_test.go +++ b/broker/patron_request/service/action_test.go @@ -715,6 +715,11 @@ func (r *MockPrRepo) GetPatronRequestById(ctx common.ExtendedContext, id string) return args.Get(0).(pr_db.PatronRequest), args.Error(1) } +func (r *MockPrRepo) GetPatronRequestByIdAndSide(ctx common.ExtendedContext, id string, side pr_db.PatronRequestSide) (pr_db.PatronRequest, error) { + args := r.Called(id, side) + return args.Get(0).(pr_db.PatronRequest), args.Error(1) +} + func (r *MockPrRepo) UpdatePatronRequest(ctx common.ExtendedContext, params pr_db.UpdatePatronRequestParams) (pr_db.PatronRequest, error) { if strings.Contains(params.ID, "error") || strings.Contains(params.RequesterReqID.String, "error") { return pr_db.PatronRequest{}, errors.New("db error") @@ -731,7 +736,7 @@ func (r *MockPrRepo) CreatePatronRequest(ctx common.ExtendedContext, params pr_d return pr_db.PatronRequest(params), nil } -func (r *MockPrRepo) GetPatronRequestBySupplierSymbolAndRequesterReqId(ctx common.ExtendedContext, symbol string, requesterReqId string) (pr_db.PatronRequest, error) { +func (r *MockPrRepo) GetLendingRequestBySupplierSymbolAndRequesterReqId(ctx common.ExtendedContext, symbol string, requesterReqId string) (pr_db.PatronRequest, error) { args := r.Called(symbol, requesterReqId) return args.Get(0).(pr_db.PatronRequest), args.Error(1) } diff --git a/broker/patron_request/service/message-handler.go b/broker/patron_request/service/message-handler.go index bb3d5af6..c39df79a 100644 --- a/broker/patron_request/service/message-handler.go +++ b/broker/patron_request/service/message-handler.go @@ -117,16 +117,16 @@ func (m *PatronRequestMessageHandler) handlePatronRequestMessage(ctx common.Exte func (m *PatronRequestMessageHandler) getPatronRequest(ctx common.ExtendedContext, msg iso18626.ISO18626Message) (pr_db.PatronRequest, error) { if msg.SupplyingAgencyMessage != nil { - return m.prRepo.GetPatronRequestById(ctx, msg.SupplyingAgencyMessage.Header.RequestingAgencyRequestId) + return m.prRepo.GetPatronRequestByIdAndSide(ctx, msg.SupplyingAgencyMessage.Header.RequestingAgencyRequestId, SideBorrowing) } else if msg.RequestingAgencyMessage != nil { if msg.RequestingAgencyMessage.Header.SupplyingAgencyRequestId != "" { - return m.prRepo.GetPatronRequestById(ctx, msg.RequestingAgencyMessage.Header.SupplyingAgencyRequestId) + return m.prRepo.GetPatronRequestByIdAndSide(ctx, msg.RequestingAgencyMessage.Header.SupplyingAgencyRequestId, SideLending) } else { symbol := msg.RequestingAgencyMessage.Header.SupplyingAgencyId.AgencyIdType.Text + ":" + msg.RequestingAgencyMessage.Header.SupplyingAgencyId.AgencyIdValue - return m.prRepo.GetPatronRequestBySupplierSymbolAndRequesterReqId(ctx, symbol, msg.RequestingAgencyMessage.Header.RequestingAgencyRequestId) + return m.prRepo.GetLendingRequestBySupplierSymbolAndRequesterReqId(ctx, symbol, msg.RequestingAgencyMessage.Header.RequestingAgencyRequestId) } } else if msg.Request != nil { - return m.prRepo.GetPatronRequestById(ctx, msg.Request.Header.RequestingAgencyRequestId) + return m.prRepo.GetPatronRequestByIdAndSide(ctx, msg.Request.Header.RequestingAgencyRequestId, SideBorrowing) } else { return pr_db.PatronRequest{}, errors.New("missing message") } @@ -292,7 +292,7 @@ func (m *PatronRequestMessageHandler) handleRequestMessage(ctx common.ExtendedCo } supplierSymbol := request.Header.SupplyingAgencyId.AgencyIdType.Text + ":" + request.Header.SupplyingAgencyId.AgencyIdValue requesterSymbol := request.Header.RequestingAgencyId.AgencyIdType.Text + ":" + request.Header.RequestingAgencyId.AgencyIdValue - _, err := m.prRepo.GetPatronRequestBySupplierSymbolAndRequesterReqId(ctx, supplierSymbol, raRequestId) + _, err := m.prRepo.GetLendingRequestBySupplierSymbolAndRequesterReqId(ctx, supplierSymbol, raRequestId) if err != nil { if !errors.Is(err, pgx.ErrNoRows) { return createRequestResponse(request, iso18626.TypeMessageStatusERROR, &iso18626.ErrorData{ diff --git a/broker/patron_request/service/message-handler_test.go b/broker/patron_request/service/message-handler_test.go index 88f06b4c..f0ac0823 100644 --- a/broker/patron_request/service/message-handler_test.go +++ b/broker/patron_request/service/message-handler_test.go @@ -17,9 +17,9 @@ import ( func TestGetPatronRequest(t *testing.T) { mockPrRepo := new(MockPrRepo) - mockPrRepo.On("GetPatronRequestById", "req-id-1").Return(pr_db.PatronRequest{ID: "req-id-1"}, nil) - mockPrRepo.On("GetPatronRequestById", "sam-id-1").Return(pr_db.PatronRequest{ID: "sam-id-1"}, nil) - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{ID: "sam-id-1"}, nil) + mockPrRepo.On("GetPatronRequestByIdAndSide", "req-id-1", SideBorrowing).Return(pr_db.PatronRequest{ID: "req-id-1", Side: SideBorrowing}, nil) + mockPrRepo.On("GetPatronRequestByIdAndSide", "sam-id-1", SideLending).Return(pr_db.PatronRequest{ID: "sam-id-1", Side: SideLending}, nil) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{ID: "sam-id-1", Side: SideLending}, nil) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), *new(events.EventBus)) msg := iso18626.ISO18626Message{ @@ -93,7 +93,7 @@ func TestHandleMessageNoMessage(t *testing.T) { func TestHandleMessageFetchPRError(t *testing.T) { mockPrRepo := new(MockPrRepo) - mockPrRepo.On("GetPatronRequestById", patronRequestId).Return(pr_db.PatronRequest{}, errors.New("db error")) + mockPrRepo.On("GetPatronRequestByIdAndSide", patronRequestId, SideBorrowing).Return(pr_db.PatronRequest{}, errors.New("db error")) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), *new(events.EventBus)) @@ -112,7 +112,7 @@ func TestHandleMessageFetchPRError(t *testing.T) { func TestHandleMessageFetchEventError(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) - mockPrRepo.On("GetPatronRequestById", patronRequestId).Return(pr_db.PatronRequest{ID: "error"}, nil) + mockPrRepo.On("GetPatronRequestByIdAndSide", patronRequestId, SideBorrowing).Return(pr_db.PatronRequest{ID: "error", Side: SideBorrowing}, nil) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) @@ -581,7 +581,7 @@ func TestHandleRequestMessage(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) mockAutoActionRunner := &MockAutoActionRunner{} - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) handler.SetAutoActionRunner(mockAutoActionRunner) @@ -614,7 +614,7 @@ func TestHandleRequestMessageAutoActionError(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) mockAutoActionRunner := &MockAutoActionRunner{err: errors.New("auto action failed")} - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) handler.SetAutoActionRunner(mockAutoActionRunner) @@ -640,7 +640,7 @@ func TestHandleRequestMessageAutoActionError(t *testing.T) { func TestHandleRequestMessageMissingRequestId(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) status, resp, err := handler.handleRequestMessage(appCtx, iso18626.Request{ @@ -669,7 +669,7 @@ func TestHandleRequestMessageMissingRequestId(t *testing.T) { func TestHandleRequestMessageExistingRequest(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, nil) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, nil) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) status, resp, err := handler.handleRequestMessage(appCtx, iso18626.Request{ @@ -698,7 +698,7 @@ func TestHandleRequestMessageExistingRequest(t *testing.T) { func TestHandleRequestMessageSearchDbError(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, errors.New("db error")) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "req-id-1").Return(pr_db.PatronRequest{}, errors.New("db error")) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) status, resp, err := handler.handleRequestMessage(appCtx, iso18626.Request{ @@ -727,7 +727,7 @@ func TestHandleRequestMessageSearchDbError(t *testing.T) { func TestHandleRequestMessageSaveError(t *testing.T) { mockPrRepo := new(MockPrRepo) mockEventBus := new(MockEventBus) - mockPrRepo.On("GetPatronRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "error").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) + mockPrRepo.On("GetLendingRequestBySupplierSymbolAndRequesterReqId", "ISIL:SUP1", "error").Return(pr_db.PatronRequest{}, pgx.ErrNoRows) handler := CreatePatronRequestMessageHandler(mockPrRepo, *new(events.EventRepo), *new(ill_db.IllRepo), mockEventBus) status, resp, err := handler.handleRequestMessage(appCtx, iso18626.Request{ diff --git a/broker/sqlc/pr_query.sql b/broker/sqlc/pr_query.sql index 5b9322bb..357bc18f 100644 --- a/broker/sqlc/pr_query.sql +++ b/broker/sqlc/pr_query.sql @@ -34,11 +34,11 @@ DELETE FROM patron_request WHERE id = $1; --- name: GetPatronRequestBySupplierSymbolAndRequesterReqId :one +-- name: GetLendingRequestBySupplierSymbolAndRequesterReqId :one -- params: supplier_symbol string, requester_req_id string SELECT sqlc.embed(patron_request) FROM patron_request -WHERE supplier_symbol = $1 AND requester_req_id = $2 +WHERE supplier_symbol = $1 AND requester_req_id = $2 AND side = 'lending' LIMIT 1; -- name: GetNextHrid :one @@ -103,4 +103,4 @@ WHERE pr_id = $1; -- name: DeleteNotificationById :exec DELETE FROM notification -WHERE id = $1; \ No newline at end of file +WHERE id = $1; diff --git a/broker/test/patron_request/api/api-handler_test.go b/broker/test/patron_request/api/api-handler_test.go index 11258e80..bf32926c 100644 --- a/broker/test/patron_request/api/api-handler_test.go +++ b/broker/test/patron_request/api/api-handler_test.go @@ -106,7 +106,6 @@ func TestCrud(t *testing.T) { id := uuid.NewString() newPr := proapi.CreatePatronRequest{ Id: &id, - SupplierSymbol: &supplierSymbol, RequesterSymbol: &requesterSymbol, Patron: &patron, IllRequest: utils.Must(common.StructToMap(request)), @@ -128,7 +127,7 @@ func TestCrud(t *testing.T) { assert.True(t, foundPr.State != "") assert.Equal(t, string(prservice.SideBorrowing), foundPr.Side) assert.Equal(t, *newPr.RequesterSymbol, *foundPr.RequesterSymbol) - assert.Equal(t, *newPr.SupplierSymbol, *foundPr.SupplierSymbol) + assert.Nil(t, foundPr.SupplierSymbol) assert.Equal(t, *newPr.Patron, *foundPr.Patron) respBytes = httpRequest(t, "POST", basePath, newPrBytes, 400) @@ -232,7 +231,6 @@ func TestActionsToCompleteState(t *testing.T) { }, } newPr := proapi.CreatePatronRequest{ - SupplierSymbol: &supplierSymbol, RequesterSymbol: &requesterSymbol, Patron: &patron, IllRequest: utils.Must(common.StructToMap(request)), @@ -266,16 +264,16 @@ func TestActionsToCompleteState(t *testing.T) { // Find supplier patron request test.WaitForPredicateToBeTrue(func() bool { - supPr, _ := prRepo.GetPatronRequestBySupplierSymbolAndRequesterReqId(appCtx, supplierSymbol, foundPr.Id) + supPr, _ := prRepo.GetLendingRequestBySupplierSymbolAndRequesterReqId(appCtx, supplierSymbol, foundPr.Id) return supPr.ID != "" }) - supPr, err := prRepo.GetPatronRequestBySupplierSymbolAndRequesterReqId(appCtx, supplierSymbol, foundPr.Id) + supPr, err := prRepo.GetLendingRequestBySupplierSymbolAndRequesterReqId(appCtx, supplierSymbol, foundPr.Id) assert.NoError(t, err) assert.NotNil(t, supPr.ID) // Wait for action supplierPrPath := basePath + "/" + supPr.ID - supQueryParams := "?side=lending&symbol=" + *foundPr.SupplierSymbol + supQueryParams := "?side=lending&symbol=" + supplierSymbol test.WaitForPredicateToBeTrue(func() bool { respBytes = httpRequest(t, "GET", supplierPrPath+"/actions"+supQueryParams, []byte{}, 200) return string(respBytes) == "[\""+string(prservice.LenderActionShip)+"\"]\n"