diff --git a/broker/email/email.go b/broker/email/email.go index 1289cf9f6..5a7c8eb87 100644 --- a/broker/email/email.go +++ b/broker/email/email.go @@ -23,10 +23,8 @@ import ( pr_db "github.com/indexdata/crosslink/broker/patron_request/db" "github.com/indexdata/crosslink/iso18626" "github.com/indexdata/go-utils/utils" - "github.com/jackc/pgx/v5/pgtype" ) -const DEFAULT_FOR_NO_VALUE = "n/a" const DATE_LAYOUT = "2006-01-02" // Environment variables for SMTP configuration. @@ -282,39 +280,31 @@ func getTemplatePlaceholders(dataType reflect.Type) []TemplatePlaceholder { return placeholders } +// GetPullSlipData collects the template fields for a request. A value the +// request does not have is left empty, so a template can test it with if or +// give its own fallback with or. func GetPullSlipData(pr pr_db.PatronRequest, notes []pr_db.Notification, conditions []pr_db.Notification, barcodeData string) PullSlipData { + bib := pr.IllRequest.BibliographicInfo data := PullSlipData{ ReqId: pr.RequesterReqID.String, - CancellationReason: textValue(pr.CancellationReason, DEFAULT_FOR_NO_VALUE), + CancellationReason: pr.CancellationReason.String, PickupLocation: getPickupLocation(pr), PickupURL: getPickupURL(pr), - NeededBy: DEFAULT_FOR_NO_VALUE, - Title: DEFAULT_FOR_NO_VALUE, - TitleOfComponent: DEFAULT_FOR_NO_VALUE, - Author: DEFAULT_FOR_NO_VALUE, - AuthorOfComponent: DEFAULT_FOR_NO_VALUE, - DueDate: DEFAULT_FOR_NO_VALUE, - ReturnAddress: DEFAULT_FOR_NO_VALUE, + Title: bib.Title, + TitleOfComponent: bib.TitleOfComponent, + Author: bib.Author, + AuthorOfComponent: bib.AuthorOfComponent, BarcodeBase64: barcodeData, - ServiceType: DEFAULT_FOR_NO_VALUE, - ServiceLevel: DEFAULT_FOR_NO_VALUE, - SystemIdentifier: DEFAULT_FOR_NO_VALUE, - Publisher: DEFAULT_FOR_NO_VALUE, - MaterialType: DEFAULT_FOR_NO_VALUE, - Volume: DEFAULT_FOR_NO_VALUE, - Issue: DEFAULT_FOR_NO_VALUE, - Pages: DEFAULT_FOR_NO_VALUE, - PagesRequested: DEFAULT_FOR_NO_VALUE, + SystemIdentifier: bib.SupplierUniqueRecordId, + Volume: bib.Volume, + Issue: bib.Issue, + Pages: bib.EstimatedNoPages, + PagesRequested: bib.PagesRequested, StaffNotes: getStaffNotes(notes), CallNumber: getCallNumber(pr), Location: getItemLocation(pr, func(item pr_db.PrItem) *string { return item.Location }), ShelvingLocation: getItemLocation(pr, func(item pr_db.PrItem) *string { return item.ShelvingLocation }), LoanConditions: getLoanConditions(conditions), - PatronGivenName: DEFAULT_FOR_NO_VALUE, - PatronName: DEFAULT_FOR_NO_VALUE, - PatronSurname: DEFAULT_FOR_NO_VALUE, - PatronId: DEFAULT_FOR_NO_VALUE, - PatronProfile: DEFAULT_FOR_NO_VALUE, } for _, item := range pr.Items { data.Items = append(data.Items, PullSlipItem{ @@ -325,38 +315,11 @@ func GetPullSlipData(pr pr_db.PatronRequest, notes []pr_db.Notification, conditi ShelvingLocation: optionalItemValue(item.ShelvingLocation), }) } - if pr.IllRequest.BibliographicInfo.Author != "" { - data.Author = pr.IllRequest.BibliographicInfo.Author - } - if pr.IllRequest.BibliographicInfo.Title != "" { - data.Title = pr.IllRequest.BibliographicInfo.Title - } - if pr.IllRequest.BibliographicInfo.TitleOfComponent != "" { - data.TitleOfComponent = pr.IllRequest.BibliographicInfo.TitleOfComponent - } - if pr.IllRequest.BibliographicInfo.AuthorOfComponent != "" { - data.AuthorOfComponent = pr.IllRequest.BibliographicInfo.AuthorOfComponent - } - if pr.IllRequest.BibliographicInfo.Volume != "" { - data.Volume = pr.IllRequest.BibliographicInfo.Volume - } - if pr.IllRequest.BibliographicInfo.Issue != "" { - data.Issue = pr.IllRequest.BibliographicInfo.Issue - } - if pr.IllRequest.BibliographicInfo.EstimatedNoPages != "" { - data.Pages = pr.IllRequest.BibliographicInfo.EstimatedNoPages - } - if pr.IllRequest.BibliographicInfo.PagesRequested != "" { - data.PagesRequested = pr.IllRequest.BibliographicInfo.PagesRequested - } - if pr.IllRequest.BibliographicInfo.SupplierUniqueRecordId != "" { - data.SystemIdentifier = pr.IllRequest.BibliographicInfo.SupplierUniqueRecordId - } - if pr.IllRequest.PublicationInfo != nil && pr.IllRequest.PublicationInfo.Publisher != "" { - data.Publisher = pr.IllRequest.PublicationInfo.Publisher - } - if pr.IllRequest.PublicationInfo != nil && pr.IllRequest.PublicationInfo.PublicationType != nil && pr.IllRequest.PublicationInfo.PublicationType.Text != "" { - data.MaterialType = pr.IllRequest.PublicationInfo.PublicationType.Text + if publication := pr.IllRequest.PublicationInfo; publication != nil { + data.Publisher = publication.Publisher + if publication.PublicationType != nil { + data.MaterialType = publication.PublicationType.Text + } } if pr.DueAt.Valid { data.DueDate = pr.DueAt.Time.Format(DATE_LAYOUT) @@ -364,38 +327,30 @@ func GetPullSlipData(pr pr_db.PatronRequest, notes []pr_db.Notification, conditi if pr.IllResponse.ReturnInfo != nil && pr.IllResponse.ReturnInfo.PhysicalAddress != nil { data.ReturnAddress = formatPhysicalAddress(pr.IllResponse.ReturnInfo.PhysicalAddress) } - if pr.IllRequest.ServiceInfo != nil { - if pr.IllRequest.ServiceInfo.NeedBeforeDate != nil && !pr.IllRequest.ServiceInfo.NeedBeforeDate.IsZero() { - data.NeededBy = pr.IllRequest.ServiceInfo.NeedBeforeDate.Format(DATE_LAYOUT) + if service := pr.IllRequest.ServiceInfo; service != nil { + if service.NeedBeforeDate != nil && !service.NeedBeforeDate.IsZero() { + data.NeededBy = service.NeedBeforeDate.Format(DATE_LAYOUT) } - if pr.IllRequest.ServiceInfo.ServiceLevel != nil && pr.IllRequest.ServiceInfo.ServiceLevel.Text != "" { - data.ServiceLevel = pr.IllRequest.ServiceInfo.ServiceLevel.Text + if service.ServiceLevel != nil { + data.ServiceLevel = service.ServiceLevel.Text } - if pr.IllRequest.ServiceInfo.ServiceType != "" { - data.ServiceType = string(pr.IllRequest.ServiceInfo.ServiceType) - } - } - if pr.IllRequest.PatronInfo != nil { - if pr.IllRequest.PatronInfo.PatronId != "" { - data.PatronId = pr.IllRequest.PatronInfo.PatronId - } - if pr.IllRequest.PatronInfo.GivenName != "" { - data.PatronGivenName = pr.IllRequest.PatronInfo.GivenName - data.PatronName = pr.IllRequest.PatronInfo.GivenName - } - if pr.IllRequest.PatronInfo.Surname != "" { - data.PatronSurname = pr.IllRequest.PatronInfo.Surname - } - if pr.IllRequest.PatronInfo.PatronType != nil && pr.IllRequest.PatronInfo.PatronType.Text != "" { - data.PatronProfile = pr.IllRequest.PatronInfo.PatronType.Text + data.ServiceType = string(service.ServiceType) + } + if patron := pr.IllRequest.PatronInfo; patron != nil { + data.PatronId = patron.PatronId + data.PatronGivenName = patron.GivenName + data.PatronName = patron.GivenName + data.PatronSurname = patron.Surname + if patron.PatronType != nil { + data.PatronProfile = patron.PatronType.Text } } return data } func optionalItemValue(value *string) string { - if value == nil || *value == "" { - return DEFAULT_FOR_NO_VALUE + if value == nil { + return "" } return *value } @@ -1067,11 +1022,7 @@ func getStaffNotes(noteList []pr_db.Notification) string { noteStrings = append(noteStrings, note.Note.String) } } - notes := strings.Join(noteStrings, "\n") - if notes == "" { - return DEFAULT_FOR_NO_VALUE - } - return notes + return strings.Join(noteStrings, "\n") } func getLoanConditions(conditionList []pr_db.Notification) string { @@ -1081,11 +1032,7 @@ func getLoanConditions(conditionList []pr_db.Notification) string { conditionStrings = append(conditionStrings, note.Code.String) } } - conditions := strings.Join(conditionStrings, "\n") - if conditions == "" { - return DEFAULT_FOR_NO_VALUE - } - return conditions + return strings.Join(conditionStrings, "\n") } func getCallNumber(request pr_db.PatronRequest) string { @@ -1095,11 +1042,7 @@ func getCallNumber(request pr_db.PatronRequest) string { callNumberStrings = append(callNumberStrings, *item.CallNumber) } } - callNumber := strings.Join(callNumberStrings, ", ") - if callNumber == "" { - return DEFAULT_FOR_NO_VALUE - } - return callNumber + return strings.Join(callNumberStrings, ", ") } func getItemLocation(request pr_db.PatronRequest, value func(pr_db.PrItem) *string) string { @@ -1108,14 +1051,7 @@ func getItemLocation(request pr_db.PatronRequest, value func(pr_db.PrItem) *stri return *location } } - return DEFAULT_FOR_NO_VALUE -} - -func textValue(value pgtype.Text, fallback string) string { - if value.Valid && value.String != "" { - return value.String - } - return fallback + return "" } func getPickupLocation(request pr_db.PatronRequest) string { @@ -1127,7 +1063,7 @@ func getPickupLocation(request pr_db.PatronRequest) string { return address.ElectronicAddress.ElectronicAddressData } } - return DEFAULT_FOR_NO_VALUE + return "" } func getPickupURL(request pr_db.PatronRequest) string { @@ -1148,7 +1084,7 @@ func getPickupURL(request pr_db.PatronRequest) string { return value } } - return DEFAULT_FOR_NO_VALUE + return "" } func formatPhysicalAddress(a *iso18626.PhysicalAddress) string { diff --git a/broker/email/email_test.go b/broker/email/email_test.go index 5964be32f..985826920 100644 --- a/broker/email/email_test.go +++ b/broker/email/email_test.go @@ -18,8 +18,8 @@ import ( // --------------------------------------------------------------------------- func TestJoinAddresses_Empty(t *testing.T) { - assert.Equal(t, "", joinAddresses(nil)) - assert.Equal(t, "", joinAddresses([]string{})) + assert.Empty(t, joinAddresses(nil)) + assert.Empty(t, joinAddresses([]string{})) } func TestJoinAddresses_Single(t *testing.T) { @@ -146,7 +146,7 @@ func TestGetPullSlipDataUsesCanonicalDueDate(t *testing.T) { want string }{ {name: "canonical overrides snapshot", due: pgtype.Timestamptz{Time: legacy.AddDate(0, 0, 7), Valid: true}, want: "2026-08-22"}, - {name: "open ended ignores snapshot", want: DEFAULT_FOR_NO_VALUE}, + {name: "open ended ignores snapshot", want: ""}, } { t.Run(tc.name, func(t *testing.T) { pr := pr_db.PatronRequest{DueAt: tc.due, IllResponse: iso18626.SupplyingAgencyMessage{ @@ -569,68 +569,28 @@ func TestGetPullSlipData_PopulatesAllAvailableFields(t *testing.T) { }, data) } -func TestGetPullSlipData_ItemsUseDefaultsForMissingOptionalFields(t *testing.T) { +func TestGetPullSlipData_ItemsLeaveMissingOptionalFieldsEmpty(t *testing.T) { data := GetPullSlipData(pr_db.PatronRequest{ Items: []pr_db.PrItem{{Barcode: "item-barcode"}}, }, nil, nil, "") - assert.Equal(t, []PullSlipItem{{ - Title: DEFAULT_FOR_NO_VALUE, - Barcode: "item-barcode", - CallNumber: DEFAULT_FOR_NO_VALUE, - Location: DEFAULT_FOR_NO_VALUE, - ShelvingLocation: DEFAULT_FOR_NO_VALUE, - }}, data.Items) + assert.Equal(t, []PullSlipItem{{Barcode: "item-barcode"}}, data.Items) } -func TestGetPullSlipData_UsesDefaultsWhenOptionalFieldsAreMissing(t *testing.T) { - data := GetPullSlipData(pr_db.PatronRequest{}, nil, nil, DEFAULT_FOR_NO_VALUE) - - assert.Equal(t, PullSlipData{ - ReqId: "", - CancellationReason: DEFAULT_FOR_NO_VALUE, - PickupLocation: DEFAULT_FOR_NO_VALUE, - PickupURL: DEFAULT_FOR_NO_VALUE, - NeededBy: DEFAULT_FOR_NO_VALUE, - Title: DEFAULT_FOR_NO_VALUE, - TitleOfComponent: DEFAULT_FOR_NO_VALUE, - Author: DEFAULT_FOR_NO_VALUE, - AuthorOfComponent: DEFAULT_FOR_NO_VALUE, - DueDate: DEFAULT_FOR_NO_VALUE, - ReturnAddress: DEFAULT_FOR_NO_VALUE, - BarcodeBase64: DEFAULT_FOR_NO_VALUE, - ServiceType: DEFAULT_FOR_NO_VALUE, - ServiceLevel: DEFAULT_FOR_NO_VALUE, - SystemIdentifier: DEFAULT_FOR_NO_VALUE, - Publisher: DEFAULT_FOR_NO_VALUE, - MaterialType: DEFAULT_FOR_NO_VALUE, - Volume: DEFAULT_FOR_NO_VALUE, - Issue: DEFAULT_FOR_NO_VALUE, - Pages: DEFAULT_FOR_NO_VALUE, - PagesRequested: DEFAULT_FOR_NO_VALUE, - StaffNotes: DEFAULT_FOR_NO_VALUE, - CallNumber: DEFAULT_FOR_NO_VALUE, - Location: DEFAULT_FOR_NO_VALUE, - ShelvingLocation: DEFAULT_FOR_NO_VALUE, - LoanConditions: DEFAULT_FOR_NO_VALUE, - PatronGivenName: DEFAULT_FOR_NO_VALUE, - PatronName: DEFAULT_FOR_NO_VALUE, - PatronSurname: DEFAULT_FOR_NO_VALUE, - PatronId: DEFAULT_FOR_NO_VALUE, - PatronProfile: DEFAULT_FOR_NO_VALUE, - }, data) +func TestGetPullSlipData_LeavesMissingFieldsEmpty(t *testing.T) { + assert.Equal(t, PullSlipData{}, GetPullSlipData(pr_db.PatronRequest{}, nil, nil, "")) } -func TestGetPullSlipData_UsesDefaultNeededByForZeroDate(t *testing.T) { +func TestGetPullSlipData_LeavesNeededByEmptyForZeroDate(t *testing.T) { data := GetPullSlipData(pr_db.PatronRequest{ IllRequest: iso18626.Request{ ServiceInfo: &iso18626.ServiceInfo{ NeedBeforeDate: &utils.XSDDateTime{}, }, }, - }, nil, nil, DEFAULT_FOR_NO_VALUE) + }, nil, nil, "") - assert.Equal(t, DEFAULT_FOR_NO_VALUE, data.NeededBy) + assert.Empty(t, data.NeededBy) } // ── formatPhysicalAddress ───────────────────────────────────────────────────── @@ -667,13 +627,13 @@ func TestFormatPhysicalAddress_EmptyRegionText(t *testing.T) { } func TestFormatPhysicalAddress_Empty(t *testing.T) { - assert.Equal(t, "", formatPhysicalAddress(&iso18626.PhysicalAddress{})) + assert.Empty(t, formatPhysicalAddress(&iso18626.PhysicalAddress{})) } // ── getStaffNotes ───────────────────────────────────────────────────────────── func TestGetStaffNotes_Empty(t *testing.T) { - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getStaffNotes([]pr_db.Notification{})) + assert.Empty(t, getStaffNotes([]pr_db.Notification{})) } func TestGetStaffNotes_InvalidNotesSkipped(t *testing.T) { @@ -695,7 +655,7 @@ func TestGetStaffNotes_Multiple(t *testing.T) { // ── getLoanConditions ───────────────────────────────────────────────────────── func TestGetLoanConditions_Empty(t *testing.T) { - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getLoanConditions([]pr_db.Notification{})) + assert.Empty(t, getLoanConditions([]pr_db.Notification{})) } func TestGetLoanConditions_InvalidSkipped(t *testing.T) { @@ -717,18 +677,18 @@ func TestGetLoanConditions_Multiple(t *testing.T) { // ── getCallNumber ───────────────────────────────────────────────────────────── func TestGetCallNumber_Empty(t *testing.T) { - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getCallNumber(pr_db.PatronRequest{})) + assert.Empty(t, getCallNumber(pr_db.PatronRequest{})) } func TestGetCallNumber_NilCallNumber(t *testing.T) { pr := pr_db.PatronRequest{Items: []pr_db.PrItem{{ID: "i1", CallNumber: nil}}} - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getCallNumber(pr)) + assert.Empty(t, getCallNumber(pr)) } func TestGetCallNumber_EmptyCallNumber(t *testing.T) { empty := "" pr := pr_db.PatronRequest{Items: []pr_db.PrItem{{ID: "i1", CallNumber: &empty}}} - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getCallNumber(pr)) + assert.Empty(t, getCallNumber(pr)) } func TestGetCallNumber_Multiple(t *testing.T) { @@ -743,7 +703,7 @@ func TestGetCallNumber_Multiple(t *testing.T) { // ── getPickupLocation ───────────────────────────────────────────────────────── func TestGetPickupLocation_NoDeliveryInfo(t *testing.T) { - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getPickupLocation(pr_db.PatronRequest{})) + assert.Empty(t, getPickupLocation(pr_db.PatronRequest{})) } func TestGetPickupLocation_NilAddress(t *testing.T) { @@ -752,7 +712,7 @@ func TestGetPickupLocation_NilAddress(t *testing.T) { RequestedDeliveryInfo: []iso18626.RequestedDeliveryInfo{{Address: nil}}, }, } - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getPickupLocation(pr)) + assert.Empty(t, getPickupLocation(pr)) } func TestGetPickupLocation_PhysicalAddress(t *testing.T) { @@ -792,7 +752,7 @@ func TestGetPickupLocation_AddressWithNoUsableFields(t *testing.T) { }, }, } - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getPickupLocation(pr)) + assert.Empty(t, getPickupLocation(pr)) } // ── getPickupURL ─────────────────────────────────────────────────────────────────── @@ -807,7 +767,7 @@ func TestGetPickupURL_IgnoresEmailAddress(t *testing.T) { }}, }} - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getPickupURL(pr)) + assert.Empty(t, getPickupURL(pr)) } func TestGetPickupURL_RejectsUnsafeRequestSchemes(t *testing.T) { @@ -824,7 +784,7 @@ func TestGetPickupURL_RejectsUnsafeRequestSchemes(t *testing.T) { }}, }} - assert.Equal(t, DEFAULT_FOR_NO_VALUE, getPickupURL(pr)) + assert.Empty(t, getPickupURL(pr)) }) } } @@ -886,7 +846,7 @@ func TestRenderTextTemplateSupportsPatronGivenNameAndLegacyPatronName(t *testing IllRequest: iso18626.Request{ PatronInfo: &iso18626.PatronInfo{GivenName: "Ann"}, }, - }, nil, nil, DEFAULT_FOR_NO_VALUE) + }, nil, nil, "") result, err := RenderTextTemplate(data, []string{"pullslip-pdf"}, "{{.PatronGivenName}}/{{.PatronName}}") diff --git a/broker/patron_request/api/api-handler_test.go b/broker/patron_request/api/api-handler_test.go index 9c15b06aa..2ef019308 100644 --- a/broker/patron_request/api/api-handler_test.go +++ b/broker/patron_request/api/api-handler_test.go @@ -1566,8 +1566,8 @@ func TestPostTemplatesPreviewRejectsBadRequests(t *testing.T) { &templateErrorDetails{part: "body", line: 3, column: 3, message: "unknown field Nope"}}, {"batch placeholder", `{"body":"{{.FullCount}}"}`, "FullCount", &templateErrorDetails{part: "body", line: 1, column: 3, message: "unknown field FullCount"}}, - {"fails on one sample", `{"body":"
{{if ne .Title \"n/a\"}}{{index .Title 40}}{{end}}
"}`, "sample typical: template body:", - &templateErrorDetails{part: "body", sample: "typical", line: 1, column: 28, message: `executing "template" at{{if .Title}}{{index .Title 40}}{{end}}
"}`, "sample typical: template body:", + &templateErrorDetails{part: "body", sample: "typical", line: 1, column: 19, message: `executing "template" at