Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main b 22692 #14954

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions migrations/app/ddl_functions_manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# If a migration is not recorded here, then it will error.
# Naming convention: fn_some_function.up.sql running <generate-ddl-migration some_function functions> will create this file.
20250223023132_fn_get_counseling_offices.up.sql
20250225183302_fn_prime_acknowledge_moves_shipments.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--B-22692 Brian Manley added prime_acknowledge_moves_shipments
CREATE OR REPLACE PROCEDURE prime_acknowledge_moves_shipments(json_data jsonb)
LANGUAGE plpgsql
AS '
DECLARE
v_move_record jsonb;
v_shipment_record jsonb;
v_log_message text;
v_move_id UUID;
v_move_prime_acknowledged_at TIMESTAMP;
v_shipment_id UUID;
v_shipment_prime_acknowledged_at TIMESTAMP;
v_move_exists BOOLEAN;
v_shipment_exists BOOLEAN;
BEGIN
RAISE NOTICE ''Starting prime_acknowledge_moves_shipments procedure'';

-- Check if the input JSON is null or empty
IF json_data IS NULL OR json_data = ''[]''::jsonb THEN
RAISE WARNING ''Input JSON is null or empty'';
RETURN;
END IF;

-- Loop through each move in the JSON array
FOR v_move_record IN SELECT jsonb_array_elements(json_data)
LOOP
-- Check if the move exists in the moves table
v_move_id := NULLIF(v_move_record->>''id'', '''')::UUID;
SELECT EXISTS(SELECT 1 FROM moves WHERE id = v_move_id) INTO v_move_exists;
IF NOT v_move_exists THEN
v_log_message := format(''Move with id %s does not exist in the moves table'', v_move_id);
RAISE WARNING ''%'', v_log_message;
CONTINUE;
END IF;

v_move_prime_acknowledged_at := NULLIF(v_move_record->>''primeAcknowledgedAt'', '''')::TIMESTAMP;
IF v_move_prime_acknowledged_at IS NOT NULL THEN
-- Update the moves table only if the existing prime_acknowledged_at value is NULL
UPDATE moves
SET prime_acknowledged_at = v_move_prime_acknowledged_at
WHERE id = v_move_id
AND prime_acknowledged_at IS NULL;

-- Check if the update affected any rows
IF NOT FOUND THEN
v_log_message := format(''Move not updated (prime_acknowledged_at already set): %s'', v_move_id);
RAISE WARNING ''%'', v_log_message;
ELSE
v_log_message := format(''Successfully updated moves.prime_acknowledged_at value to %s for id %s'', v_move_prime_acknowledged_at, v_move_id);
RAISE NOTICE ''%'', v_log_message;
END IF;
END IF;

-- Check if mtoShipments exists and is an array
IF jsonb_typeof(v_move_record->''mtoShipments'') = ''array'' THEN
-- Loop through each shipment in the mtoShipments array
FOR v_shipment_record IN SELECT jsonb_array_elements(v_move_record->''mtoShipments'')
LOOP
v_shipment_id := NULLIF(v_shipment_record->>''id'', '''')::UUID;
SELECT EXISTS(SELECT 1 FROM mto_shipments WHERE id = v_shipment_id and move_id = v_move_id) INTO v_shipment_exists;
IF NOT v_shipment_exists THEN
v_log_message := format(''Shipment with id %s and move_id %s does not exist in the mto_shipments table'', v_shipment_id, v_move_id);
RAISE WARNING ''%'', v_log_message;
CONTINUE;
END IF;

v_shipment_prime_acknowledged_at := NULLIF(v_shipment_record->>''primeAcknowledgedAt'', '''')::TIMESTAMP;
IF v_shipment_prime_acknowledged_at IS NOT NULL THEN
-- Update the mto_shipments table only if the existing prime_acknowledged_at column is NULL
UPDATE mto_shipments
SET prime_acknowledged_at = v_shipment_prime_acknowledged_at
WHERE id = v_shipment_id
AND prime_acknowledged_at IS NULL;

-- Check if the update affected any rows
IF NOT FOUND THEN
v_log_message := format(''Shipment not updated (prime_acknowledged_at already set): %s'', v_shipment_id);
RAISE WARNING ''%'', v_log_message;
ELSE
v_log_message := format(''Successfully updated mto_shipments.prime_acknowledged_at value to %s for id %s'', v_shipment_prime_acknowledged_at, v_shipment_id);
RAISE NOTICE ''%'', v_log_message;
END IF;
END IF;
END LOOP;
END IF;
END LOOP;
END;
';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE public.moves
ADD COLUMN IF NOT EXISTS prime_acknowledged_at TIMESTAMPTZ NULL;

COMMENT ON COLUMN public.moves.prime_acknowledged_at IS 'The date and time the prime acknowledged the move';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE public.mto_shipments
ADD COLUMN IF NOT EXISTS prime_acknowledged_at TIMESTAMPTZ NULL;

COMMENT ON COLUMN public.mto_shipments.prime_acknowledged_at IS 'The date and time the prime acknowledged the shipment';
2 changes: 2 additions & 0 deletions migrations/app/ddl_tables_manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
20250221195354_tbl_alter_entitlements_B-22651.up.sql
20250224162949_B-22706_tbl_update_moves.up.sql
20250224200700_tbl_ppm_shipments.up.sql
20250225182052_tbl_B-22692_alter_moves.up.sql
20250225183010_tbl_B-22692_alter_mto_shipments.up.sql
20250227211221_tbl_re_country_prn_divisions.up.sql
1 change: 1 addition & 0 deletions pkg/models/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type Move struct {
TOODestinationAssignedUser *OfficeUser `json:"too_destination_assigned" belongs_to:"office_users" fk_id:"too_destination_assigned_id"`
CounselingOfficeID *uuid.UUID `json:"counseling_transportation_office_id" db:"counseling_transportation_office_id"`
CounselingOffice *TransportationOffice `json:"counseling_transportation_office" belongs_to:"transportation_offices" fk_id:"counseling_transportation_office_id"`
PrimeAcknowledgedAt *time.Time `db:"prime_acknowledged_at"`
}

type MoveWithEarliestDate struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/mto_shipments.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type MTOShipment struct {
DestinationSITAuthEndDate *time.Time `json:"destination_sit_auth_end_date" db:"dest_sit_auth_end_date"`
MobileHome *MobileHome `json:"mobile_home" has_one:"mobile_home" fk_id:"shipment_id"`
MarketCode MarketCode `json:"market_code" db:"market_code"`
PrimeAcknowledgedAt *time.Time `db:"prime_acknowledged_at"`
}

// TableName overrides the table name used by Pop.
Expand Down
Loading