Skip to content

Commit

Permalink
Ajout d'un identifiant définitif non-éditable à la création d'une org…
Browse files Browse the repository at this point in the history
…anisation (#52)

Co-authored-by: Arnaud Ambroselli <[email protected]>
  • Loading branch information
rap2hpoutre and arnaudambro authored Mar 5, 2024
1 parent 4c8e184 commit b1babcf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
4 changes: 3 additions & 1 deletion api/src/controllers/organisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ router.post(
try {
z.object({
orgName: z.string().min(1),
orgId: z.string().min(1),
name: z.string().min(1),
email: z.string().email(),
}).parse(req.body);
Expand All @@ -119,13 +120,14 @@ router.post(
error.status = 400;
return next(error);
}
const { orgName, name, email } = req.body;
const { orgName, name, email, orgId } = req.body;
const user = await User.findOne({ where: { email } });
if (!!user) return res.status(400).send({ ok: false, error: "Cet email existe déjà dans une autre organisation" });

const organisation = await Organisation.create(
{
name: orgName,
orgId: orgId,
// We have to add default custom fields on creation
// (search for "custom-fields-persons-setup" or "custom-fields-persons-refacto-regroup" in code).
customFieldsPersons: [
Expand Down
14 changes: 14 additions & 0 deletions api/src/db/migrations/20240301142954-add-org-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query('ALTER TABLE "mano"."Organisation" ADD COLUMN "orgId" TEXT;');

await queryInterface.sequelize.query('UPDATE "mano"."Organisation" SET "orgId" = "name";');
},

async down(queryInterface) {
await queryInterface.sequelize.query('ALTER TABLE "mano"."Organisation" DROP COLUMN "orgId";');
},
};
1 change: 1 addition & 0 deletions api/src/models/organisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = (sequelize, DataTypes) => {
const schema = {
_id: { type: DataTypes.UUID, allowNull: false, defaultValue: DataTypes.UUIDV4, primaryKey: true },
name: DataTypes.TEXT,
orgId: DataTypes.TEXT,
categories: DataTypes.ARRAY(DataTypes.TEXT),
actionsGroupedCategories: {
type: DataTypes.JSONB, // example: [{"groupTitle": "médical", categories: ["seringue", "pansement"]}, { "groupTitle": "local", "categories": ["entretien", "lavage"]}]
Expand Down
21 changes: 15 additions & 6 deletions dashboard/src/scenes/organisation/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ButtonCustom from '../../components/ButtonCustom';

import Loading from '../../components/loading';
import API from '../../services/api';
import { formatDateWithFullMonth } from '../../services/date';
import { formatAge, formatDateWithFullMonth } from '../../services/date';
import useTitle from '../../services/useTitle';
import DeleteButtonAndConfirmModal from '../../components/DeleteButtonAndConfirmModal';
import { capture } from '../../services/sentry';
Expand Down Expand Up @@ -71,11 +71,11 @@ const List = () => {
data={organisations}
key={updateKey}
columns={[
{ title: 'Nom', dataKey: 'name', onSortOrder: setSortOrder, onSortBy: setSortBy, sortOrder, sortBy },
{ title: 'Nom', dataKey: 'name', onSortOrder: setSortOrder, onSortBy: setSortBy, sortOrder, sortBy, render: (o) => <div>{o.name}<br /><small className="tw-text-gray-500">ID: {o.orgId}</small></div> },
{
title: 'Créée le',
dataKey: 'createdAt',
render: (o) => formatDateWithFullMonth(o.createdAt || ''),
render: (o) => <div>{formatDateWithFullMonth(o.createdAt || '')}<br /><small className="tw-text-gray-500">il y a {o.createdAt ? formatAge(o.createdAt) : "un certain temps"}</small></div>,
onSortOrder: setSortOrder,
onSortBy: setSortBy,
sortOrder,
Expand Down Expand Up @@ -138,7 +138,7 @@ const List = () => {
sortOrder,
onSortOrder: setSortOrder,
onSortBy: setSortBy,
render: (o) => formatDateWithFullMonth(o.encryptionLastUpdateAt || ''),
render: (o) => <div>{o.encryptionLastUpdateAt ? formatDateWithFullMonth(o.encryptionLastUpdateAt) : "Pas encore chiffrée"}<br /><small className="tw-text-gray-500">{o.encryptionLastUpdateAt ? "il y a "+ formatAge(o.encryptionLastUpdateAt) : ""}</small></div>,
},
{
title: 'Action',
Expand Down Expand Up @@ -196,11 +196,12 @@ const Create = ({ onChange, open, setOpen }) => {
<ModalHeader toggle={() => setOpen(false)}>Créer une nouvelle organisation et un administrateur</ModalHeader>
<ModalBody>
<Formik
initialValues={{ orgName: '', name: '', email: '' }}
initialValues={{ orgName: '', name: '', email: '', orgId: ''}}
validate={(values) => {
const errors = {};
if (!values.name) errors.name = 'Le nom est obligatoire';
if (!values.orgName) errors.orgName = "Le nom de l'organisation est obligatoire";
if (!values.orgId) errors.orgId = "L'identifiant est obligatoire";
if (!values.email) errors.email = "L'email est obligatoire";
else if (!emailRegex.test(values.email)) errors.email = "L'email est invalide";
return errors;
Expand Down Expand Up @@ -228,8 +229,16 @@ const Create = ({ onChange, open, setOpen }) => {
{touched.orgName && errors.orgName && <span className="tw-text-xs tw-text-red-500">{errors.orgName}</span>}
</FormGroup>
</Col>
<Col md={6}>
<FormGroup>
<Label htmlFor="orgName">
Identifiant interne <small>(non modifiable)</small>
</Label>
<Input name="orgId" id="orgId" value={values.orgId} onChange={handleChange} />
{touched.orgId && errors.orgId && <span className="tw-text-xs tw-text-red-500">{errors.orgId}</span>}
</FormGroup>
</Col>
</Row>
<br />
<Row>
<Col md={6}>
<FormGroup>
Expand Down

0 comments on commit b1babcf

Please sign in to comment.