Skip to content

Commit

Permalink
No mandate match (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr authored Sep 13, 2024
2 parents 62808fc + 3674ad8 commit 692a5df
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 4 deletions.
37 changes: 36 additions & 1 deletion src/Geopilot.Frontend/cypress/e2e/delivery.cy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadWithoutAuth, loginAsUploader } from "./helpers/appHelpers.js";
import { clickCancel } from "./helpers/buttonHelpers.js";
import { hasError, setInput, setSelect, toggleCheckbox } from "./helpers/formHelpers.js";
import { evaluateSelect, hasError, isDisabled, setInput, setSelect, toggleCheckbox } from "./helpers/formHelpers.js";

const fileNameExists = (filePath, success) => {
const fileName = filePath.split("/").pop();
Expand Down Expand Up @@ -243,6 +243,7 @@ describe("Delivery tests", () => {
it("can submit a delivery", () => {
mockValidationSuccess();
mockMandates();
cy.intercept({ url: "/api/v1/delivery?mandateId=*", method: "GET" }).as("predecessors");
cy.intercept({ url: "/api/v1/delivery", method: "POST" }, req => {
req.reply({
statusCode: 201,
Expand Down Expand Up @@ -325,14 +326,23 @@ describe("Delivery tests", () => {
stepIsCompleted("validate");
stepIsActive("submit");
cy.wait("@mandates");
cy.wait(500); // Wait for the select to be populated and enabled

cy.get('[data-cy="createDelivery-button"]').should("be.disabled");
isDisabled("mandate", false);
isDisabled("predecessor", true);
setSelect("mandate", 1, 4);
cy.wait("@predecessors");
isDisabled("predecessor", false);
cy.get('[data-cy="createDelivery-button"]').should("be.enabled");
setSelect("predecessor", 1);
setSelect("mandate", 0);
isDisabled("predecessor", true);
cy.get('[data-cy="createDelivery-button"]').should("be.disabled");
hasError("mandate");
setSelect("mandate", 2);
cy.wait("@predecessors");
evaluateSelect("predecessor", "");
setSelect("predecessor", 2);
setSelect("predecessor", 0);
hasError("predecessor", false);
Expand Down Expand Up @@ -365,8 +375,11 @@ describe("Delivery tests", () => {
cy.wait("@validation");
stepIsCompleted("validate");
stepIsActive("submit");
cy.wait("@mandates");
cy.wait(1000); // Wait for the select to be populated and enabled

setSelect("mandate", 1);
cy.wait("@predecessors");
setSelect("predecessor", 1);
toggleCheckbox("isPartial");
setInput("comment", "This is a test comment.");
Expand Down Expand Up @@ -437,4 +450,26 @@ describe("Delivery tests", () => {
cy.wait("@deliveryRequest").its("response.statusCode").should("eq", 404);
stepHasError("submit", true, "Not found");
});

it("displays error if no mandates were found", () => {
mockValidationSuccess();
cy.intercept({ url: "/api/v1/mandate?jobId=d49ba857-5db5-45a0-b838-9d41cc7d8d64", method: "GET" }, req => {
req.reply({
statusCode: 200,
body: [],
delay: 500,
});
}).as("mandates");

loginAsUploader();
addFile("deliveryFiles/ilimodels_valid.xml", true);
uploadFile();
cy.wait("@upload");
cy.wait("@validation");
cy.wait("@mandates");

stepHasError("submit", true, "No suitable mandate was found for your delivery");
isDisabled("mandate", true);
resetDelivery("submit");
});
});
30 changes: 30 additions & 0 deletions src/Geopilot.Frontend/cypress/e2e/helpers/formHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ export const hasError = (fieldName, hasError = true, parent) => {
}
};

/**
* Checks if a form element is disabled.
* @param {string} fieldName The name of the form element.
* @param {boolean} isDisabled The expected disabled state.
* @param {string} parent (optional) The parent of the form element.
*/
export const isDisabled = (fieldName, isDisabled = true, parent) => {
const selector = createBaseSelector(parent) + `[data-cy^="${fieldName}-form"] .Mui-disabled`;
if (isDisabled) {
cy.get(selector).should("exist");
} else {
cy.get(selector).should("not.exist");
}
};

/**
* Sets the value for an input form element.
* @param {string} fieldName The name of the input field.
Expand Down Expand Up @@ -75,6 +90,21 @@ export const setSelect = (fieldName, index, expected, parent) => {
selectDropdownOption(index);
};

/**
* Evaluates the state of a select form element.
* @param {string} fieldName The name of the select field.
* @param {string} expectedValue The expected value of the select.
* @param {string} parent (optional) The parent of the form element.
*/
export const evaluateSelect = (fieldName, expectedValue, parent) => {
var selector = createBaseSelector(parent) + `[data-cy="${fieldName}-formSelect"] input`;
cy.get(selector)
.filter((k, input) => {
return input.value === expectedValue;
})
.should("have.length", 1);
};

/**
* Toggles the checkbox for a checkbox form element.
* @param {string} fieldName The name of the checkbox field.
Expand Down
1 change: 1 addition & 0 deletions src/Geopilot.Frontend/public/locale/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"mandates": "Mandate",
"mandatesLoadingError": "Beim Laden der Mandate ist ein Fehler aufgetreten: {{error}}",
"name": "Name",
"noMandatesFound": "Es wurde kein passendes Mandat für Ihre Lieferung gefunden.",
"or": "oder",
"organisationDisconnect": "Möchten Sie die Organisation wirklich inaktiv setzen? Die Organisation wird von allen Mandaten und Benutzer:innen getrennt. Diese Aktion kann nicht rückgängig gemacht werden.",
"organisationSaveError": "Beim Speichern der Organisation ist ein Fehler aufgetreten: {{error}}",
Expand Down
1 change: 1 addition & 0 deletions src/Geopilot.Frontend/public/locale/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"mandates": "Mandates",
"mandatesLoadingError": "An error occurred while loading the mandates: {{error}}",
"name": "Name",
"noMandatesFound": "No suitable mandate was found for your delivery.",
"or": "or",
"organisationDisconnect": "Do you really want to disconnect the organisation? This will remove all connections to mandates and users, and cannot be undone.",
"organisationSaveError": "An error occurred while saving the organisation: {{error}}",
Expand Down
1 change: 1 addition & 0 deletions src/Geopilot.Frontend/public/locale/fr/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"mandates": "Mandats",
"mandatesLoadingError": "Une erreur s'est produite lors du chargement des mandats: {{error}}",
"name": "Nom",
"noMandatesFound": "Aucun mandat approprié n'a été trouvé pour leur livraison.",
"or": "ou",
"organisationDisconnect": "Voulez-vous vraiment déconnecter l'organisation? Cette action supprime toutes les connexions avec les mandats et les utilisateurs. Cette action ne peut pas être annulée.",
"organisationSaveError": "Une erreur s'est produite lors de l'enregistrement de l'organisation: {{error}}",
Expand Down
1 change: 1 addition & 0 deletions src/Geopilot.Frontend/public/locale/it/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"mandates": "Mandati",
"mandatesLoadingError": "Si è verificato un errore durante il caricamento dei mandati: {{error}}",
"name": "Nome",
"noMandatesFound": "Non è stato trovato un mandato adatto per la vostra consegna.",
"or": "o",
"organisationDisconnect": "Volete davvero scollegare l'organizzazione?Questa operazione rimuove tutti i collegamenti ai mandati e agli utenti, e non può essere annullata.",
"organisationSaveError": "Si è verificato un errore durante il salvataggio dell'organizzazione: {{error}}",
Expand Down
18 changes: 15 additions & 3 deletions src/Geopilot.Frontend/src/pages/delivery/deliverySubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ import { FormCheckbox, FormInput, FormSelect } from "../../components/form/form.
import SendIcon from "@mui/icons-material/Send";
import { Delivery, Mandate } from "../../api/apiInterfaces.ts";
import { useApi } from "../../api";
import { DeliverySubmitData } from "./deliveryInterfaces.tsx";
import { DeliveryStepEnum, DeliverySubmitData } from "./deliveryInterfaces.tsx";
import { BaseButton, CancelButton } from "../../components/buttons.tsx";
import { useTranslation } from "react-i18next";

export const DeliverySubmit = () => {
const { t } = useTranslation();
const { enabled, user, login } = useGeopilotAuth();
const formMethods = useForm({ mode: "all" });
const { fetchApi } = useApi();
const { validationResponse, isLoading, submitDelivery, resetDelivery } = useContext(DeliveryContext);
const { setStepError, validationResponse, isLoading, submitDelivery, resetDelivery } = useContext(DeliveryContext);
const [mandates, setMandates] = useState<Mandate[]>([]);
const [previousDeliveries, setPreviousDeliveries] = useState<Delivery[]>([]);

useEffect(() => {
if (validationResponse?.jobId && user) {
fetchApi<Mandate[]>("/api/v1/mandate?" + new URLSearchParams({ jobId: validationResponse.jobId })).then(
setMandates,
mandates => {
if (mandates.length === 0) {
setStepError(DeliveryStepEnum.Submit, t("noMandatesFound"));
}
setMandates(mandates);
},
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -34,6 +41,9 @@ export const DeliverySubmit = () => {
fetchApi<Delivery[]>("/api/v1/delivery?" + new URLSearchParams({ mandateId: mandateId })).then(
setPreviousDeliveries,
);
} else {
setPreviousDeliveries([]);
formMethods.setValue("predecessor", undefined);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [formMethods.getValues()["mandate"]]);
Expand All @@ -54,13 +64,15 @@ export const DeliverySubmit = () => {
fieldName="mandate"
label="mandate"
required={true}
disabled={mandates.length === 0}
values={mandates
?.sort((a, b) => a.name.localeCompare(b.name))
.map(mandate => ({ key: mandate.id, name: mandate.name }))}
/>
<FormSelect
fieldName="predecessor"
label="predecessor"
disabled={previousDeliveries.length === 0}
values={previousDeliveries.map(delivery => ({ key: delivery.id, name: delivery.date.toLocaleString() }))}
/>
</FlexRowSpaceBetweenBox>
Expand Down

0 comments on commit 692a5df

Please sign in to comment.