Skip to content

Commit

Permalink
Add: request to get all chat data for a specific client-provider pair
Browse files Browse the repository at this point in the history
  • Loading branch information
georgipavlov-7DIGIT committed Jun 8, 2023
1 parent dbee0b5 commit aeccc83
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
39 changes: 38 additions & 1 deletion service/controllers/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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;
});
};
16 changes: 16 additions & 0 deletions service/queries/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
};
31 changes: 31 additions & 0 deletions service/routes/v1/MessagingRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
updateClientSocketByChatId,
getProviderSocketByChatId,
updateProviderSocketByChatId,
getAllChatData,
} from "#controllers/messaging";

import {
Expand All @@ -18,6 +19,7 @@ import {
updateClientSocketByChatIdSchema,
getProviderSocketByChatIdSchema,
updateProviderSocketByChatIdSchema,
getAllChatDataSchema,
} from "#schemas/messagingSchemas";

const router = express.Router();
Expand Down Expand Up @@ -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 };
9 changes: 9 additions & 0 deletions service/schemas/messagingSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
8 changes: 8 additions & 0 deletions service/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit aeccc83

Please sign in to comment.