diff --git a/service/controllers/messaging.js b/service/controllers/messaging.js index 1615d29..eada569 100644 --- a/service/controllers/messaging.js +++ b/service/controllers/messaging.js @@ -3,9 +3,10 @@ import { addMessageToChatQuery, updateClientSocketByChatIdQuery, updateProviderSocketByChatIdQuery, + getAllChatDataQuery, } from "#queries/messaging"; -import { chatNotFound } from "#utils/errors"; +import { chatNotFound, userNotAuthorized } from "#utils/errors"; export const getChatById = async ({ country, language, chatId }) => { return await getChatByIdQuery({ poolCountry: country, chatId }) @@ -123,3 +124,39 @@ export const updateProviderSocketByChatId = async ({ throw err; }); }; + +export const getAllChatData = async ({ + country, + language, + providerDetailId, + clientDetailId, + requesterId, + requestedBy, +}) => { + const clientIdMismatch = + requestedBy === "client" && requesterId !== clientDetailId; + const providerIdMismatch = + requestedBy === "provider" && requesterId !== providerDetailId; + if (clientIdMismatch || providerIdMismatch) { + throw userNotAuthorized(language); + } + + return await getAllChatDataQuery({ + poolCountry: country, + providerDetailId, + clientDetailId, + }) + .then((res) => { + if (res.rowCount === 0) { + return chatNotFound(language); + } else { + const messages = res.rows.map((x) => x.messages).flat(); + const result = res.rows[0]; + result.messages = messages.filter((x) => x !== null); + return result; + } + }) + .catch((err) => { + throw err; + }); +}; diff --git a/service/queries/messaging.js b/service/queries/messaging.js index 55570f6..9e2ac83 100644 --- a/service/queries/messaging.js +++ b/service/queries/messaging.js @@ -50,3 +50,19 @@ export const updateProviderSocketByChatIdQuery = async ({ `, [chatId, socketId] ); + +export const getAllChatDataQuery = async ({ + poolCountry, + providerDetailId, + clientDetailId, +}) => { + return await getDBPool("clinicalDb", poolCountry).query( + ` + SELECT chat_id, client_detail_id, provider_detail_id, messages, date, client_socket_id, provider_socket_id + FROM chat + WHERE client_detail_id = $1 AND provider_detail_id = $2 + ORDER BY created_at ASC + `, + [clientDetailId, providerDetailId] + ); +}; diff --git a/service/routes/v1/MessagingRouter.js b/service/routes/v1/MessagingRouter.js index e2daa12..19650ab 100644 --- a/service/routes/v1/MessagingRouter.js +++ b/service/routes/v1/MessagingRouter.js @@ -9,6 +9,7 @@ import { updateClientSocketByChatId, getProviderSocketByChatId, updateProviderSocketByChatId, + getAllChatData, } from "#controllers/messaging"; import { @@ -18,6 +19,7 @@ import { updateClientSocketByChatIdSchema, getProviderSocketByChatIdSchema, updateProviderSocketByChatIdSchema, + getAllChatDataSchema, } from "#schemas/messagingSchemas"; const router = express.Router(); @@ -158,4 +160,33 @@ router.put("/provider-socket", async (req, res, next) => { .catch(next); }); +router.get("/all-chat-data", populateUser, async (req, res, next) => { + /** + * #route GET /messaging/v1/all-chat-data + * #desc Get all chat data by given provider detail ID and client detail ID + */ + const country = req.header("x-country-alpha-2"); + const language = req.header("x-language-alpha-2"); + + const { providerDetailId, clientDetailId } = req.query; + const { client_detail_id, provider_detail_id, type } = req.user; + + const requesterId = type === "client" ? client_detail_id : provider_detail_id; + + return await getAllChatDataSchema + .noUnknown(true) + .strict(true) + .validate({ + country, + language, + providerDetailId, + clientDetailId, + requesterId, + requestedBy: type, + }) + .then(getAllChatData) + .then((result) => res.status(200).send(result)) + .catch(next); +}); + export { router }; diff --git a/service/schemas/messagingSchemas.js b/service/schemas/messagingSchemas.js index dea4166..40b01b9 100644 --- a/service/schemas/messagingSchemas.js +++ b/service/schemas/messagingSchemas.js @@ -43,3 +43,12 @@ export const updateProviderSocketByChatIdSchema = yup.object().shape({ chatId: yup.string().uuid().required(), socketId: yup.string().required(), }); + +export const getAllChatDataSchema = yup.object().shape({ + country: yup.string().required(), + language: yup.string().required(), + providerDetailId: yup.string().uuid().required(), + clientDetailId: yup.string().uuid().required(), + requesterId: yup.string().uuid().required(), + requestedBy: yup.string().oneOf(["client", "provider"]).required(), +}); diff --git a/service/utils/errors.js b/service/utils/errors.js index 8cda05b..3b34d9f 100644 --- a/service/utils/errors.js +++ b/service/utils/errors.js @@ -7,3 +7,11 @@ export const chatNotFound = (language) => { error.status = 404; return error; }; + +export const userNotAuthorized = (language) => { + const error = new Error(); + error.message = t("user_not_authorized_error", language); + error.name = "USER NOT AUTHORIZED"; + error.status = 401; + return error; +};