Skip to content

Commit

Permalink
feat: intra org shipment WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fhenrich33 committed Nov 9, 2024
1 parent eb38d63 commit f5ccd75
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 92 deletions.
2 changes: 1 addition & 1 deletion front/src/types/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,7 @@ export type AllAcceptedTransferAgreementsQuery = { __typename?: 'Query', base?:
export type CreateShipmentMutationVariables = Exact<{
sourceBaseId: Scalars['Int']['input'];
targetBaseId: Scalars['Int']['input'];
transferAgreementId: Scalars['Int']['input'];
transferAgreementId?: InputMaybe<Scalars['Int']['input']>;
}>;


Expand Down
25 changes: 18 additions & 7 deletions front/src/views/Transfers/CreateShipment/CreateShipmentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const ALL_ACCEPTED_TRANSFER_AGREEMENTS_QUERY = gql`

export const CREATE_SHIPMENT_MUTATION = gql`
${SHIPMENT_FIELDS_FRAGMENT}
mutation CreateShipment($sourceBaseId: Int!, $targetBaseId: Int!, $transferAgreementId: Int!) {
mutation CreateShipment($sourceBaseId: Int!, $targetBaseId: Int!, $transferAgreementId: Int) {
createShipment(
creationInput: {
sourceBaseId: $sourceBaseId
Expand Down Expand Up @@ -109,6 +109,7 @@ function CreateShipmentView() {
const currentBase = allAcceptedTransferAgreements?.data?.base;
const currentOrganisationLabel = `${currentBase?.organisation?.name} - ${currentBase?.name}`;
const currentOrganisationId = globalPreferences.organisation?.id;
const currentOrganisationBases = globalPreferences.availableBases;
const acceptedTransferAgreementsPartnerData =
allAcceptedTransferAgreements.data?.transferAgreements
?.filter(
Expand Down Expand Up @@ -167,15 +168,19 @@ function CreateShipmentView() {
// Handle Submission
const onSubmitCreateShipmentForm = useCallback(
(createShipmentFormData: ICreateShipmentFormData) => {
console.log(createShipmentFormData);
// Find the possible agreement Ids for the partner base
const agreementIds: Array<string> =
acceptedTransferAgreementsPartnerData
?.filter((org) =>
org.bases.some((base) => base.id === createShipmentFormData.receivingBase.value),
)
.map((org) => org.agreementId) || [];
createShipmentFormData.shipmentTarget === "currentOrg"
? []
: acceptedTransferAgreementsPartnerData
?.filter((org) =>
org.bases.some((base) => base.id === createShipmentFormData.receivingBase.value),
)
.map((org) => org.agreementId) || [];

if (agreementIds.length === 0) {
// Valid to not have agreements for intra org shipments.
if (agreementIds.length === 0 && createShipmentFormData.shipmentTarget === "partners") {
triggerError({
message: "Error while trying to create a new shipment",
});
Expand Down Expand Up @@ -227,6 +232,7 @@ function CreateShipmentView() {
return <APILoadingIndicator />;
}

// Valid to not have agreements for intra org shipments.
const renderNoAcceptedAgreementsAlert = (
<Alert status="warning">
<AlertIcon />
Expand All @@ -244,14 +250,17 @@ function CreateShipmentView() {
</Alert>
);

// Valid to not have agreements for intra org shipments.
const noAcceptedAgreements = allAcceptedTransferAgreements.data?.transferAgreements.length === 0;
const noPartnerOrgBaseData =
!partnerOrganisationBaseData || partnerOrganisationBaseData.length === 0;

// Valid to not have agreements for intra org shipments.
if (noAcceptedAgreements) {
return renderNoAcceptedAgreementsAlert;
}

// Valid to not have agreements for intra org shipments.
if (noPartnerOrgBaseData || allAcceptedTransferAgreements.error) {
return renderErrorAlert;
}
Expand All @@ -262,7 +271,9 @@ function CreateShipmentView() {
<Center>
<CreateShipment
isLoading={createShipmentMutationState.loading}
currentOrganisationId={currentOrganisationId || ""}
currentOrganisationLabel={currentOrganisationLabel}
currentOrganisationBases={currentOrganisationBases || []}
organisationBaseData={partnerOrganisationBaseData}
onSubmit={onSubmitCreateShipmentForm}
/>
Expand Down
Loading

0 comments on commit f5ccd75

Please sign in to comment.