Skip to content

Commit

Permalink
fix: nouveau champ numéro de téléphone pour les utilisateurs (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre authored Oct 12, 2023
1 parent f8afb90 commit b632f8c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
18 changes: 15 additions & 3 deletions api/src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ router.post(
try {
z.object({
name: z.string().optional(),
phone: z.string().optional(),
email: z.preprocess((email) => email.trim().toLowerCase(), z.string().email().optional().or(z.literal(""))),
healthcareProfessional: z.boolean(),
team: z.array(z.string().regex(looseUuidRegex)),
Expand All @@ -319,10 +320,11 @@ router.post(
return next(error);
}

const { name, email, role, team, healthcareProfessional } = req.body;
const { name, email, role, team, healthcareProfessional, phone } = req.body;
const token = crypto.randomBytes(20).toString("hex");
const newUser = {
name: sanitizeAll(name),
phone: sanitizeAll(phone) || null,
role,
healthcareProfessional: role === "restricted-access" ? false : healthcareProfessional,
email: sanitizeAll(email.trim().toLowerCase()),
Expand Down Expand Up @@ -376,6 +378,7 @@ Guillaume Demirhan, porteur du projet: [email protected] - +33 7 66 56 1
data: {
_id: data._id,
name: data.name,
phone: data.phone,
email: data.email,
role: data.role,
healthcareProfessional: data.healthcareProfessional,
Expand Down Expand Up @@ -432,6 +435,7 @@ router.post(
user: {
_id: userWithoutPassword._id,
name: userWithoutPassword.name,
phone: userWithoutPassword.phone,
email: userWithoutPassword.email,
createdAt: userWithoutPassword.createdAt,
updatedAt: userWithoutPassword.updatedAt,
Expand Down Expand Up @@ -469,6 +473,7 @@ router.get(
data: {
_id: user._id,
name: user.name,
phone: user.phone,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
Expand Down Expand Up @@ -511,6 +516,7 @@ router.get(
data.push({
_id: user._id,
name: user.name,
phone: user.phone,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
Expand All @@ -535,6 +541,7 @@ router.put(
try {
z.object({
name: z.optional(z.string().min(1)),
phone: z.string().optional(),
email: z.preprocess((email) => email.trim().toLowerCase(), z.string().email().optional().or(z.literal(""))),
password: z.optional(z.string().min(1)),
gaveFeedbackEarly2023: z.optional(z.boolean()),
Expand All @@ -548,12 +555,13 @@ router.put(
}

const _id = req.user._id;
const { name, email, password, team, termsAccepted, gaveFeedbackEarly2023 } = req.body;
const { name, email, password, team, termsAccepted, gaveFeedbackEarly2023, phone } = req.body;

const user = await User.findOne({ where: { _id } });
if (!user) return res.status(404).send({ ok: false, error: "Utilisateur non trouvé" });

if (name) user.set({ name: sanitizeAll(name) });
if (phone) user.set({ phone: sanitizeAll(phone) });
if (email) user.set({ email: sanitizeAll(email.trim().toLowerCase()) });
if (termsAccepted) user.set({ termsAccepted: termsAccepted });
if (password) {
Expand All @@ -579,6 +587,7 @@ router.put(
user: {
_id: user._id,
name: user.name,
phone: user.phone,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
Expand All @@ -604,6 +613,7 @@ router.put(
}),
body: z.object({
name: z.optional(z.string().min(1)),
phone: z.string().optional(),
email: z.optional(z.preprocess((email) => email.trim().toLowerCase(), z.string().email().optional().or(z.literal("")))),
password: z.optional(z.string().min(1)),
team: z.optional(z.array(z.string().regex(looseUuidRegex))),
Expand All @@ -617,12 +627,13 @@ router.put(
}

const _id = req.params._id;
const { name, email, team, role, healthcareProfessional } = req.body;
const { name, email, team, role, healthcareProfessional, phone } = req.body;

const user = await User.findOne({ where: { _id, organisation: req.user.organisation } });
if (!user) return res.status(404).send({ ok: false, error: "Not Found" });

if (name) user.name = sanitizeAll(name);
if (phone) user.phone = sanitizeAll(phone);
if (email) user.email = sanitizeAll(email.trim().toLowerCase());

if (healthcareProfessional !== undefined) user.set({ healthcareProfessional });
Expand All @@ -647,6 +658,7 @@ router.put(
user: {
_id: user._id,
name: user.name,
phone: user.phone,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
Expand Down
15 changes: 15 additions & 0 deletions api/src/db/migrations/20231010095801-add-phone-to-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.query(`
ALTER TABLE "mano"."User"
ADD COLUMN IF NOT EXISTS "phone" text;
`);
},

async down() {
// Qui fait des down, et pourquoi ?
},
};
1 change: 1 addition & 0 deletions api/src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = (sequelize, DataTypes) => {
const schema = {
_id: { type: DataTypes.UUID, allowNull: false, defaultValue: DataTypes.UUIDV4, primaryKey: true },
name: DataTypes.TEXT,
phone: DataTypes.TEXT,
email: { type: DataTypes.TEXT, allowNull: false },
password: { type: DataTypes.TEXT, allowNull: false },
organisation: { type: DataTypes.UUID, references: { model: "Organisation", key: "_id", deferrable: Deferrable.INITIALLY_IMMEDIATE } },
Expand Down
14 changes: 14 additions & 0 deletions dashboard/src/scenes/user/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ const List = () => {
sortOrder,
sortBy,
},
{
title: 'Téléphone',
dataKey: 'phone',
onSortOrder: setSortOrder,
onSortBy: setSortBy,
sortOrder,
sortBy,
},
{
title: 'Rôle',
dataKey: 'role',
Expand Down Expand Up @@ -121,6 +129,7 @@ const List = () => {
);
},
},

{
title: 'Créé le',
dataKey: 'createdAt',
Expand Down Expand Up @@ -154,6 +163,7 @@ const Create = ({ onChange, users }) => {
return {
name: '',
email: '',
phone: '',
role: 'normal',
team: teams.map((t) => t._id),
healthcareProfessional: false,
Expand Down Expand Up @@ -226,6 +236,10 @@ const Create = ({ onChange, users }) => {
<label htmlFor="email">Email</label>
<input className="tailwindui" placeholder="[email protected]" name="email" id="email" value={data.email} onChange={handleChange} />
</div>
<div className="tw-flex tw-basis-1/2 tw-flex-col tw-px-4 tw-py-2">
<label htmlFor="phone">Téléphone</label>
<input className="tailwindui" placeholder="0612345678" name="phone" id="phone" value={data.phone} onChange={handleChange} />
</div>
<div className="tw-flex tw-basis-1/2 tw-flex-col tw-px-4 tw-py-2">
<label htmlFor="role">Role</label>
<div className="tw-mt-1 tw-w-full">
Expand Down
9 changes: 9 additions & 0 deletions dashboard/src/scenes/user/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const View = () => {
initialValues={{
name: localUser.name,
email: localUser.email,
phone: localUser.phone,
team: localUser.team,
role: localUser.role,
healthcareProfessional: localUser.healthcareProfessional,
Expand All @@ -64,6 +65,7 @@ const View = () => {
try {
if (!body.team?.length) return toast.error('Au moins une équipe est obligatoire');
if (body.email && !emailRegex.test(body.email)) return toast.error('Email invalide');
if (!body.name) return toast.error('Le nom doit faire au moins un caractère');
body.organisation = organisation._id;
const res = await API.put({ path: `/user/${id}`, body });
if (!res.ok) return actions.setSubmitting(false);
Expand All @@ -87,12 +89,19 @@ const View = () => {
<Input name="name" id="name" value={values.name} onChange={handleChange} />
</FormGroup>
</Col>

<Col md={6}>
<FormGroup>
<Label htmlFor="email">Email</Label>
<Input name="email" id="email" value={values.email} onChange={handleChange} />
</FormGroup>
</Col>
<Col md={6}>
<FormGroup>
<Label htmlFor="phone">Téléphone</Label>
<Input name="phone" id="phone" value={values.phone} onChange={handleChange} />
</FormGroup>
</Col>
<Col md={6}>
<FormGroup>
<Label htmlFor="team">Équipes</Label>
Expand Down

0 comments on commit b632f8c

Please sign in to comment.