Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions broker/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 4 additions & 7 deletions broker/oapi/open-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,6 @@ components:
requesterSymbol:
type: string
description: Requester symbol
supplierSymbol:
type: string
description: Supplier symbol
required:
- illRequest

Expand Down Expand Up @@ -740,15 +737,15 @@ components:

PrNotification:
type: object
title: Notification
title: Notification
description: Patron request notification
properties:
id:
type: string
description: Notification id
fromSymbol:
type: string
description: Symbol of notification sender
description: Symbol of notification sender
toSymbol:
type: string
description: Symbol of notification receiver
Expand Down Expand Up @@ -1455,7 +1452,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/SseResult'

/state_model/models/{model}:
get:
summary: Retrieve a state model by name
Expand All @@ -1464,7 +1461,7 @@ paths:
parameters:
- in: path
name: model
schema:
schema:
type: string
required: true
description: The name of the statemodel to retrieve
Expand Down
3 changes: 2 additions & 1 deletion broker/patron_request/api/api-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions broker/patron_request/api/api-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions broker/patron_request/db/prrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion broker/patron_request/service/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions broker/patron_request/service/message-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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{
Expand Down
22 changes: 11 additions & 11 deletions broker/patron_request/service/message-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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))

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions broker/sqlc/pr_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,4 +103,4 @@ WHERE pr_id = $1;
-- name: DeleteNotificationById :exec
DELETE
FROM notification
WHERE id = $1;
WHERE id = $1;
10 changes: 4 additions & 6 deletions broker/test/patron_request/api/api-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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)
Expand Down Expand Up @@ -232,7 +231,6 @@ func TestActionsToCompleteState(t *testing.T) {
},
}
newPr := proapi.CreatePatronRequest{
SupplierSymbol: &supplierSymbol,
RequesterSymbol: &requesterSymbol,
Patron: &patron,
IllRequest: utils.Must(common.StructToMap(request)),
Expand Down Expand Up @@ -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"
Expand Down