Skip to content

Commit

Permalink
Add: when assigning/removing providers from organizations, send a req…
Browse files Browse the repository at this point in the history
…uest to delete their cache items
  • Loading branch information
georgipavlov-7DIGIT committed Sep 19, 2024
1 parent fde5575 commit 6c6be49
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
16 changes: 15 additions & 1 deletion service/controllers/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
organizationNotFound,
providerAlreadyAssignedToOrg,
} from "#utils/errors";
import { removeProvidersCacheRequest } from "#utils/helperFunctions";

export const createOrganization = async (data) => {
return await createOrganizationQuery(data)
Expand Down Expand Up @@ -89,6 +90,13 @@ export const assignProviderToOrganization = async (data) => {
throw err;
});
}

await removeProvidersCacheRequest({
providerIds: data.providerDetailIds,
country: data.country,
language: data.language,
});

return { success: true };
};

Expand Down Expand Up @@ -285,7 +293,13 @@ export const getOrganizationById = async (data) => {

export const removeProviderFromOrganization = async (data) => {
return await removeProviderFromOrganizationQuery(data)
.then((res) => {
.then(async (res) => {
await removeProvidersCacheRequest({
providerIds: [data.providerDetailId],
country: data.country,
language: data.language,
});

return res.rows[0];
})
.catch((err) => {
Expand Down
34 changes: 34 additions & 0 deletions service/utils/helperFunctions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import bcrypt from "bcryptjs";
import { updateAdminUserPassword } from "#queries/admins";
import fetch from "node-fetch";

const PROVIDER_URL = process.env.PROVIDER_URL;
const PROVIDER_LOCAL_HOST = "http://localhost:3002";

export const updatePassword = async ({ admin_id, password }) => {
const salt = await bcrypt.genSalt(12);
Expand Down Expand Up @@ -66,3 +70,33 @@ export const generatePassword = (length) => {
if (passwordPattern.test(password)) return password;
else return generatePassword(length);
};

export const removeProvidersCacheRequest = async ({
providerIds,
country,
language,
}) => {
const response = await fetch(
`${PROVIDER_URL}/provider/v1/provider/remove-cache`,
{
method: "PUT",
headers: {
"x-language-alpha-2": language,
"x-country-alpha-2": country,
host: PROVIDER_LOCAL_HOST,
"Content-type": "application/json",
},
body: JSON.stringify({
providerDetailIds: providerIds,
}),
}
).catch(console.log);

const result = await response.json();

if (result.error) {
throw result.error;
}

return result;
};

0 comments on commit 6c6be49

Please sign in to comment.