diff --git a/service/controllers/users.js b/service/controllers/users.js index 86c7ace..9086159 100644 --- a/service/controllers/users.js +++ b/service/controllers/users.js @@ -109,8 +109,8 @@ export const updateNotificationPreferences = async ({ }); }; -export const getTwilioToken = async ({ userId, consultationId }) => { - const token = videoToken(userId, consultationId, TWILIO_CONFIG); +export const getTwilioToken = async ({ user_id, consultationId }) => { + const token = videoToken(user_id, consultationId, TWILIO_CONFIG); return { token: token.toJwt() }; }; diff --git a/service/queries/users.js b/service/queries/users.js index d92ea74..d7437b6 100644 --- a/service/queries/users.js +++ b/service/queries/users.js @@ -60,7 +60,7 @@ export const getProviderUserByEmail = async (poolCountry, email) => export const getUserByID = async (poolCountry, user_id) => await getDBPool("piiDb", poolCountry).query( ` - SELECT user_id, country_id, type, client_detail_id, notification_preference_id, password + SELECT user_id, country_id, type, client_detail_id, provider_detail_id, notification_preference_id, password FROM "user" WHERE user_id = $1 AND deleted_at IS NULL diff --git a/service/routes/v1/UserRouter.js b/service/routes/v1/UserRouter.js index df72df8..c3d86c0 100644 --- a/service/routes/v1/UserRouter.js +++ b/service/routes/v1/UserRouter.js @@ -53,12 +53,30 @@ router.patch("/password", securedRoute, async (req, res, next) => { return await changePasswordSchema .noUnknown(true) .strict(true) - .validate({ country, language, user_id, ...payload }) + .validate({ ...payload, country, language, user_id }) .then(changeUserPassword) .then((result) => res.status(200).send(result)) .catch(next); }); +router.get("/:id", async (req, res, next) => { + /** + * #route GET /user/v1/user/:id + * #desc Get user data by ID (Internal use only) + */ + const country = req.header("x-country-alpha-2"); + const language = req.header("x-language-alpha-2"); + const { id } = req.params; + + return await getUserByIdSchema + .noUnknown(true) + .strict(true) + .validate({ country, language, user_id: id }) + .then(getSharedUserData) + .then((result) => res.status(200).send(result)) + .catch(next); +}); + router .route("/notification-preferences") .get(securedRoute, async (req, res, next) => { @@ -87,13 +105,12 @@ router const language = req.header("x-language-alpha-2"); const notification_preference_id = req.user.notification_preference_id; - const payload = req.body; return await updateNotificationPreferencesSchema .noUnknown(true) .strict(true) - .validate({ country, language, notification_preference_id, ...payload }) + .validate({ ...payload, country, language, notification_preference_id }) .then(updateNotificationPreferences) .then((result) => res.status(200).send(result)) .catch(next); @@ -109,14 +126,13 @@ router const country = req.header("x-country-alpha-2"); const language = req.header("x-language-alpha-2"); - const userId = req.user.user_id; - + const user_id = req.user.user_id; const consultationId = req.query.consultationId; return await getTwilioTokenSchema .noUnknown(true) .strict(true) - .validate({ country, language, userId, consultationId }) + .validate({ country, language, user_id, consultationId }) .then(getTwilioToken) .then((result) => res.status(200).send(result)) .catch(next); @@ -135,7 +151,7 @@ router.route("/add-contact-form").post(async (req, res, next) => { return await addContactFormSchema .noUnknown(true) .strict(true) - .validate({ country, language, ...payload }) + .validate({ ...payload, country, language }) .then(addContactForm) .then((result) => res.status(200).send(result)) .catch(next); diff --git a/service/routes/v1/authRouter.js b/service/routes/v1/authRouter.js index 077865e..df3d8c1 100644 --- a/service/routes/v1/authRouter.js +++ b/service/routes/v1/authRouter.js @@ -114,7 +114,7 @@ router.post("/refresh-token", async (req, res, next) => { return await refreshAccessTokenSchema .noUnknown(true) .strict() - .validate({ country, language, ...payload }) + .validate({ ...payload, country, language }) .then(refreshAccessToken) .then((result) => res.status(200).send(result)) .catch(next); diff --git a/service/routes/v1/rescueRouter.js b/service/routes/v1/rescueRouter.js index 159d67f..90e5e36 100644 --- a/service/routes/v1/rescueRouter.js +++ b/service/routes/v1/rescueRouter.js @@ -22,8 +22,7 @@ router const country = req.header("x-country-alpha-2"); const language = req.header("x-language-alpha-2"); - const { email } = req.query; - const { type } = req.query; + const { email, type } = req.query; return await initForgotPasswordSchema .noUnknown(true) @@ -46,7 +45,7 @@ router return await resetForgotPasswordSchema .noUnknown(true) .strict(true) - .validate({ country, language, ...payload }) + .validate({ ...payload, country, language }) .then(resetForgotPassword) .then((result) => res.status(200).send(result)) .catch(next); diff --git a/service/schemas/userSchemas.js b/service/schemas/userSchemas.js index 8defe83..6ede6f0 100644 --- a/service/schemas/userSchemas.js +++ b/service/schemas/userSchemas.js @@ -94,10 +94,7 @@ export const createUserSchema = (language) => ["client", "provider"] ); -export const changePasswordSchema = yup.object().shape({ - country: yup.string().required(), - language: yup.string().required(), - user_id: yup.string().uuid().required(), +export const changePasswordSchema = getUserByIdSchema.shape({ oldPassword: yup.string().required(), newPassword: yup.string().matches(PASSWORD_REGEX).required(), }); @@ -108,22 +105,17 @@ export const getNotificationPreferencesSchema = yup.object().shape({ notification_preference_id: yup.string().uuid().required(), }); -export const updateNotificationPreferencesSchema = yup.object().shape({ - country: yup.string().required(), - language: yup.string().required(), - notification_preference_id: yup.string().uuid().required(), - email: yup.boolean().required(), - consultationReminder: yup.boolean().required(), - consultationReminderMin: yup.number().positive().max(60).required(), - inPlatform: yup.boolean().required(), - push: yup.boolean().required(), -}); +export const updateNotificationPreferencesSchema = + getNotificationPreferencesSchema.shape({ + email: yup.boolean().required(), + consultationReminder: yup.boolean().required(), + consultationReminderMin: yup.number().positive().max(60).required(), + inPlatform: yup.boolean().required(), + push: yup.boolean().required(), + }); -export const getTwilioTokenSchema = yup.object().shape({ - country: yup.string().required(), - language: yup.string().required(), +export const getTwilioTokenSchema = getUserByIdSchema.shape({ consultationId: yup.string().uuid().required(), - userId: yup.string().uuid().required(), }); export const addContactFormSchema = yup.object().shape({