import Axios from 'axios';
import CONFIG from '../../../config/config';
import moment from 'moment';
const url = {
getPortfolioSurveysConsultants:
CONFIG.BASE_URL + 'double-materiality/getPortfolioSurveysConsultants',
getSurveys: CONFIG.BASE_URL + 'double-materiality/getSurveys',
};
/*
Desc: Api to create double materiality
Params: Data:- will be save on collection based on consultantId
*/ const localUrl =
'http://localhost:17538/api/v1/';
const createDoubleMaterialitySurvey = async data => {
let url = CONFIG.BASE_URL + 'createSurveyResult';
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
/*
Desc: Api to update double materiality
Params: Data:- will be save on collection based on consultantId
consultantId:- to identity collection
currentSurveyId:- id to update doc in collection
surveyId:- id to update doc in collection
*/
const updateDoubleMaterialitySurvey = async (data, consultantId, currentSurveyId, surveyId) => {
let url =
CONFIG.BASE_URL +
'double-materiality/portfolio-surveys-consultants' +
'?currentSurveyId=' +
currentSurveyId ??
'' + '&surveyId=' + surveyId ??
'' + '&consultantId=' + consultantId;
const response = await Axios.put(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
/*
Desc: Api to get double materiality details
Params:
consultantId:- to identity collection
surveyId:- id to update doc in collection
*/
const getbysurveyid = async (surveyId,consultantId) => {
const response = await Axios.get(`${CONFIG.BASE_URL}getbysurveyid?surveyId=${surveyId}&consultantId=${consultantId || ''}`, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors'
})
if(response?.data?.data['survey-results']) {
delete response?.data?.data['survey-results'];
}
if(response?.data?.data['workshops']) {
delete response?.data?.data['workshops'];
}
if(response?.data?.data['financial-workshops']) {
delete response?.data?.data['financial-workshops'];
}
return { response, data: () => response?.data?.data || {}, id: response?.data?.data?.id ?? '' };
};
/*
Desc: Api to get surveyresults details
Params:
consultantId:- to identity collection
surveyId:- fetch data based on surveyId
*/
const getsurveyresultsbysurveyId = (surveyId,consultantId) => {
return Axios.get(`${CONFIG.BASE_URL}getsurveyresultsbysurveyId?surveyId=${surveyId}&consultantId=${consultantId || ''}`, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors'
})
}
/*
Desc: Api to get workshops details
Params:
consultantId:- to identity collection
surveyId:- fetch data based on surveyId
*/
const getworkshopsbysurveyId = (surveyId,consultantId) => {
return Axios.get(`${CONFIG.BASE_URL}getworkshopsbysurveyId?surveyId=${surveyId}&consultantId=${consultantId || ''}`, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors'
})
}
/*
Desc: Api to get financialworkshops details
Params:
consultantId:- to identity collection
surveyId:- fetch data based on surveyId
*/
const getfinancialworkshopsbysurveyid = (surveyId,consultantId) => {
return Axios.get(`${CONFIG.BASE_URL}getfinancialworkshopsbysurveyid?surveyId=${surveyId}&consultantId=${consultantId || ''}`, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors'
})
}
/*
Desc: function to get subcollection details
Params:
consultantId:- to identity collection
surveyId:- fetch data based on surveyId
collectionName: indicate collection name
*/
const getSurveyResultForSubcollection = async (surveyId, consultantId, collectionName) => {
try {
if(collectionName == 'survey-results') {
const response = await getsurveyresultsbysurveyId(surveyId,consultantId);
const [surveyResult = []] = response?.data?.data;
let value = parseSnapShortData(response,collectionName);
return value;
}
if(collectionName == 'workshops') {
const response = await getworkshopsbysurveyId(surveyId,consultantId);
const [surveyResult = []] = response?.data?.data;
let value = parseSnapShortData(response,collectionName);
debugger
return value;
}
if(collectionName == 'financial-workshops') {
const response = await getfinancialworkshopsbysurveyid(surveyId,consultantId);
const [surveyResult = []] = response?.data?.data;
let value = parseSnapShortData(response,collectionName);
return value;
}
if (collectionName == 'dashboard-worksheet-audit-logs') {
let url = 'getsurveyDashboardWorksheetAuditLogs';
if (consultantId) {
url = 'getportfolioSurveyDashboardWorksheetAuditLogs';
}
const response = await Axios.get(
`${CONFIG.AUDIT_LOG_URL}${url}?surveyId=${surveyId}&consultantId=${consultantId || ''}`,
{
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
},
);
// const [surveyResult = []] = response?.data?.data;
let value = parseLogSnapShortData(response, collectionName);
return value;
}
if (collectionName == 'methodology-logs') {
let url = 'getSurveyMethodologyLogs';
if (consultantId) {
url = 'getportfolioSurveyMethodologyLogs';
}
const response = await Axios.get(
`${CONFIG.AUDIT_LOG_URL}${url}?surveyId=${surveyId}&consultantId=${consultantId || ''}`,
{
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
},
);
// const [surveyResult = []] = response?.data?.data;
let value = parseLogSnapShortData(response, collectionName);
return value;
}
if (collectionName == 'methodology-task-logs') {
let url = 'getsurveyMethodologyTaskAuditLogModel';
if (consultantId) {
url = 'getportfolioSurveyMethodologyTaskAuditLogModel';
}
const response = await Axios.get(
`${CONFIG.AUDIT_LOG_URL}${url}?surveyId=${surveyId}&consultantId=${consultantId || ''}`,
{
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
},
);
// const [surveyResult = []] = response?.data?.data;
let value = parseLogSnapShortData(response, collectionName);
return value;
}
if (collectionName == 'action-report-task-logs') {
let url = 'getsurveyActionReportLogs';
if (consultantId) {
url = 'getportfolioSurveyActionReportLogs';
}
const response = await Axios.get(
`${CONFIG.AUDIT_LOG_URL}${url}?surveyId=${surveyId}&consultantId=${consultantId || ''}`,
{
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
},
);
// const [surveyResult = []] = response?.data?.data;
let value = parseLogSnapShortData(response, collectionName);
return value;
}
if (collectionName == 'audit-log') {
let url = 'getbysurveyId';
if (consultantId) {
url = 'getbyportfolioSurveyConsultantId';
}
const response = await Axios.get(
`${CONFIG.AUDIT_LOG_URL}${url}?surveyId=${surveyId}&consultantId=${consultantId || ''}`,
{
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
},
);
// const [surveyResult = []] = response?.data?.data;
let value = parseLogSnapShortData(response, collectionName);
return value;
}
} catch (error) {
console.log(error, 'error');
}
};
const getsubmittedSurveyResult = async (surveyId,consultantId) => {
const response = await Axios.get(`${CONFIG.BASE_URL}getsubmittedSurveyResult?surveyId=${surveyId}&consultantId=${consultantId || ''}`, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors'
})
let value = [];
if (response?.data?.data?.length && response?.data?.data[0]['survey-results']?.length) {
value = response?.data?.data[0]['survey-results'].map((v, i) => {
return {
data: () => v,
id: v.id || i,
};
});
}
return value;
}
/*
Desc: function to parse the response and return in expected formate
Params:
response: Response from collection
*/
const parseSnapShortData = response => {
const surveyResult = response?.data?.data ?? [];
let value = [];
if (surveyResult?.length) {
value = surveyResult.map((v, i) => {
return {
data: () => v || {},
id: v.id || v?._id?.toString() || i,
};
});
}
return value;
};
const parseLogSnapShortData = (response, collectionName) => {
const surveyResult = response?.data?.data ?? [];
let value = [];
if (surveyResult?.length) {
value = surveyResult.map((v, i) => {
return {
data: () => v || {},
id: v.id || v?._id?.toString() || i,
};
});
}
return value;
};
/*
Desc: Api to update double materiality
Params:
consultantId:- to identity collection
surveyId:- fetch data based on surveyId
*/
const updateSurveyById = (consultantId,surveyId,currentSurveyId) => {
let url = CONFIG.BASE_URL+'double-materiality/portfolio-surveys-consultants'+'?surveyId='+surveyId ?? '';
url += '&consultantId='+consultantId ?? '';
url += '¤tSurveyId='+currentSurveyId ?? '';
const update = async (data, url) => {
const response = await Axios.put(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
return { update: data => update(data, url), consultantId, surveyId, currentSurveyId };
};
const addToMethodologyLogs = (consultantId, surveyId, currentSurveyId) => {
let url = CONFIG.AUDIT_LOG_URL + 'setSurveyMethodologyLogs';
if (consultantId) {
url = CONFIG.AUDIT_LOG_URL + 'setportfolioSurveyMethodologyLogs';
}
const add = async (data,url) => {
const id = surveyId ? surveyId : currentSurveyId;
console.log(url)
data = {
data,
surveyId: id,
currentSurveyId,
};
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
return {add : (data) => add(data,url) , consultantId, surveyId,currentSurveyId};
}
const setWorkshops = async (consultantId,data) => {
let url = CONFIG.BASE_URL+'double-materiality/set-workshops?';
url += '&consultantId='+consultantId ?? '';
const response = await Axios.post(url,data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
const updateWorkshops = async (consultantId,id,data) => {
let url = CONFIG.BASE_URL+'double-materiality/update-workshops?';
url += '&consultantId='+consultantId ?? '';
url += '&id='+id ?? '';
const response = await Axios.put(url,data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
const setFinancialWorkshops = async (consultantId,data) => {
let url = CONFIG.BASE_URL+'double-materiality/set-financial-workshops?';
url += '&consultantId='+consultantId ?? '';
const response = await Axios.post(url,data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
const updateFinancialWorkshops = async (consultantId,id,data) => {
let url = CONFIG.BASE_URL+'double-materiality/update-financial-workshops?';
url += '&consultantId='+consultantId ?? '';
url += '&id='+id ?? '';
const response = await Axios.put(url,data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
/**
* Desc: Api to get survey result by id
* Params:
surveyResultId:- to fetch data based on the survey result
*/
const getSurveyResultById = async surveyResId => {
const response = await Axios.get(
`${CONFIG.BASE_URL}getsurveyresultsbyId?surveyResultId=${surveyResId}`,
{
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
},
);
return { response, data: () => response?.data?.data || {}, id: response?.data?.data?.id ?? '' };
};
/**
* Desc: Api to update survey result
* Params:
consultantId: to identify the collection
data: to be added in survey-results
*/
const setSurveyResult = async (consultantId, data) => {
let url = CONFIG.BASE_URL + 'double-materiality/set-survey-results?';
url += '&consultantId=' + consultantId ?? '';
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
/**
* Desc: Api to update survey result
* Params:
consultantId: to identify the collection
id: id of the survey-result to be updated
data: to be updated against the survey-result selected
*/
const updateSurveyResultData = async (consultantId, id, data) => {
let url = CONFIG.BASE_URL + 'double-materiality/update-survey-results?';
url += 'consultantId=' + consultantId ?? '';
url += '&id=' + id ?? '';
const response = await Axios.put(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
const addToSurveyResults = (surveyId, currentSurveyId) => {
const url = CONFIG.BASE_URL + 'insertIntoSurveyResults';
const add = async (payload, url) => {
const id = surveyId ? surveyId : currentSurveyId;
const data = {
data: payload,
surveyId: id,
};
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
return { add: payload => add(payload, url), surveyId, currentSurveyId };
};
const addToAuditLogs = (consultantId, surveyId, currentSurveyId) => {
let url = CONFIG.AUDIT_LOG_URL + 'addauditstosurveys';
if (consultantId) {
url = CONFIG.AUDIT_LOG_URL + 'addauditstoportfoilosurveysconsultants';
}
const add = async (data, url) => {
const id = surveyId ? surveyId : currentSurveyId;
console.log(url);
data = {
data,
surveyId: id,
currentSurveyId,
};
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
};
return { add: data => add(data, url), consultantId, surveyId, currentSurveyId };
};
/**
* Adds a new comment to an answered topic.
* @param {Object} params - an object containing surveyId and consultantId.
* @param {Object} body - data to be sent in the request body.
* @returns {Object} - an object containing response, data and status.
* @throws {Error} - if unable to fetch the workshop.
*/
const addAnsweredTopicComment = async (params, body) => {
const { surveyId, consultantId } = params;
let URL = `${localUrl}addAnsweredTopic?surveyId=${surveyId}&consultantId=${consultantId || ""}`;
try {
const response = await Axios.post(URL, body, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return {
response: response?.data?.data || {},
data: () => response?.data?.data,
status: response?.status,
};
} catch (error) {
console.log(error);
throw new Error('Failed to fetch workshop');
}
};
const editAnsweredTopicComment = async (params, body) => {
const { surveyId, consultantId, workshopId } = params;
console.log("workshopId", workshopId)
let URL = `${localUrl}updateAnsweredTopic?surveyId=${surveyId}&consultantId=${consultantId || ""}&workshopId=${workshopId || ""}`;
console.log("URL", URL)
try {
const response = await Axios.put(URL, body, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return {
response: response?.data?.data || {},
data: () => response?.data?.data,
status: response?.status,
};
} catch (error) {
console.log(error);
throw new Error('Failed to fetch workshop');
}
};
/**
* Updates a workshop property based on given endpoint, params and data.
* @param {Object} params - an object containing surveyId, consultantId, workshopId, type and property.
* @param {Object} data - data to be sent in the request body.
* @returns {Object} - an object containing response and a function to get the data.
* @throws {Error} - if unable to fetch the workshop.
*/
const updateWorkshopProperty = async (params, body) => {
const { surveyId, consultantId, workshopId, type, property } = params;
console.log('workshopId', workshopId);
let URL = `${localUrl}double-materiality/update-workshop-property?surveyId=${surveyId}&consultantId=${consultantId ||
''}&workshopId=${workshopId || ''}&type=${type}`;
if (property) URL += `&property=${property}`;
console.log('URL', URL);
try {
const response = await Axios.post(URL, body, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return {
response: response?.data?.data || {},
data: () => response?.data?.data,
status: response?.status,
};
} catch (error) {
console.log(error);
throw new Error('Failed to fetch workshop');
}
};
/*
* Updates consultants or survey Model property based on given endpoint, params and data.
* @param {Object} params - an object containing surveyId, consultantId and property.
* @param {Object} data - data to be sent in the request body.
* @returns {Object} - an object containing response and a function to get the data.
* @throws {Error} - if unable to fetch the workshop.
*/
const updateConsultantsOrSurveyProperty = async (params, body) => {
const { surveyId, consultantId, property } = params;
let URL = `${localUrl}double-materiality/updateConsultantsOrSurvey?surveyId=${surveyId}&consultantId=${consultantId ||
''}`;
if (property) URL += `&property=${property}`;
try {
const response = await Axios.post(URL, body, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return {
response: response?.data?.data || {},
data: () => response?.data?.data,
status: response?.status,
};
} catch (error) {
console.log(error);
throw new Error('Failed to fetch workshop');
}
}
/**
* Creates a new document with the action report task audit log details based on the data provided.
* @param {String} consultantId - consultantId to identify the collection
* @param {String} surveyId - surveyId to fetch data based on
* @param {String} currentSurveyId - currentSurveyId to fetch data based on if surveyId is not provided
* @returns {Object} - an object containing add function and consultantId, surveyId, currentSurveyId
* @throws {Error} - if unable to created the document.
*/
const addToActionReportTaskAuditLogs = (consultantId, surveyId, currentSurveyId) => {
let url = CONFIG.AUDIT_LOG_URL + 'setSurveyActionReportLogs';
if (consultantId) {
url = CONFIG.AUDIT_LOG_URL + 'setportfolioSurveyActionReportLogs';
}
const add = async (data, url) => {
const id = surveyId ? surveyId : currentSurveyId;
data = {
data,
surveyId: id,
currentSurveyId
}
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
return { add: (data) => add(data, url), consultantId, surveyId, currentSurveyId };
}
/**
* Creates a new document with the double materiality audit log details based on the data provided.
* @param {data} params - contains surveyId, consultantId, field and updated by details
* @returns {Object} - an object with status
* @throws {Error} - if unable to created the document.
*/
const addToDoubleMaterialityAuditLog = (consultantId, surveyId) => {
let url = CONFIG.AUDIT_LOG_URL + 'setDoubleMaterialityAuditLogs';
const add = async (data, url) => {
const response = await Axios.post(url, data, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors',
});
return response;
}
return { add: (data) => add(data, url), consultantId, surveyId };
}
/**
* Fetches the double materiality audit logs by its surveyId.
* @param {surveyId} params - contains the surveyId of which the details are to be fetched.
* @returns {Object} - an object containing audit log details.
* @throws {Error} - if unable to fetch the details.
*/
const getDoubleMaterialityAuditLogs = async (surveyId) => {
const response = await Axios.get(`${CONFIG.AUDIT_LOG_URL}getDoubleMaterialityAuditLogs?surveyId=${surveyId}`, {
headers: {
'x-api-key': CONFIG.X_API_KEY,
},
mode: 'no-cors'
})
let value = [];
if (response?.data?.data?.length && response?.data?.data[0]['survey-results']?.length) {
value = response?.data?.data[0]['survey-results'].map((v, i) => {
return {
data: () => v,
id: v.id || i
}
})
}
return value;
}
export {
parseSnapShortData,
getsurveyresultsbysurveyId,
getworkshopsbysurveyId,
getfinancialworkshopsbysurveyid,
getsubmittedSurveyResult,
getSurveyResultForSubcollection,
addToMethodologyLogs,
updateSurveyById,
getbysurveyid,
createDoubleMaterialitySurvey,
updateDoubleMaterialitySurvey,
setWorkshops,
updateWorkshops,
setFinancialWorkshops,
updateFinancialWorkshops,
getSurveyResultById,
setSurveyResult,
updateSurveyResultData,
addToAuditLogs,
addToSurveyResults,
};