getSurveys -- Working code for reference
Thu Sep 19 2024 04:35:18 GMT+0000 (Coordinated Universal Time)
Saved by @alfred.roshan
getSurveys = async ( surveyId, filterByStakeholderTypeValue, filterByCountryValue, filterByContinentValue, consultantId, ) => { try { const { assessmentType } = this.state; // Fetch survey details const surveyDetails = await getbysurveyid(surveyId, consultantId); const survey = surveyDetails.data(); console.log("survey", survey); const submittedSurveyResults = await getsubmittedSurveyResult(surveyId, consultantId); const tabComments = survey["tab-comments"] || []; const surveyConditions = () => (Object.keys(survey).length > 0 && survey?.approvedTopics) ? survey?.approvedTopics : false; const workshops = await getSurveyResultForSubcollection(surveyId, consultantId, 'workshops') const financialWorkshops = await getSurveyResultForSubcollection(surveyId, consultantId, 'financial-workshops') // this.setState({ // survey: { ...survey, id: survey.id }, // // loading: false, // }); // Object to hold all state changes to schedule the state updations const newState = {} console.log("newState 1", newState) newState.survey = { ...survey, id: survey.id } if (survey.answeredTopics) { let allTopics = survey?.surveyTopicsShort ? [ ...survey.surveyTopicsShort.business, ...survey.surveyTopicsShort.social, ...survey.surveyTopicsShort.human, ...survey.surveyTopicsShort.environment, ...survey.surveyTopicsShort.leadership, ] : []; newState.materialityAssessmentTopics = survey.answeredTopics; newState.financialConsolidatedReports = [...survey.answeredTopics, ...allTopics] .filter((v, i, a) => a.findIndex(t => t.topic === v.topic && t.id === v.id) === i) .filter(item => item.entryType !== 'comment' && item.entryType !== 'added-entry') .sort((a, b) => { if (a.framework == 'CSRD') { let _a = CSRDAllTopics?.find( item2 => item2['mainId'] == a['mainId'] && item2['topic'] == a['topic'], )?.sortIndex || 0; let _b = CSRDAllTopics?.find( item2 => item2['mainId'] == b['mainId'] && item2['topic'] == b['topic'], )?.sortIndex || 0; return _a < _b ? -1 : _a > _b ? 1 : 0; } if (a.framework == 'GRI') { let _a = a.id; let _b = b.id; return _a < _b ? -1 : _a > _b ? 1 : 0; } if (a.topic < b.topic) return -1; if (a.topic > b.topic) return 1; }) ?.sort((a, b) => { if (a.isCustom || b.isCustom) { // Move objects with 'isCustom' property to the end if (a.isCustom && !b.isCustom) { return 1; } else if (!a.isCustom && b.isCustom) { return -1; } else { // If both have 'isCustom' or neither have 'isCustom', sort by customSortId if (a.customSortId && b.customSortId) { return a.customSortId > b.customSortId ? 1 : -1; } else { return a.topic > b.topic ? 1 : -1; } } } }) .sort((a, b) => { if (this.renderESG(a.dimension) == 'Environmental') return -1; if (this.renderESG(b.dimension) == 'Environmental') return 1; if (this.renderESG(a.dimension) == 'Social') return -1; if (this.renderESG(b.dimension) == 'Social') return 1; if (this.renderESG(a.dimension) == 'Governance') return -1; if (this.renderESG(b.dimension) == 'Governance') return 1; if (this.renderESG(a.dimension) < this.renderESG(b.dimension)) return -1; if (this.renderESG(a.dimension) > this.renderESG(b.dimension)) return 1; }) .sort((a, b) => { if (this.renderESG(a.dimension) == 'Environmental') return -1; if (this.renderESG(b.dimension) == 'Environmental') return 1; if (this.renderESG(a.dimension) == 'Social') return -1; if (this.renderESG(b.dimension) == 'Social') return 1; if (this.renderESG(a.dimension) == 'Governance') return -1; if (this.renderESG(b.dimension) == 'Governance') return 1; if (this.renderESG(a.dimension) < this.renderESG(b.dimension)) return -1; if (this.renderESG(a.dimension) > this.renderESG(b.dimension)) return 1; }); newState.allTopics = allTopics; newState.answeredTopics = survey.answeredTopics ? [...survey.answeredTopics, ...allTopics].filter( (v, i, a) => a.findIndex(t => t.topic === v.topic && t.id === v.id) === i, ) : []; newState.addedCommentTopics = Array.from(survey.addedCommentTopics || []); } else { // this.setState({ loading: false }); } const properties = { impactMaterialityMethodology: true, impactMaterialityScoringMethodology: true, financialMaterialityMethodology: true, financialMaterialityScoringMethodology: true, financialMappingMethodology: true, materialityAssessmentTopics: true, verifiedTopics: true, scoringSetting: true, reasonForMaxOrAverage: true, isNotFirstTimeOpeningImpactWorksheet: true, isNotFirstTimeOpeningFinancialWorksheet: true }; for (const key in properties) { if (survey[key] !== undefined) { newState[key] = survey[key]; } } // Default value for stakeholderTypes newState.stakeholderTypes = survey.stakeholderTypes || _stakeholderTypes; const getProperDate = date => { if (date.toDate) { return date.toDate(); } else { return date; } }; // Survey List operations this.handleSubmittedSurveyListOperation(submittedSurveyResults, surveyConditions) const workshopList = workshops.map(workshop => { return { ...workshop, id: workshop.id, answeredEntries: workshop?.['answeredEntries']?.map(itm => ({ ...itm, Scale: itm['Scale'] == '0 - None' ? '1 - Minimal' : itm['Scale'] || '', Scope: itm['Scope'] == '0 - No impact' ? '1 - Limited' : itm['Scope'] || '', Likelihood: itm['Likelihood'] == '0 - Very Unlikely (Has not occurred, and the possible chance of occuring is low)' ? '1 - Rare' : itm['Likelihood'] || '', Remediability: itm['Remediability'] == '0 - Very Easy to remedy' ? '1 - Relatively easy to remedy' : itm['Remediability'] || '', })), }; }); const financialWorkshopList = financialWorkshops.map(workshop => ({ ...workshop, id: workshop.id, answeredEntries: workshop?.['answeredEntries']?.map(itm => ({ ...itm, Likelihood: itm['Likelihood'] == '0 - Very Unlikely (Has not occurred, and the possible chance of occuring is low)' ? '1 - Rare' : itm['Likelihood'] || '', Magnitude: itm['Magnitude'] == '0 - Not material (0%)' ? '1 - Minimal Impact' : itm['Magnitude'] || '', })), })); const tabCommentsList = tabComments.map(comment => { if (!comment.comment) return; // Workaround for damaged data --> remove once data is changed from DB return { ...comment, id: comment.id, // date: getProperDate(comment.date) date: comment.date.toDate ? comment.date.toDate() : comment.date, }; }); newState.workshops = workshopList newState.financialworkshops = financialWorkshopList newState.tabComments = tabCommentsList newState.loading = false console.log("newState", newState); this.setState(newState); } catch (ex) { this.setState({ loading: false }); } };
Comments