-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom fields consultation (#551)
* feat: up 1 * fix: up * chore: up * chore: up * fix: up * fix: last step * fix: up * fix: up * fix: up * fix: up * fix: up
- Loading branch information
1 parent
05918f4
commit ffee784
Showing
15 changed files
with
371 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const passport = require("passport"); | ||
const { z } = require("zod"); | ||
const sequelize = require("../db/sequelize"); | ||
const { catchErrors } = require("../errors"); | ||
const Organisation = require("../models/organisation"); | ||
const validateEncryptionAndMigrations = require("../middleware/validateEncryptionAndMigrations"); | ||
const { capture } = require("../sentry"); | ||
const validateUser = require("../middleware/validateUser"); | ||
const { looseUuidRegex, customFieldSchema } = require("../utils"); | ||
const Person = require("../models/person"); | ||
|
||
router.put( | ||
"/", | ||
passport.authenticate("user", { session: false }), | ||
validateUser("admin"), | ||
validateEncryptionAndMigrations, | ||
catchErrors(async (req, res, next) => { | ||
try { | ||
z.array( | ||
z.object({ | ||
_id: z.string().regex(looseUuidRegex), | ||
encrypted: z.string(), | ||
encryptedEntityKey: z.string(), | ||
}) | ||
).parse(req.body.persons); | ||
z.optional( | ||
z.array( | ||
z.object({ | ||
name: z.string().min(1), | ||
fields: z.array(customFieldSchema), | ||
}) | ||
) | ||
).parse(req.body.consultations); | ||
} catch (e) { | ||
const error = new Error(`Invalid request in consultation update: ${e}`); | ||
error.status = 400; | ||
return next(error); | ||
} | ||
|
||
const organisation = await Organisation.findOne({ where: { _id: req.user.organisation } }); | ||
if (!organisation) return res.status(404).send({ ok: false, error: "Not Found" }); | ||
|
||
const { persons = [], consulations = [] } = req.body; | ||
|
||
try { | ||
await sequelize.transaction(async (tx) => { | ||
for (const { encrypted, encryptedEntityKey, _id } of persons) { | ||
await Person.update({ encrypted, encryptedEntityKey }, { where: { _id }, transaction: tx }); | ||
} | ||
|
||
organisation.set({ consulations }); | ||
await organisation.save({ transaction: tx }); | ||
}); | ||
} catch (e) { | ||
capture("error updating consultation", e); | ||
throw e; | ||
} | ||
return res.status(200).send({ ok: true }); | ||
}) | ||
); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api/src/db/migrations/2022-03-29_add_consultations_columns.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const sequelize = require("../sequelize"); | ||
|
||
const json = JSON.stringify([ | ||
{ name: "Psychologique", fields: [{ name: "description", type: "textarea", label: "Description", enabled: true, showInStats: false }] }, | ||
{ name: "Infirmier", fields: [{ name: "description", type: "textarea", label: "Description", enabled: true, showInStats: false }] }, | ||
{ name: "Médicale", fields: [{ name: "description", type: "textarea", label: "Description", enabled: true, showInStats: false }] }, | ||
]); | ||
|
||
// No injection here, no need to escape. | ||
sequelize.query(` | ||
ALTER TABLE "mano"."Organisation" | ||
ADD COLUMN IF NOT EXISTS "consultations" JSONB DEFAULT '${json}'::JSONB; | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.