Skip to content

Commit

Permalink
Merge pull request #13701 from transcom/B-21041-MAIN
Browse files Browse the repository at this point in the history
B 21041 Prime can update existing second/third addresses
  • Loading branch information
pambecker authored Sep 23, 2024
2 parents b4afd6a + 5a108e0 commit 9134dc5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 191 deletions.
6 changes: 3 additions & 3 deletions pkg/handlers/primeapi/mto_shipment_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ func (h UpdateMTOShipmentAddressHandler) Handle(params mtoshipmentops.UpdateMTOS
}

if dbShipment.ShipmentType == models.MTOShipmentTypeHHGIntoNTSDom &&
(*dbShipment.DestinationAddressID == addressID) {
(dbShipment.DestinationAddressID != nil && *dbShipment.DestinationAddressID == addressID) {
return mtoshipmentops.NewUpdateMTOShipmentAddressUnprocessableEntity().WithPayload(payloads.ValidationError(
"Cannot update the destination address of an NTS shipment directly, please update the storage facility address instead", h.GetTraceIDFromRequest(params.HTTPRequest), nil)), err
}

if dbShipment.Status == models.MTOShipmentStatusApproved &&
(*dbShipment.DestinationAddressID == addressID) {
(dbShipment.DestinationAddressID != nil && *dbShipment.DestinationAddressID == addressID) {
return mtoshipmentops.NewUpdateMTOShipmentAddressUnprocessableEntity().WithPayload(payloads.ValidationError(
"This shipment is approved, please use the updateShipmentDestinationAddress endpoint to update the destination address of an approved shipment", h.GetTraceIDFromRequest(params.HTTPRequest), nil)), err
}

if dbShipment.ShipmentType == models.MTOShipmentTypeHHGOutOfNTSDom &&
(*dbShipment.PickupAddressID == addressID) {
(*dbShipment.PickupAddressID != uuid.Nil && *dbShipment.PickupAddressID == addressID) {
return mtoshipmentops.NewUpdateMTOShipmentAddressUnprocessableEntity().WithPayload(payloads.ValidationError(
"Cannot update the pickup address of an NTS-Release shipment directly, please update the storage facility address instead", h.GetTraceIDFromRequest(params.HTTPRequest), nil)), err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/mto_shipment/mto_shipment_address_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func isAddressOnShipment(address *models.Address, mtoShipment *models.MTOShipmen
mtoShipment.DestinationAddressID,
mtoShipment.SecondaryDeliveryAddressID,
mtoShipment.SecondaryPickupAddressID,
mtoShipment.TertiaryDeliveryAddressID,
mtoShipment.TertiaryPickupAddressID,
}

for _, id := range addressIDs {
Expand Down
50 changes: 43 additions & 7 deletions src/components/PrimeUI/Shipment/Shipment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ShipmentShape } from 'types/shipment';
import { primeSimulatorRoutes } from 'constants/routes';
import { ppmShipmentStatuses, shipmentDestinationTypes } from 'constants/shipments';
import styles from 'pages/PrimeUI/MoveTaskOrder/MoveDetails.module.scss';
import { SHIPMENT_OPTIONS } from 'shared/constants';
import { ADDRESS_TYPES, SHIPMENT_OPTIONS } from 'shared/constants';

const Shipment = ({ shipment, moveId, onDelete, mtoServiceItems }) => {
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
Expand Down Expand Up @@ -188,32 +188,68 @@ const Shipment = ({ shipment, moveId, onDelete, mtoServiceItems }) => {
<div className={descriptionListStyles.row}>
<dt>Pickup Address:</dt>
<dd>{formatPrimeAPIShipmentAddress(shipment.pickupAddress)}</dd>
<dd>{shipment.pickupAddress?.id && moveId && <Link to={editShipmentAddressUrl}>Edit</Link>}</dd>
<dd>
{shipment.pickupAddress?.id && moveId && (
<Link to={editShipmentAddressUrl} state={{ addressType: ADDRESS_TYPES.PICKUP }}>
Edit
</Link>
)}
</dd>
</div>
<div className={descriptionListStyles.row}>
<dt>Second Pickup Address:</dt>
<dd>{formatPrimeAPIShipmentAddress(shipment.secondaryPickupAddress)}</dd>
<dd>{shipment.secondaryPickupAddress?.id && moveId && <Link to={editShipmentAddressUrl}>Edit</Link>}</dd>
<dd>
{shipment.secondaryPickupAddress?.id && moveId && (
<Link to={editShipmentAddressUrl} state={{ addressType: ADDRESS_TYPES.SECOND_PICKUP }}>
Edit
</Link>
)}
</dd>
</div>
<div className={descriptionListStyles.row}>
<dt>Third Pickup Address:</dt>
<dd>{formatPrimeAPIShipmentAddress(shipment.tertiaryPickupAddress)}</dd>
<dd>{shipment.tertiaryPickupAddress?.id && moveId && <Link to={editShipmentAddressUrl}>Edit</Link>}</dd>
<dd>
{shipment.tertiaryPickupAddress?.id && moveId && (
<Link to={editShipmentAddressUrl} state={{ addressType: ADDRESS_TYPES.THIRD_PICKUP }}>
Edit
</Link>
)}
</dd>
</div>
<div className={descriptionListStyles.row}>
<dt>Destination Address:</dt>
<dd>{formatPrimeAPIShipmentAddress(shipment.destinationAddress)}</dd>
<dd>{shipment.destinationAddress?.id && moveId && <Link to={editShipmentAddressUrl}>Edit</Link>}</dd>
<dd>
{shipment.destinationAddress?.id && moveId && (
<Link to={editShipmentAddressUrl} state={{ addressType: ADDRESS_TYPES.DESTINATION }}>
Edit
</Link>
)}
</dd>
</div>
<div className={descriptionListStyles.row}>
<dt>Second Destination Address:</dt>
<dd>{formatPrimeAPIShipmentAddress(shipment.secondaryDeliveryAddress)}</dd>
<dd>{shipment.secondaryDeliveryAddress?.id && moveId && <Link to={editShipmentAddressUrl}>Edit</Link>}</dd>
<dd>
{shipment.secondaryDeliveryAddress?.id && moveId && (
<Link to={editShipmentAddressUrl} state={{ addressType: ADDRESS_TYPES.SECOND_DESTINATION }}>
Edit
</Link>
)}
</dd>
</div>
<div className={descriptionListStyles.row}>
<dt>Third Destination Address:</dt>
<dd>{formatPrimeAPIShipmentAddress(shipment.tertiaryDeliveryAddress)}</dd>
<dd>{shipment.tertiaryDeliveryAddress?.id && moveId && <Link to={editShipmentAddressUrl}>Edit</Link>}</dd>
<dd>
{shipment.tertiaryDeliveryAddress?.id && moveId && (
<Link to={editShipmentAddressUrl} state={{ addressType: ADDRESS_TYPES.THIRD_DESTINATION }}>
Edit
</Link>
)}
</dd>
</div>
<div className={descriptionListStyles.row}>
<dt>Destination type:</dt>
Expand Down
100 changes: 39 additions & 61 deletions src/pages/PrimeUI/Shipment/PrimeUIShipmentUpdateAddress.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useNavigate, useParams, generatePath } from 'react-router-dom';
import { useNavigate, useParams, generatePath, useLocation } from 'react-router-dom';
import { useQueryClient, useMutation } from '@tanstack/react-query';
import { Alert, Grid, GridContainer } from '@trussworks/react-uswds';
import * as Yup from 'yup';
Expand All @@ -11,26 +11,24 @@ import { usePrimeSimulatorGetMove } from 'hooks/queries';
import LoadingPlaceholder from 'shared/LoadingPlaceholder';
import SomethingWentWrong from 'shared/SomethingWentWrong';
import { primeSimulatorRoutes } from 'constants/routes';
import { addressSchema } from 'utils/validation';
import { ZIP_CODE_REGEX } from 'utils/validation';
import scrollToTop from 'shared/scrollToTop';
import { updatePrimeMTOShipmentAddress } from 'services/primeApi';
import primeStyles from 'pages/PrimeUI/Prime.module.scss';
import { isEmpty } from 'shared/utils';
import { fromPrimeAPIAddressFormat } from 'utils/formatters';
import { PRIME_SIMULATOR_MOVE } from 'constants/queryKeys';
import { getAddressLabel } from 'shared/constants';

const updatePickupAddressSchema = Yup.object().shape({
const updateAddressSchema = Yup.object().shape({
addressID: Yup.string(),
pickupAddress: Yup.object().shape({
address: addressSchema,
}),
eTag: Yup.string(),
});

const updateDestinationAddressSchema = Yup.object().shape({
addressID: Yup.string(),
destinationAddress: Yup.object().shape({
address: addressSchema,
address: Yup.object().shape({
id: Yup.string(),
streetAddress1: Yup.string().required('Required'),
streetAddress2: Yup.string(),
city: Yup.string().required('Required'),
state: Yup.string().required('Required').length(2, 'Must use state abbreviation'),
postalCode: Yup.string().required('Required').matches(ZIP_CODE_REGEX, 'Must be valid zip code'),
}),
eTag: Yup.string(),
});
Expand All @@ -42,6 +40,11 @@ const PrimeUIShipmentUpdateAddress = () => {
const mtoShipments = moveTaskOrder?.mtoShipments;
const shipment = mtoShipments?.find((mtoShipment) => mtoShipment?.id === shipmentId);
const navigate = useNavigate();
const location = useLocation();

const addressType = location?.state?.addressType;
const addressLabel = getAddressLabel(addressType);
const addressData = shipment ? shipment[addressType] : null;

const handleClose = () => {
navigate(generatePath(primeSimulatorRoutes.VIEW_MOVE_PATH, { moveCodeOrID }));
Expand All @@ -52,12 +55,10 @@ const PrimeUIShipmentUpdateAddress = () => {
onSuccess: (updatedMTOShipmentAddress) => {
const shipmentIndex = mtoShipments.findIndex((mtoShipment) => mtoShipment.id === shipmentId);
let updateQuery = false;
['pickupAddress', 'destinationAddress'].forEach((key) => {
if (updatedMTOShipmentAddress.id === mtoShipments[shipmentIndex][key].id) {
mtoShipments[shipmentIndex][key] = updatedMTOShipmentAddress;
updateQuery = true;
}
});
if (updatedMTOShipmentAddress.id === mtoShipments[shipmentIndex][addressType].id) {
mtoShipments[shipmentIndex][addressType] = updatedMTOShipmentAddress;
updateQuery = true;
}
if (updateQuery) {
moveTaskOrder.mtoShipments = mtoShipments;
queryClient.setQueryData([PRIME_SIMULATOR_MOVE, moveCodeOrID], moveTaskOrder);
Expand Down Expand Up @@ -87,19 +88,16 @@ const PrimeUIShipmentUpdateAddress = () => {
if (isError) return <SomethingWentWrong />;

const onSubmit = (values, { setSubmitting }) => {
// Choose pickupAddress or destinationAddress by the presence of the object
// by the same name. It's possible that these values are blank and set to
// `undefined` or an empty string `""`.
const address = values.pickupAddress ? values.pickupAddress.address : values.destinationAddress.address;
const { streetAddress1, streetAddress2, streetAddress3, city, state, postalCode } = values.address;

const body = {
id: values.addressID,
streetAddress1: address.streetAddress1,
streetAddress2: address.streetAddress2,
streetAddress3: address.streetAddress3,
city: address.city,
state: address.state,
postalCode: address.postalCode,
streetAddress1,
streetAddress2,
streetAddress3,
city,
state,
postalCode,
};

// Check if the address payload contains any blank properties and remove
Expand All @@ -121,24 +119,13 @@ const PrimeUIShipmentUpdateAddress = () => {
});
};

const reformatPrimeApiPickupAddress = fromPrimeAPIAddressFormat(shipment.pickupAddress);
const reformatPrimeApiDestinationAddress = fromPrimeAPIAddressFormat(shipment.destinationAddress);
const editablePickupAddress = !isEmpty(reformatPrimeApiPickupAddress);
const editableDestinationAddress = !isEmpty(reformatPrimeApiDestinationAddress);
const reformatPriApiAddress = fromPrimeAPIAddressFormat(addressData);
const editableAddress = !isEmpty(reformatPriApiAddress);

const initialValuesPickupAddress = {
addressID: shipment.pickupAddress?.id,
pickupAddress: {
address: reformatPrimeApiPickupAddress,
},
eTag: shipment.pickupAddress?.eTag,
};
const initialValuesDestinationAddress = {
addressID: shipment.destinationAddress?.id,
destinationAddress: {
address: reformatPrimeApiDestinationAddress,
},
eTag: shipment.destinationAddress?.eTag,
const initialValues = {
addressID: addressData?.id,
address: reformatPriApiAddress,
eTag: addressData?.eTag,
};

return (
Expand All @@ -155,23 +142,14 @@ const PrimeUIShipmentUpdateAddress = () => {
</Alert>
</div>
)}
<h1>Update Existing Pickup & Destination Address</h1>
{editablePickupAddress && (
<PrimeUIShipmentUpdateAddressForm
initialValues={initialValuesPickupAddress}
onSubmit={onSubmit}
updateShipmentAddressSchema={updatePickupAddressSchema}
addressLocation="Pickup address"
name="pickupAddress.address"
/>
)}
{editableDestinationAddress && (
<h1>Update Existing {`${addressLabel}`}</h1>
{editableAddress && (
<PrimeUIShipmentUpdateAddressForm
initialValues={initialValuesDestinationAddress}
initialValues={initialValues}
onSubmit={onSubmit}
updateShipmentAddressSchema={updateDestinationAddressSchema}
addressLocation="Destination address"
name="destinationAddress.address"
updateShipmentAddressSchema={updateAddressSchema}
addressLocation={addressLabel}
name="address"
/>
)}
</Grid>
Expand Down
Loading

0 comments on commit 9134dc5

Please sign in to comment.