From bc93088ea824669952c75f3fc8a1f26dbc9c64b5 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 24 Nov 2026 18:06:23 -0500 Subject: [PATCH 1/2] ILLDEV-583 Pass empty string instead of n/a to templates This way the template can more readily determine its own placeholder or if it wants to display the field at all. --- broker/email/email.go | 146 +++++------------- broker/email/email_test.go | 84 +++------- broker/patron_request/api/api-handler_test.go | 4 +- broker/patron_request/service/action.go | 2 +- broker/patron_request/service/template.go | 11 +- .../patron_request/service/template_test.go | 29 +++- broker/pullslip/service/pdf_test.go | 5 +- migration/export-crosslink-config.sql | 9 +- migration/scripts.md | 5 +- 9 files changed, 102 insertions(+), 193 deletions(-) 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 : error calling index: index out of range: 40`}}, + {"fails on one sample", `{"body":"

{{if .Title}}{{index .Title 40}}{{end}}

"}`, "sample typical: template body:", + &templateErrorDetails{part: "body", sample: "typical", line: 1, column: 19, message: `executing "template" at : error calling index: index out of range: 40`}}, } { t.Run(tc.name, func(t *testing.T) { rr := postTemplatesPreview(t, tc.body) diff --git a/broker/patron_request/service/action.go b/broker/patron_request/service/action.go index 5cd682a7b..41c2a3c9b 100644 --- a/broker/patron_request/service/action.go +++ b/broker/patron_request/service/action.go @@ -2443,7 +2443,7 @@ func (a *PatronRequestActionService) createAndSendEmail(ctx common.ExtendedConte } return fmt.Errorf("validate email template %q (label %q, audience %s): %w", template.ID, label, audience, err) } - data := email.GetPullSlipData(pr, notes, conditions, email.DEFAULT_FOR_NO_VALUE) + data := email.GetPullSlipData(pr, notes, conditions, "") var body string if template.ContentType == string(proapi.Html) { body, err = email.RenderHtmlTemplate(data, template.Labels, template.Body) diff --git a/broker/patron_request/service/template.go b/broker/patron_request/service/template.go index 3d9d0b383..226847426 100644 --- a/broker/patron_request/service/template.go +++ b/broker/patron_request/service/template.go @@ -64,14 +64,13 @@ func ValidateTemplateRenderingForContext(purpose proapi.TemplatePurpose, content var data any switch context { case PatronRequestTemplateContext: - data = email.GetPullSlipData(pr_db.PatronRequest{}, nil, nil, email.DEFAULT_FOR_NO_VALUE) - // Request IDs can be empty in a zero-valued request, but runtime - // rendering supplies a populated request ID. - patronData := data.(email.PullSlipData) - patronData.ReqId = email.DEFAULT_FOR_NO_VALUE + // Optional fields stay empty, as they are for a sparse request, but + // runtime rendering always has a request ID. + patronData := email.GetPullSlipData(pr_db.PatronRequest{}, nil, nil, "") + patronData.ReqId = "REQ-1" data = patronData case BatchEmailTemplateContext: - data = email.GetBatchEmailData(1, 1, email.DEFAULT_FOR_NO_VALUE) + data = email.GetBatchEmailData(1, 1, "query") default: return fmt.Errorf("unknown template rendering context %d", context) } diff --git a/broker/patron_request/service/template_test.go b/broker/patron_request/service/template_test.go index 8c62f3a5c..862ae3dcb 100644 --- a/broker/patron_request/service/template_test.go +++ b/broker/patron_request/service/template_test.go @@ -33,27 +33,44 @@ func TestGetStateModelTemplateDefaultMatchesPurposeAudienceAndLabel(t *testing.T assert.ErrorContains(t, err, "no state-model default template") } -func TestValidateTemplateRenderingUsesRuntimeFallbackValues(t *testing.T) { +func TestValidateTemplateRenderingUsesRuntimeValues(t *testing.T) { tests := []struct { name string purpose proapi.TemplatePurpose contentType proapi.TemplateContentType body string labels []string + wantErr string }{ { - name: "patron request html", + name: "patron request html with request ID", + purpose: proapi.Pullslip, + contentType: proapi.Html, + body: `{{index .ReqId 0}}`, + labels: []string{"pullslip-pdf"}, + }, + { + name: "patron request text with request ID", + purpose: proapi.Email, + contentType: proapi.Text, + body: `{{index .ReqId 0}}`, + labels: []string{"received-notification"}, + }, + { + name: "patron request html with missing title", purpose: proapi.Pullslip, contentType: proapi.Html, body: `{{index .Title 0}}`, labels: []string{"pullslip-pdf"}, + wantErr: "index out of range", }, { - name: "patron request text", + name: "patron request text with missing title", purpose: proapi.Email, contentType: proapi.Text, body: `{{index .Title 0}}`, labels: []string{"received-notification"}, + wantErr: "index out of range", }, { name: "batch email html", @@ -74,7 +91,11 @@ func TestValidateTemplateRenderingUsesRuntimeFallbackValues(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { err := ValidateTemplateRendering(test.purpose, test.contentType, test.body, nil, test.labels) - assert.NoError(t, err) + if test.wantErr == "" { + assert.NoError(t, err) + } else { + assert.ErrorContains(t, err, test.wantErr) + } }) } } diff --git a/broker/pullslip/service/pdf_test.go b/broker/pullslip/service/pdf_test.go index 4cacc422d..8e61aab46 100644 --- a/broker/pullslip/service/pdf_test.go +++ b/broker/pullslip/service/pdf_test.go @@ -12,7 +12,6 @@ import ( "github.com/carlos7ags/folio/reader" "github.com/indexdata/crosslink/broker/common" - "github.com/indexdata/crosslink/broker/email" pr_db "github.com/indexdata/crosslink/broker/patron_request/db" "github.com/indexdata/crosslink/broker/patron_request/proapi" prservice "github.com/indexdata/crosslink/broker/patron_request/service" @@ -66,7 +65,7 @@ func TestGeneratePdfPullSlip_Defaults(t *testing.T) { String: "REQ-DEFAULTS", Valid: true, }, - // No bibliographic info — all fields should fall back to DEFAULT_FOR_NO_VALUE + // No bibliographic info, so those fields render empty. } pdfBytes, err := svc.GeneratePdfPullSlip(appCtx, pr, []pr_db.Notification{}, []pr_db.Notification{}) assert.NoError(t, err) @@ -376,7 +375,7 @@ func TestRenderPdfPullSlip_EverySampleWithDefaultPreset(t *testing.T) { want := map[string][]string{ "typical": {"REQ-2026-00417", "QA76.6", "2026-08-15", "Library use only"}, "long": {"US-REQ-CONSORTIUM-2026-0000419", "Z 105.5"}, - "sparse": {"REQ-7", email.DEFAULT_FOR_NO_VALUE}, + "sparse": {"REQ-7"}, "requester": {"REQ-2026-00418", "Silent Spring", "2026-09-01"}, } samples, err := prservice.GetTemplateSamples() diff --git a/migration/export-crosslink-config.sql b/migration/export-crosslink-config.sql index 90c1b2abb..597368ffa 100644 --- a/migration/export-crosslink-config.sql +++ b/migration/export-crosslink-config.sql @@ -98,7 +98,7 @@ DECLARE 'all locations' ); if_replacement TEXT := coalesce( - '{{if ne ' || crosslink_path || ' "n/a"}}', + '{{if ' || crosslink_path || '}}', '{{if true}}' ); BEGIN @@ -364,12 +364,7 @@ template_action_patterns AS ( replace(placeholder_mappings.crosslink_path, '.', '[.]'), '|' ORDER BY placeholder_mappings.step ) - || ')|if[[:space:]]+ne[[:space:]]+(' - || string_agg( - replace(placeholder_mappings.crosslink_path, '.', '[.]'), - '|' ORDER BY placeholder_mappings.step - ) - || ')[[:space:]]+"n/a"|if[[:space:]]+true|else|end)' + || ')|if[[:space:]]+true|else|end)' || '[[:space:]]*[}][}]' AS supported_action_pattern FROM placeholder_mappings diff --git a/migration/scripts.md b/migration/scripts.md index 7097e2140..ae9742b64 100644 --- a/migration/scripts.md +++ b/migration/scripts.md @@ -121,9 +121,8 @@ After all schemas are exported, the script runs `join-crosslink-exports.sh` on the current directory. Known Handlebars placeholders, `#if` blocks, and triple-brace placeholders are -converted to CrossLink Go-template syntax. Converted `#if` blocks compare their -field against the `n/a` missing-value sentinel, so absent optional values follow -the Handlebars `else` branch. Triple-brace values use CrossLink's normal HTML +converted to CrossLink Go-template syntax. A missing optional value is empty, +so a converted `#if` block follows the Handlebars `else` branch for it. Triple-brace values use CrossLink's normal HTML escaping. A converted template is included in `config.ndjson` only when it is valid for either patron-request or batch-email data according to the exporter's syntax and data-context checks. Templates with residual Handlebars actions or a From a0f17841a65ea9bad1e088fb78a33dc4cace5c5e Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 27 Nov 2026 13:18:37 -0500 Subject: [PATCH 2/2] There's always a barcode --- broker/patron_request/service/template.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/broker/patron_request/service/template.go b/broker/patron_request/service/template.go index 226847426..fad259b92 100644 --- a/broker/patron_request/service/template.go +++ b/broker/patron_request/service/template.go @@ -65,9 +65,13 @@ func ValidateTemplateRenderingForContext(purpose proapi.TemplatePurpose, content switch context { case PatronRequestTemplateContext: // Optional fields stay empty, as they are for a sparse request, but - // runtime rendering always has a request ID. + // runtime rendering always has a request ID, and a PDF pull slip + // always has its barcode image. Emails render without one. patronData := email.GetPullSlipData(pr_db.PatronRequest{}, nil, nil, "") patronData.ReqId = "REQ-1" + if purpose == proapi.Pullslip { + patronData.BarcodeBase64 = "iVBORw0KGgo=" + } data = patronData case BatchEmailTemplateContext: data = email.GetBatchEmailData(1, 1, "query")