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
32 changes: 8 additions & 24 deletions src/controllers/userOrgLocal.controller.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
import { storeInCache } from "../cache_service/index.js";
import { updateProxyDetails, getProxyDetails, removeClientUser } from "../services/proxy.service.js";
import axios from "axios";
import {
updateProxyDetails,
getProxyDetails,
removeClientUser,
generateProxyAuthToken,
} from "../services/proxy.service.js";

const userOrgLocalToken = async (req, res, next) => {
// Call external API to generate auth token
const apiUrl = `https://routes.msg91.com/api/${process.env.PUBLIC_REFERENCEID}/generateAuthToken`;
const response = await axios.get(apiUrl, {
headers: {
authkey: process.env.ADMIN_API_KEY,
proxy_auth_token: req.headers.proxy_auth_token || req.headers.authorization?.replace("Bearer ", ""),
},
});

const token = response.data.data.jwt;
// const token = reissueToken(jwtToken);
const token = await generateProxyAuthToken(req);
res.locals = { data: { token }, success: true };
req.statusCode = 200;
return next();
};

const switchUserOrgLocal = async (req, res, next) => {
// Call external API to generate auth token with new org
const apiUrl = `https://routes.msg91.com/api/${process.env.PUBLIC_REFERENCEID}/generateAuthToken`;
const response = await axios.get(apiUrl, {
headers: {
authkey: process.env.ADMIN_API_KEY,
proxy_auth_token: req.headers.proxy_auth_token || req.headers.authorization?.replace("Bearer ", ""),
},
});

const token = response.data.data.jwt;
// const token = reissueToken(jwtToken);
const token = await generateProxyAuthToken(req);
res.locals = { data: { token }, success: true };
req.statusCode = 200;
return next();
Expand Down
101 changes: 48 additions & 53 deletions src/services/proxy.service.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import axios from "axios";
import { findInCache, storeInCache } from "../cache_service/index.js";
import { objectToQueryParams } from "./utils/utility.service.js";
// import { findInCache, storeInCache } from './cache.js';

async function fetchProxyDetails(referenceId, params = {}, options = {}) {
const response = await axios.get(`${process.env.PROXY_BASE_URL}/${referenceId}/getDetails`, {
params,
headers: {
"Content-Type": "application/json",
Authkey: process.env.PROXY_ADMIN_TOKEN,
...options.headers,
},
});
return response;
}

async function updateProxyDetailsById(referenceId, updateObject, options = {}) {
const response = await axios.put(`${process.env.PROXY_BASE_URL}/${referenceId}/updateDetails`, updateObject, {
headers: {
"Content-Type": "application/json",
Authkey: process.env.PROXY_ADMIN_TOKEN,
...options.headers,
},
});
return response;
}

export async function getUserOrgMapping(userId, orgId) {
try {
if (!userId || !orgId) throw new Error("Sorry, Either the fields are missing or you are not authorized!");
const cache_key = `userOrgMapping-${userId}-${orgId}`;
const data = await findInCache(cache_key);
if (data) return JSON.parse(data);
const response = await axios.get(
`${process.env.PROXY_BASE_URL}/${process.env.PROXY_USER_REFERENCE_ID}/getDetails`,
{
params: {
company_id: orgId,
user_id: userId,
},
headers: {
"Content-Type": "application/json",
Authkey: process.env.PROXY_ADMIN_TOKEN,
},
}
);
const response = await fetchProxyDetails(process.env.PROXY_USER_REFERENCE_ID, {
company_id: orgId,
user_id: userId,
});
// eslint-disable-next-line no-constant-binary-expression
const result = parseInt(response?.data?.data?.totalEntityCount, 10) === 1 ?? false;
storeInCache(cache_key, result);
Expand Down Expand Up @@ -86,23 +99,11 @@ export async function updateOrganizationData(orgId, orgDetails) {
company: orgDetails,
};
try {
const response = await axios.put(
`${process.env.PROXY_BASE_URL}/${process.env.PROXY_USER_REFERENCE_ID}/updateDetails`,
updateObject,
{
headers: {
"Content-Type": "application/json",
Authkey: process.env.PROXY_ADMIN_TOKEN,
},
// You can include credentials if required (e.g., 'withCredentials': true)
}
);

const data = response?.data;
return data;
const response = await updateProxyDetailsById(process.env.PROXY_USER_REFERENCE_ID, updateObject);
return response?.data;
} catch (error) {
console.error("Error fetching data:", error.message);
throw error; // Re-throw the error for the caller to handle
throw error;
}
}

Expand Down Expand Up @@ -131,15 +132,10 @@ export async function createProxyToken(token_data) {

export async function getUsers(org_id, page = 1, pageSize = 10) {
try {
const response = await axios.get(`${process.env.PROXY_BASE_URL}/${process.env.PUBLIC_REFERENCEID}/getDetails`, {
params: {
company_id: org_id,
pageNo: page,
itemsPerPage: pageSize,
},
headers: {
authkey: process.env.PROXY_ADMIN_TOKEN,
},
const response = await fetchProxyDetails(process.env.PUBLIC_REFERENCEID, {
company_id: org_id,
pageNo: page,
itemsPerPage: pageSize,
});
return response?.data?.data;
} catch (error) {
Expand All @@ -151,7 +147,7 @@ export async function getUsers(org_id, page = 1, pageSize = 10) {
export async function validateCauthKey(pauthkey) {
try {
const response = await axios.post(
"https://routes.msg91.com/api/validateCauthKey",
`${process.env.PROXY_BASE_URL}/validateCauthKey`,
{
cAuthKey: pauthkey,
},
Expand All @@ -172,15 +168,8 @@ export async function validateCauthKey(pauthkey) {
}

export async function updateProxyDetails(updateObject) {
const PUBLIC_REFERENCEID = process.env.PROXY_USER_REFERENCE_ID;
const apiUrl = `https://routes.msg91.com/api/${PUBLIC_REFERENCEID}/updateDetails`;
try {
const response = await axios.put(apiUrl, updateObject, {
headers: {
Authkey: process.env.PROXY_ADMIN_TOKEN,
"Content-Type": "application/json",
},
});
const response = await updateProxyDetailsById(process.env.PROXY_USER_REFERENCE_ID, updateObject);
return response.data;
} catch (error) {
console.error("Error updating details:", error);
Expand All @@ -190,12 +179,7 @@ export async function updateProxyDetails(updateObject) {

export async function getProxyDetails(params) {
try {
const response = await axios.get(`${process.env.PROXY_BASE_URL}/${process.env.PUBLIC_REFERENCEID}/getDetails`, {
params,
headers: {
authkey: process.env.PROXY_ADMIN_TOKEN,
},
});
const response = await fetchProxyDetails(process.env.PUBLIC_REFERENCEID, params);
return response.data;
} catch (error) {
console.error("Error fetching details:", error.message);
Expand All @@ -221,3 +205,14 @@ export async function removeClientUser(userId, companyId, featureId) {
throw error;
}
}

export async function generateProxyAuthToken(req) {
const apiUrl = `${process.env.PROXY_BASE_URL}/${process.env.PUBLIC_REFERENCEID}/generateAuthToken`;
const response = await axios.get(apiUrl, {
headers: {
authkey: process.env.ADMIN_API_KEY,
proxy_auth_token: req.headers.proxy_auth_token || req.headers.authorization?.replace("Bearer ", ""),
},
});
return response.data.data.jwt;
}