Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PII_DB_URL_KZ=
CLINICAL_DB_URL_KZ=
MASTER_DB_URL=
USER_API_URL=

NODE_ENV=
PORT=
99 changes: 25 additions & 74 deletions service/controllers/messaging.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
import {
getChatByIdQuery,
addMessageToChatQuery,
updateClientSocketByChatIdQuery,
updateProviderSocketByChatIdQuery,
updateSocketByChatIdQuery,
} from "#queries/messaging";

import { chatNotFound } from "#utils/errors";

export const getChatById = async ({ country, language, chatId }) => {
return await getChatByIdQuery({ poolCountry: country, chatId })
.then((res) => {
if (res.rowCount === 0) {
return chatNotFound(language);
} else {
return res.rows[0];
}
})
.catch((err) => {
throw err;
});
};

export const addMessageToChat = async ({
export const getChatById = async ({
country,
language,
chatId,
message,
socketType,
}) => {
return await addMessageToChatQuery({ poolCountry: country, chatId, message })
const chatData = await getChatByIdQuery({ poolCountry: country, chatId })
.then((res) => {
if (res.rowCount === 0) {
return chatNotFound(language);
Expand All @@ -38,38 +23,29 @@ export const addMessageToChat = async ({
.catch((err) => {
throw err;
});
};

export const getClientSocketByChatId = async ({
country,
language,
chatId,
}) => {
return await getChatByIdQuery({ poolCountry: country, chatId })
.then((res) => {
if (res.rowCount === 0) {
return chatNotFound(language);
} else {
const { client_socket_id } = res.rows[0];
return { client_socket_id };
}
})
.catch((err) => {
throw err;
});
switch (socketType) {
case "provider": {
const { provider_socket_id } = chatData;
return { provider_socket_id };
}
case "client": {
const { client_socket_id } = chatData;
return { client_socket_id };
}
default: {
return chatData;
}
}
};

export const updateClientSocketByChatId = async ({
export const addMessageToChat = async ({
country,
language,
chatId,
socketId,
message,
}) => {
return await updateClientSocketByChatIdQuery({
poolCountry: country,
chatId,
socketId,
})
return await addMessageToChatQuery({ poolCountry: country, chatId, message })
.then((res) => {
if (res.rowCount === 0) {
return chatNotFound(language);
Expand All @@ -82,39 +58,14 @@ export const updateClientSocketByChatId = async ({
});
};

export const getProviderSocketByChatId = async ({
country,
language,
chatId,
}) => {
return await getChatByIdQuery({ poolCountry: country, chatId })
.then((res) => {
if (res.rowCount === 0) {
return chatNotFound(language);
} else {
const { provider_socket_id } = res.rows[0];
return { provider_socket_id };
}
})
.catch((err) => {
throw err;
});
};

export const updateProviderSocketByChatId = async ({
country,
language,
chatId,
socketId,
}) => {
return await updateProviderSocketByChatIdQuery({
poolCountry: country,
chatId,
socketId,
export const updateSocketByChatId = async (props) => {
return await updateSocketByChatIdQuery({
...props,
poolCountry: props.country,
})
.then((res) => {
if (res.rowCount === 0) {
return chatNotFound(language);
return chatNotFound(props.language);
} else {
return res.rows[0];
}
Expand Down
21 changes: 13 additions & 8 deletions service/middlewares/populateMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { getUserByID } from "#queries/users";
import fetch from "node-fetch";

const USER_API_URL = process.env.USER_API_URL;
const USER_API_LOCAL_HOST = "http://localhost:3010";

export const populateUser = async (req, res, next) => {
const country = req.header("x-country-alpha-2");
const user_id = req.header("x-user-id");

const user = await getUserByID(country, user_id)
.then((res) => res.rows[0])
.catch((err) => {
throw err;
});
const user = await fetch(`${USER_API_URL}/user/v1/user/${user_id}`, {
headers: {
...req.headers,
host: USER_API_LOCAL_HOST,
"Content-type": "application/json",
},
})
.then((raw) => raw.json())
.catch(console.log);

req.user = user;

return next();
};
31 changes: 15 additions & 16 deletions service/queries/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,31 @@ export const addMessageToChatQuery = async ({ poolCountry, chatId, message }) =>
[chatId, message]
);

export const updateClientSocketByChatIdQuery = async ({
export const updateSocketByChatIdQuery = async ({
poolCountry,
chatId,
socketId,
}) =>
await getDBPool("clinicalDb", poolCountry).query(
`
socketType,
}) => {
if (socketType === "client") {
return await getDBPool("clinicalDb", poolCountry).query(
`
UPDATE chat
SET client_socket_id = $2
WHERE chat_id = $1
RETURNING *;
`,
[chatId, socketId]
);

export const updateProviderSocketByChatIdQuery = async ({
poolCountry,
chatId,
socketId,
}) =>
await getDBPool("clinicalDb", poolCountry).query(
`
[chatId, socketId]
);
} else {
return await getDBPool("clinicalDb", poolCountry).query(
`
UPDATE chat
SET provider_socket_id = $2
WHERE chat_id = $1
RETURNING *;
`,
[chatId, socketId]
);
[chatId, socketId]
);
}
};
14 changes: 0 additions & 14 deletions service/queries/users.js

This file was deleted.

Loading