Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
feat: psy admin form : format input values + update fields (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineBda authored May 23, 2022
1 parent a99c0a1 commit 1944469
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 264 deletions.
2 changes: 1 addition & 1 deletion config/keycloak/mon-psy-sante-realm.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@
"groups": [
{
"id": "5b4a2802-26c7-457d-9977-c1ab23e02ab4",
"name": "admin",
"name": "CPAM",
"path": "/admin",
"attributes": {},
"realmRoles": [
Expand Down
98 changes: 98 additions & 0 deletions src/__tests__/psychologists-update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { stub } from "sinon";

import { models } from "../db/models";
import { getOnePsychologist } from "../db/seeds/psychologist";
import { updateIfExists } from "../pages/api/admin/psychologists/[id]";
import * as address from "../services/getAddressCoordinates";
import { Psychologist } from "../types/psychologist";

describe("updateIfExists", () => {
let getAddressCoordinatesStub;
beforeAll(async () => {
await models.Psychologist.destroy({ where: {} });

const psy = await getOnePsychologist({ id: 1, department: "01" });
// @ts-ignore
await models.Psychologist.create(psy);
});
beforeEach(() => {
getAddressCoordinatesStub = stub(address, "default");
});

afterEach(() => {
getAddressCoordinatesStub.restore();
});
const valideInput = {
address: "My new adress",
addressAdditional: "Mon complément d'adresse",
cdsmsp: "optio",
displayEmail: true,
email: "updated@example.net",
firstName: "CaRmen",
lastName: "strOMan",
phone: "02 71 94 65 55",
displayPhone: false,
public: "Adultes et enfants/adolescents",
teleconsultation: true,
visible: true,
website: "grotesque-proximity.info",
shouldBeIgnored: "should not failed",
};

it("update should undefined if does not exists", async () => {
expect(await updateIfExists("1111", "", {})).toEqual(undefined);
});
it("update should undefined if the department does not match", async () => {
expect(await updateIfExists("1111", "99", {})).toEqual(undefined);
});
it("update should return error if wrong params", async () => {
let exception;
await updateIfExists("1", "01", {}).catch((e) => (exception = e));
expect(exception.details.length).toEqual(9);
expect(exception.details[0].message).toEqual('"address" is required');
});
it("update should update psy in db", async () => {
let exception;
getAddressCoordinatesStub.returns({ latitude: 456, longitude: 123 });

const result = await updateIfExists("1", "01", valideInput).catch(
(e) => (exception = e)
);
expect(exception).toEqual(undefined);
expect(result).toEqual([1]);

// @ts-ignore
const updatedPsy: Psychologist = await models.Psychologist.findOne({
raw: true,
where: { email: valideInput.email },
});
expect(updatedPsy.firstName).toEqual("Carmen");
expect(updatedPsy.lastName).toEqual("STROMAN");
expect(updatedPsy.phone).toEqual("02 71 94 65 55");
expect(updatedPsy.displayPhone).toEqual(false);
expect(updatedPsy.public).toEqual("Adultes et enfants/adolescents");
expect(updatedPsy.addressAdditional).toEqual("Mon complément d'adresse");
expect(updatedPsy.coordinates.coordinates).toEqual([123, 456]);
expect(updatedPsy.coordinates.type).toEqual("Point");

// @ts-ignore
expect(updatedPsy.shouldBeIgnored).toEqual(undefined);
});
it("update should coordinate with null if API returns null", async () => {
let exception;
getAddressCoordinatesStub.returns(null);

const result = await updateIfExists("1", "01", valideInput).catch(
(e) => (exception = e)
);
expect(exception).toEqual(undefined);
expect(result).toEqual([1]);

// @ts-ignore
const updatedPsy: Psychologist = await models.Psychologist.findOne({
raw: true,
where: { email: valideInput.email },
});
expect(updatedPsy.coordinates).toEqual(null);
});
});
173 changes: 135 additions & 38 deletions src/components/Admin/PsychologistForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,55 @@ import { Psychologist } from "../../types/psychologist";
const editableFields = [
{
field: "visible",
label: "Actuellement disponible",
label: "Disponibilité du psychologue :",
legend: "En indisponible les modalités de contact ne seront plus visible",
type: "boolean",
options: [
{ label: "Disponible", value: true },
{ label: "Indisponible", value: false },
],
required: true,
},
{ field: "lastName", label: "Nom", required: true },
{ field: "firstName", label: "Prénom", required: true },
{ field: "address", label: "Adresse", required: true },
{ field: "addressAdditional", label: "Complément d'adresse" },
{ field: "secondAddress", label: "Adresse secondaire" },
{
field: "secondAddressAdditional",
label: "Complément d'adresse secondaire",
field: "address",
label: "Adresse postale du cabinet principal",
legend:
"Préciser ici : numéro, libellé de la voie, code postal, et ville. Cette adresse doit se situer dans le même département que la CPAM. Pour les séances au domicile du patient, noter le code postal + ville.",
required: true,
},
{ field: "phone", label: "Téléphone", required: true },
{
field: "displayPhone",
label: "Afficher le téléphone",
type: "boolean",
field: "addressAdditional",
label: "Informations complémentaires (cabinet principal)",
legend: "Informations complémentaires (cabinet principal)",
},
{
field: "secondAddress",
label: "Adresse postale d'un second lieu d'exercice",
legend:
"Bien préciser : numéro et libellé de la voie, code postal, et ville",
},
{
field: "secondAddressAdditional",
label: "Informations complémentaires (second lieu)",
},
{ field: "phone", label: "Téléphone", required: true },
{ field: "email", label: "Email" },
{
field: "displayEmail",
label: "Afficher l'email",
label: "Afficher l'email :",
type: "boolean",
options: [
{ label: "Oui", value: true },
{ label: "Non", value: false },
],
},
{
field: "website",
label: "Site web",
legend:
"Préciser ici l'url complet du site internet en commençant par http:// ou https://",
},
{
field: "public",
Expand All @@ -49,18 +75,37 @@ const editableFields = [
required: true,
type: "select",
},
{ field: "languages", label: "Langue(s) parlée(s)" },
{ field: "cdsmsp", label: "Nom du CDS ou de la MSP" },
{ field: "website", label: "Site" },
{
field: "languages",
label: "Langues de réalisation des séances (autre que le français)",
},
{
field: "cdsmsp",
label: "Nom du CDS ou de la MSP",
legend:
"A préciser UNIQUEMENT si le psychologue a une activité salariée en MSP ou CDS et s’il a choisi d’être conventionné au titre de cette activité salariée.",
},
{
field: "teleconsultation",
label: "Possibilité de séances à distance",
label: "Possibilité de séances à distance :",
type: "boolean",
options: [
{ label: "Oui", value: true },
{ label: "Non", value: false },
],
},
];

const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
const PsychologistForm = ({
psychologist,
isSuperAdmin,
}: {
psychologist: Psychologist;
isSuperAdmin: boolean;
}) => {
const [result, setResult] = useState<{ type: string; text: string }>();
const [sending, setSending] = useState(false);

const [modifiedPsychologist, setModifiedPsychologist] =
useState(psychologist);
useEffect(() => {
Expand All @@ -74,26 +119,20 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
});
};

const findLabel = (options: any[], value: boolean): string => {
const selected = options.find((item) => item.value === value);
return selected.label;
};

const submit = (e) => {
e.preventDefault();
setResult(null);
setSending(true);
axios
.put(`/api/admin/psychologists/${modifiedPsychologist.id}`, {
address: modifiedPsychologist.address,
secondAddress: modifiedPsychologist.secondAddress,
cdsmsp: modifiedPsychologist.cdsmsp,
displayEmail: modifiedPsychologist.displayEmail,
email: modifiedPsychologist.email,
firstName: modifiedPsychologist.firstName,
languages: modifiedPsychologist.languages,
lastName: modifiedPsychologist.lastName,
phone: modifiedPsychologist.phone,
displayPhone: modifiedPsychologist.displayPhone,
public: modifiedPsychologist.public,
teleconsultation: modifiedPsychologist.teleconsultation,
visible: modifiedPsychologist.visible,
website: modifiedPsychologist.website,
})
.put(
`/api/admin/psychologists/${modifiedPsychologist.id}`,
modifiedPsychologist
)
.then(() => {
setResult({
text: "Psychologue correctement mis à a jour",
Expand All @@ -105,20 +144,40 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
text: "Une erreur est survenue, veuillez reesayer",
type: "error",
});
})
.finally(() => {
setSending(false);
});
};

return (
<Row className="fr-my-4w" justifyContent="center">
<Col className="fr-col-lg-8">
<h1>Modifier le psychologue</h1>
<form onSubmit={submit}>
<p className="fr-hint-text">
Les champs suivis d’un astérisque ( <span className="error"> *</span>{" "}
) sont obligatoires.
</p>
<form onSubmit={submit} className="fr-mt-3w">
{editableFields.map((editableField) => {
switch (editableField.type) {
case "boolean":
return (
<>
<label className="fr-label">{editableField.label}</label>
<div className="fr-fieldset fr-mb-1w">
<label className="fr-label">
{editableField.label}{" "}
<strong>
{findLabel(
editableField.options,
modifiedPsychologist[editableField.field]
)}
</strong>
</label>
{editableField.legend && (
<p className="fr-hint-text fr-mb-0">
{editableField.legend}
</p>
)}
<div key={editableField.label} className="fr-toggle">
<input
id={editableField.label}
Expand All @@ -134,7 +193,7 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
htmlFor={editableField.label}
/>
</div>
</>
</div>
);
case "select":
return (
Expand All @@ -143,6 +202,7 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
//@ts-ignore
required={editableField.required}
label={editableField.label}
hint={editableField.legend}
options={editableField.options.map((option) => ({
label: option,
value: option,
Expand All @@ -159,6 +219,7 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
key={editableField.label}
required={editableField.required}
label={editableField.label}
hint={editableField.legend}
//@ts-ignore
value={modifiedPsychologist[editableField.field]}
onChange={(e) =>
Expand All @@ -168,7 +229,44 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
);
}
})}
<Button submit>Enregistrer</Button>
{isSuperAdmin && (
<div className="fr-fieldset fr-mb-1w">
<label className="fr-label">
Afficher le téléphone :{" "}
<strong>
{findLabel(
[
{ label: "Oui", value: true },
{ label: "Non", value: false },
],
modifiedPsychologist["displayPhone"]
)}
</strong>
</label>
<div className="fr-toggle">
<input
id="displayPhone"
type="checkbox"
className="fr-toggle__input"
checked={modifiedPsychologist["displayPhone"]}
onChange={(e) => {
update("displayPhone", e.target.checked);
}}
/>
<label className="fr-toggle__label" htmlFor={"displayPhone"} />
</div>
</div>
)}
<Button submit disabled={sending}>
{sending ? (
<>
<span className="fr-fi-refresh-line" aria-hidden="true" />{" "}
Sauvegarde en cours ...
</>
) : (
"Enregistrer"
)}
</Button>
{result && (
<Alert
data-test-id="alert"
Expand All @@ -183,5 +281,4 @@ const PsychologistForm = ({ psychologist }: { psychologist: Psychologist }) => {
</Row>
);
};

export default PsychologistForm;
Loading

0 comments on commit 1944469

Please sign in to comment.