feat(hardlink): unify kiosk payments and finalize legacy cleanup
This commit is contained in:
+18
-12
@@ -17,14 +17,20 @@ import (
|
||||
// ReadHotel gets the hotel record from CMS and returns its reservation system
|
||||
// configuration if the record has been updated since the last read.
|
||||
func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, error) {
|
||||
var reservationSystem cmstypes.ReservationSystemRec
|
||||
hotel, err := ReadHotelDetails(hotelCode, CMSBaseURL)
|
||||
return hotel.ReservationSystem, err
|
||||
}
|
||||
|
||||
// ReadHotelDetails retains the complete hotel response, including its currency.
|
||||
func ReadHotelDetails(hotelCode, CMSBaseURL string) (cmstypes.Hotel, error) {
|
||||
var hotel cmstypes.Hotel
|
||||
|
||||
if hotelCode == "" {
|
||||
return reservationSystem, errors.New("hotel code is empty")
|
||||
return hotel, errors.New("hotel code is empty")
|
||||
}
|
||||
|
||||
if CMSBaseURL == "" {
|
||||
return reservationSystem, errors.New("CMS base URL is empty")
|
||||
return hotel, errors.New("CMS base URL is empty")
|
||||
}
|
||||
|
||||
var request cmstypes.RequestRec
|
||||
@@ -36,7 +42,7 @@ func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, err
|
||||
|
||||
requestData, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"marshal CMS hotel request: %w",
|
||||
err,
|
||||
)
|
||||
@@ -46,7 +52,7 @@ func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, err
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(requestData))
|
||||
if err != nil {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"create CMS hotel request: %w",
|
||||
err,
|
||||
)
|
||||
@@ -61,7 +67,7 @@ func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, err
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"perform CMS hotel request: %w",
|
||||
err,
|
||||
)
|
||||
@@ -70,7 +76,7 @@ func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, err
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"read CMS hotel response: %w",
|
||||
err,
|
||||
)
|
||||
@@ -78,7 +84,7 @@ func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, err
|
||||
|
||||
if resp.StatusCode < http.StatusOK ||
|
||||
resp.StatusCode >= http.StatusMultipleChoices {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"CMS hotel request returned HTTP %s: %s",
|
||||
resp.Status,
|
||||
string(body),
|
||||
@@ -88,26 +94,26 @@ func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, err
|
||||
var hotelResponse cmstypes.HotelResponseRec
|
||||
|
||||
if err := json.Unmarshal(body, &hotelResponse); err != nil {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"unmarshal CMS hotel response: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if hotelResponse.Status.Code != cmstypes.StatusSuccessCode {
|
||||
return reservationSystem, fmt.Errorf(
|
||||
return hotel, fmt.Errorf(
|
||||
"CMS hotel request failed: %s",
|
||||
hotelResponse.Status.Message,
|
||||
)
|
||||
}
|
||||
|
||||
if hotelResponse.TheHotel.Updated <= -1 {
|
||||
return reservationSystem, errors.New(
|
||||
return hotel, errors.New(
|
||||
"hotel record not updated since last read",
|
||||
)
|
||||
}
|
||||
|
||||
return hotelResponse.TheHotel.ReservationSystem, nil
|
||||
return hotelResponse.TheHotel, nil
|
||||
}
|
||||
|
||||
func PaymentSystemIndex(name string) int {
|
||||
|
||||
+5
-6
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -17,7 +16,7 @@ func TestReadHotelSuccess(t *testing.T) {
|
||||
|
||||
expected := cmstypes.ReservationSystemRec{}
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
server := newCMSMockServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf(
|
||||
@@ -157,7 +156,7 @@ func TestReadHotelValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadHotelNotUpdated(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
server := newCMSMockServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, _ *http.Request) {
|
||||
response := cmstypes.HotelResponseRec{}
|
||||
response.Status.Code = cmstypes.StatusSuccessCode
|
||||
@@ -185,7 +184,7 @@ func TestReadHotelNotUpdated(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadHotelHTTPError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
server := newCMSMockServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
@@ -211,7 +210,7 @@ func TestReadHotelHTTPError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadHotelInvalidJSON(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
server := newCMSMockServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
@@ -230,4 +229,4 @@ func TestReadHotelInvalidJSON(t *testing.T) {
|
||||
if !strings.Contains(err.Error(), "unmarshal CMS hotel response") {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package cms
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
||||
)
|
||||
|
||||
func TestReadHotelDetailsRetainsCMSPaymentConfiguration(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
hotel := cmstypes.Hotel{Currency: cmstypes.CurrencyRec{Code: "GBP", NumDecimals: 2}}
|
||||
hotel.Updated = 1
|
||||
hotel.ReservationSystem = cmstypes.ReservationSystemRec{PaymentSystem: cmstypes.PaySystemTrustPayments, PaymentGatewayURL: "https://sandbox.link.trustpayments.com/trustpayments/rest", PaymentGatewaySiteIDEPOS: "SYNTH-SITE", PaymentGatewayPasswordEPOS: "SYNTH-PASSWORD", TimeZone: "Europe/London", PDQs: []cmstypes.PDQRec{{Kiosk: 9, Serial: "SYNTH-PED"}}}
|
||||
server := newCMSMockServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
if r.Method != http.MethodPost || r.URL.Path != cmstypes.APIHotelDetails {
|
||||
t.Errorf("CMS request = %s %s, want existing hotel-details POST", r.Method, r.URL.Path)
|
||||
}
|
||||
var request cmstypes.RequestRec
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if request.Auth.Hotel != "SYNTH-HOTEL" || request.Data != "SYNTH-HOTEL" {
|
||||
t.Error("CMS hotel identity changed")
|
||||
}
|
||||
response := cmstypes.HotelResponseRec{TheHotel: hotel}
|
||||
response.Status.Code = cmstypes.StatusSuccessCode
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
got, err := ReadHotelDetails("SYNTH-HOTEL", server.URL)
|
||||
if err != nil || !reflect.DeepEqual(got, hotel) || calls.Load() != 1 {
|
||||
t.Fatalf("ReadHotelDetails = %+v/%v requests=%d, want complete CMS hotel in one request", got, err, calls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadHotelProjectionPreservesOtherProviderSettings(t *testing.T) {
|
||||
for _, provider := range []int{cmstypes.PaySystemCreditCall, cmstypes.PaySystemDojo, cmstypes.PaySystemPayBridge} {
|
||||
t.Run(cmstypes.PaySystemNames[provider], func(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
response := cmstypes.HotelResponseRec{}
|
||||
response.Status.Code = cmstypes.StatusSuccessCode
|
||||
response.TheHotel.Updated = 1
|
||||
response.TheHotel.Currency = cmstypes.CurrencyRec{Code: "GBP", NumDecimals: 2}
|
||||
response.TheHotel.ReservationSystem = cmstypes.ReservationSystemRec{PaymentSystem: provider, PaymentGatewayURL: "https://synthetic.invalid", PaymentGatewayAPIKeyWeb: "SYNTH-API", PaymentGatewaySiteIDEPOS: "SYNTH-SITE", PaymentGatewayPasswordEPOS: "SYNTH-PASSWORD", PDQs: []cmstypes.PDQRec{{Kiosk: 9, Serial: "SYNTH-PED"}}}
|
||||
server := newCMSMockServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls.Add(1); _ = json.NewEncoder(w).Encode(response) }))
|
||||
defer server.Close()
|
||||
got, err := ReadHotel("SYNTH-HOTEL", server.URL)
|
||||
if err != nil || !reflect.DeepEqual(got, response.TheHotel.ReservationSystem) || calls.Load() != 1 {
|
||||
t.Fatalf("ReadHotel projection = %+v/%v calls=%d, want unchanged provider settings and one request", got, err, calls.Load())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cms
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var cmsMockHosts = struct {
|
||||
sync.RWMutex
|
||||
values map[string]bool
|
||||
}{values: make(map[string]bool)}
|
||||
|
||||
type cmsOfflineTransport struct{ base http.RoundTripper }
|
||||
|
||||
func (t cmsOfflineTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
cmsMockHosts.RLock()
|
||||
allowed := cmsMockHosts.values[r.URL.Host]
|
||||
cmsMockHosts.RUnlock()
|
||||
if !allowed {
|
||||
return nil, errors.New("offline CMS tests reject unregistered endpoints")
|
||||
}
|
||||
return t.base.RoundTrip(r)
|
||||
}
|
||||
|
||||
func newCMSMockServer(handler http.Handler) *httptest.Server {
|
||||
server := httptest.NewServer(handler)
|
||||
cmsMockHosts.Lock()
|
||||
cmsMockHosts.values[server.Listener.Addr().String()] = true
|
||||
cmsMockHosts.Unlock()
|
||||
return server
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
base := http.DefaultTransport.(*http.Transport).Clone()
|
||||
base.Proxy = nil
|
||||
http.DefaultTransport = cmsOfflineTransport{base: base}
|
||||
http.DefaultClient = &http.Client{Transport: http.DefaultTransport}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
Reference in New Issue
Block a user