Skip to content

Commit

Permalink
Merge branch 'integrationTesting' into B-21997-INT-Order
Browse files Browse the repository at this point in the history
  • Loading branch information
antgmann authored Jan 16, 2025
2 parents 3a61318 + 9227667 commit d98d2b8
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 108 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ RUN apt-get update
RUN apt-get install -y ca-certificates --no-install-recommends
RUN update-ca-certificates

## Give full perms to tmp for the server to have a read/write location
RUN mkdir -p /tmp && chmod 777 /tmp

# hadolint ignore=DL3007
FROM gcr.io/distroless/base-debian11@sha256:ac69aa622ea5dcbca0803ca877d47d069f51bd4282d5c96977e0390d7d256455
COPY --from=build-env /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

# Pull in full perms tmp
COPY --from=build-env --chown=nonroot:nonroot /tmp /tmp

COPY bin/rds-ca-rsa4096-g1.pem /bin/rds-ca-rsa4096-g1.pem
COPY bin/rds-ca-2019-root.pem /bin/rds-ca-2019-root.pem
COPY bin/milmove /bin/milmove
Expand All @@ -23,6 +29,9 @@ COPY swagger/* /swagger/
COPY build /build
COPY public/static/react-file-viewer /public/static/react-file-viewer

# Keep the tmp!
VOLUME ["/tmp"]

ENTRYPOINT ["/bin/milmove"]

CMD ["serve", "--logging-level=debug"]
Expand Down
11 changes: 11 additions & 0 deletions Dockerfile.dp3
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
FROM debian:stable AS build-env

## Give full perms to tmp for the server to have a read/write location
RUN mkdir -p /tmp && chmod 777 /tmp

# hadolint ignore=DL3007
FROM gcr.io/distroless/base-debian11@sha256:ac69aa622ea5dcbca0803ca877d47d069f51bd4282d5c96977e0390d7d256455

Expand All @@ -6,6 +11,9 @@ COPY bin/rds-ca-rsa4096-g1.pem /bin/rds-ca-rsa4096-g1.pem

COPY bin/milmove /bin/milmove

# Pull in full perms tmp
COPY --from=build-env --chown=nonroot:nonroot /tmp /tmp

# Demo Environment certs
COPY config/tls/api.demo.dp3.us.chain.der.p7b /config/tls/api.demo.dp3.us.chain.der.p7b
COPY config/tls/api.demo.dp3.us.crt /config/tls/api.demo.dp3.us.crt
Expand All @@ -27,6 +35,9 @@ COPY swagger/* /swagger/
COPY build /build
COPY public/static/react-file-viewer /public/static/react-file-viewer

# Keep the tmp!
VOLUME ["/tmp"]

ENTRYPOINT ["/bin/milmove"]

CMD ["serve", "--logging-level=debug"]
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ require (
github.com/lib/pq v1.10.9
github.com/markbates/goth v1.79.0
github.com/namsral/flag v1.7.4-pre
github.com/pdfcpu/pdfcpu v0.6.0
github.com/pdfcpu/pdfcpu v0.9.1
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.7
github.com/pterm/pterm v0.12.79
Expand Down Expand Up @@ -222,7 +222,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
Expand All @@ -237,7 +237,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/peterbourgon/diskv/v3 v3.0.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/zerolog v1.29.0 // indirect
Expand All @@ -259,7 +259,7 @@ require (
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/image v0.18.0 // indirect
golang.org/x/image v0.21.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
Expand Down Expand Up @@ -511,8 +511,8 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/patrickmn/go-cache v0.0.0-20180815053127-5633e0862627 h1:pSCLCl6joCFRnjpeojzOpEYs4q7Vditq8fySFG5ap3Y=
github.com/patrickmn/go-cache v0.0.0-20180815053127-5633e0862627/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pdfcpu/pdfcpu v0.6.0 h1:z4kARP5bcWa39TTYMcN/kjBnm7MvhTWjXgeYmkdAGMI=
github.com/pdfcpu/pdfcpu v0.6.0/go.mod h1:kmpD0rk8YnZj0l3qSeGBlAB+XszHUgNv//ORH/E7EYo=
github.com/pdfcpu/pdfcpu v0.9.1 h1:q8/KlBdHjkE7ZJU4ofhKG5Rjf7M6L324CVM6BMDySao=
github.com/pdfcpu/pdfcpu v0.9.1/go.mod h1:fVfOloBzs2+W2VJCCbq60XIxc3yJHAZ0Gahv1oO0gyI=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/peterbourgon/diskv/v3 v3.0.1 h1:x06SQA46+PKIUftmEujdwSEpIx8kR+M9eLYsUxeYveU=
Expand Down Expand Up @@ -547,8 +547,8 @@ github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTK
github.com/rickar/cal/v2 v2.1.13 h1:FENBPXxDPyL1OWGf9ZdpWGcEiGoSjt0UZED8VOxvK0c=
github.com/rickar/cal/v2 v2.1.13/go.mod h1:/fdlMcx7GjPlIBibMzOM9gMvDBsrK+mOtRXdTzUqV/A=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -730,8 +730,8 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjs
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down
1 change: 1 addition & 0 deletions migrations/app/migrations_manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1074,3 +1074,4 @@
20250110001339_update_nts_release_enum_name.up.sql
20250110153428_add_shipment_address_updates_to_move_history.up.sql
20250110214012_homesafeconnect_cert.up.sql
20250113201232_update_estimated_pricing_procs_add_is_peak_func.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
-- removing ServiceAreaOrigin
DELETE FROM service_params
WHERE service_id = '9f3d551a-0725-430e-897e-80ee9add3ae9'
AND service_item_param_key_id = '599bbc21-8d1d-4039-9a89-ff52e3582144';

-- function that evaluates a date and returns T/F if it is during peak period
CREATE OR REPLACE FUNCTION is_peak_period(input_date DATE) RETURNS BOOLEAN AS $$
DECLARE
peak_start DATE := MAKE_DATE(EXTRACT(YEAR FROM input_date)::INT, 5, 15); -- May 15th of the input year
peak_end DATE := MAKE_DATE(EXTRACT(YEAR FROM input_date)::INT, 9, 30); -- September 30th of the input year
BEGIN
IF input_date IS NULL THEN
RAISE EXCEPTION 'Input date cannot be NULL';
END IF;
-- if the input date is between May 15 and September 30 (inclusive), return true
IF input_date BETWEEN peak_start AND peak_end THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
$$ LANGUAGE plpgsql;


-- adding the is_peak_period check to refine the price query further
CREATE OR REPLACE FUNCTION calculate_escalated_price(
o_rate_area_id UUID,
d_rate_area_id UUID,
re_service_id UUID,
c_id UUID,
service_code TEXT,
requested_pickup_date DATE
) RETURNS NUMERIC AS $$
DECLARE
per_unit_cents NUMERIC;
escalation_factor NUMERIC;
escalated_price NUMERIC;
peak_period BOOLEAN;
BEGIN
-- we need to query the appropriate table based on the service code
-- need to establish if the shipment is being moved during peak period
peak_period := is_peak_period(requested_pickup_date);
IF service_code IN ('ISLH', 'UBP') THEN
SELECT rip.per_unit_cents
INTO per_unit_cents
FROM re_intl_prices rip
WHERE rip.origin_rate_area_id = o_rate_area_id AND rip.destination_rate_area_id = d_rate_area_id
AND rip.service_id = re_service_id
AND rip.contract_id = c_id
AND rip.is_peak_period = peak_period;
ELSE
SELECT riop.per_unit_cents
INTO per_unit_cents
FROM re_intl_other_prices riop
WHERE (riop.rate_area_id = o_rate_area_id OR riop.rate_area_id = d_rate_area_id OR
(o_rate_area_id IS NULL AND d_rate_area_id IS NULL))
AND riop.service_id = re_service_id
AND riop.contract_id = c_id
AND riop.is_peak_period = peak_period;
END IF;

RAISE NOTICE '% per unit cents: %', service_code, per_unit_cents;
IF per_unit_cents IS NULL THEN
RAISE EXCEPTION 'No per unit cents found for service item id: %, origin rate area: %, dest rate area: %, and contract_id: %', re_service_id, o_rate_area_id, d_rate_area_id, c_id;
END IF;

SELECT rcy.escalation_compounded
INTO escalation_factor
FROM re_contract_years rcy
WHERE rcy.contract_id = c_id
AND requested_pickup_date BETWEEN rcy.start_date AND rcy.end_date;

IF escalation_factor IS NULL THEN
RAISE EXCEPTION 'Escalation factor not found for contract_id %', c_id;
END IF;
-- calculate the escalated price, return in dollars (dividing by 100)
per_unit_cents := per_unit_cents / 100; -- putting in dollars
escalated_price := ROUND(per_unit_cents * escalation_factor, 2); -- rounding to two decimals (100.00)

RETURN escalated_price;
END;
$$ LANGUAGE plpgsql;

DROP FUNCTION IF EXISTS get_fuel_price(date);


-- updating get_fuel_price to return an INT instead of decimal, we were rounding too soon
CREATE OR REPLACE FUNCTION get_fuel_price(requested_pickup_date DATE)
RETURNS INTEGER AS $$
DECLARE
fuel_price_in_cents INTEGER;
BEGIN

SELECT fuel_price_in_millicents
INTO fuel_price_in_cents
FROM ghc_diesel_fuel_prices
WHERE requested_pickup_date BETWEEN effective_date AND end_date;

-- fallback to most recent fuel price if no match
IF fuel_price_in_cents IS NULL THEN
SELECT fuel_price_in_millicents
INTO fuel_price_in_cents
FROM ghc_diesel_fuel_prices
ORDER BY publication_date DESC
LIMIT 1;
END IF;

IF fuel_price_in_cents IS NULL THEN
RAISE EXCEPTION 'No fuel price found for requested_pickup_date: %', requested_pickup_date;
END IF;

RAISE NOTICE 'Received fuel price of % for requested_pickup_date: %', fuel_price_in_cents, requested_pickup_date;

RETURN fuel_price_in_cents;
END;
$$ LANGUAGE plpgsql;

-- updating to subtract the millicents value to avoid premature rounding
CREATE OR REPLACE FUNCTION calculate_price_difference(fuel_price DECIMAL)
RETURNS DECIMAL AS $$
BEGIN
RETURN (fuel_price - 250000)::DECIMAL / 1000;
END;
$$ LANGUAGE plpgsql;

-- updating to use the shipment.requested_pickup_date value to refine search to get more accurate prices
CREATE OR REPLACE PROCEDURE update_service_item_pricing(
shipment_id UUID,
mileage INT
) AS
'
DECLARE
shipment RECORD;
service_item RECORD;
escalated_price NUMERIC;
estimated_price NUMERIC;
o_rate_area_id UUID;
d_rate_area_id UUID;
contract_id UUID;
service_code TEXT;
o_zip_code TEXT;
d_zip_code TEXT;
distance NUMERIC; -- This will be replaced by mileage
estimated_fsc_multiplier NUMERIC;
fuel_price NUMERIC;
cents_above_baseline NUMERIC;
price_difference NUMERIC;
BEGIN
SELECT ms.id, ms.pickup_address_id, ms.destination_address_id, ms.requested_pickup_date, ms.prime_estimated_weight
INTO shipment
FROM mto_shipments ms
WHERE ms.id = shipment_id;
IF shipment IS NULL THEN
RAISE EXCEPTION ''Shipment with ID % not found'', shipment_id;
END IF;
-- exit the proc if prime_estimated_weight is NULL
IF shipment.prime_estimated_weight IS NULL THEN
RETURN;
END IF;
-- loop through service items in the shipment
FOR service_item IN
SELECT si.id, si.re_service_id
FROM mto_service_items si
WHERE si.mto_shipment_id = shipment_id
LOOP
-- get the service code for the current service item to determine calculation
SELECT code
INTO service_code
FROM re_services
WHERE id = service_item.re_service_id;
CASE
WHEN service_code IN (''ISLH'', ''UBP'') THEN
contract_id := get_contract_id(shipment.requested_pickup_date);
o_rate_area_id := get_rate_area_id(shipment.pickup_address_id, service_item.re_service_id, contract_id);
d_rate_area_id := get_rate_area_id(shipment.destination_address_id, service_item.re_service_id, contract_id);
escalated_price := calculate_escalated_price(o_rate_area_id, d_rate_area_id, service_item.re_service_id, contract_id, service_code, shipment.requested_pickup_date);
IF shipment.prime_estimated_weight IS NOT NULL THEN
estimated_price := ROUND((escalated_price * shipment.prime_estimated_weight / 100), 2) * 100;
RAISE NOTICE ''%: Received estimated price of % (% * (% / 100)) cents'', service_code, estimated_price, escalated_price, shipment.prime_estimated_weight;
-- update the pricing_estimate value in mto_service_items
UPDATE mto_service_items
SET pricing_estimate = estimated_price
WHERE id = service_item.id;
END IF;
WHEN service_code IN (''IHPK'', ''IUBPK'') THEN
-- perform IHPK/IUBPK-specific logic (no destination rate area)
contract_id := get_contract_id(shipment.requested_pickup_date);
o_rate_area_id := get_rate_area_id(shipment.pickup_address_id, service_item.re_service_id, contract_id);
escalated_price := calculate_escalated_price(o_rate_area_id, NULL, service_item.re_service_id, contract_id, service_code, shipment.requested_pickup_date);
IF shipment.prime_estimated_weight IS NOT NULL THEN
estimated_price := ROUND((escalated_price * shipment.prime_estimated_weight / 100), 2) * 100;
RAISE NOTICE ''%: Received estimated price of % (% * (% / 100)) cents'', service_code, estimated_price, escalated_price, shipment.prime_estimated_weight;
-- update the pricing_estimate value in mto_service_items
UPDATE mto_service_items
SET pricing_estimate = estimated_price
WHERE id = service_item.id;
END IF;
WHEN service_code IN (''IHUPK'', ''IUBUPK'') THEN
-- perform IHUPK/IUBUPK-specific logic (no origin rate area)
contract_id := get_contract_id(shipment.requested_pickup_date);
d_rate_area_id := get_rate_area_id(shipment.destination_address_id, service_item.re_service_id, contract_id);
escalated_price := calculate_escalated_price(NULL, d_rate_area_id, service_item.re_service_id, contract_id, service_code, shipment.requested_pickup_date);
IF shipment.prime_estimated_weight IS NOT NULL THEN
estimated_price := ROUND((escalated_price * shipment.prime_estimated_weight / 100), 2) * 100;
RAISE NOTICE ''%: Received estimated price of % (% * (% / 100)) cents'', service_code, estimated_price, escalated_price, shipment.prime_estimated_weight;
-- update the pricing_estimate value in mto_service_items
UPDATE mto_service_items
SET pricing_estimate = estimated_price
WHERE id = service_item.id;
END IF;
WHEN service_code IN (''POEFSC'', ''PODFSC'') THEN
distance = mileage;
-- getting FSC multiplier from re_fsc_multipliers
estimated_fsc_multiplier := get_fsc_multiplier(shipment.prime_estimated_weight);
fuel_price := get_fuel_price(shipment.requested_pickup_date);
price_difference := calculate_price_difference(fuel_price);
-- calculate estimated price, return as cents
IF estimated_fsc_multiplier IS NOT NULL AND distance IS NOT NULL THEN
cents_above_baseline := distance * estimated_fsc_multiplier;
RAISE NOTICE ''Distance: % * FSC Multipler: % = $% cents above baseline of $2.50'', distance, estimated_fsc_multiplier, cents_above_baseline;
RAISE NOTICE ''The fuel price is % above the baseline (% - 250000 baseline)'', price_difference, fuel_price;
estimated_price := ROUND((cents_above_baseline * price_difference) * 100);
RAISE NOTICE ''Received estimated price of % cents for service_code: %.'', estimated_price, service_code;
-- update the pricing_estimate value in mto_service_items
UPDATE mto_service_items
SET pricing_estimate = estimated_price
WHERE id = service_item.id;
END IF;
END CASE;
END LOOP;
END;
'
LANGUAGE plpgsql;
Loading

0 comments on commit d98d2b8

Please sign in to comment.