Advertisement
KoctrX

Untitled

Nov 6th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachAssessmentsAttributes(application, data, preset, stringArray = [], skipProcentage = false) {
  2.     const applicationQuestions = application?.criticalRequirements?.applicationQuestions || [];
  3.     for (const keyString of stringArray) {
  4.         const applicationQuestion = applicationQuestions.find(aq => aq?.question?.qualityAttribute?.key === keyString);
  5.  
  6.         if (applicationQuestion) {
  7.             data[keyString] = applicationQuestion.answer;
  8.         } else {
  9.             data[keyString] = example[keyString];
  10.         }
  11.     }
  12.  
  13.     const assessments = ["clarifications", "1st-interview", "2nd-interview", "post_interview_survey"];
  14.     const parsedAvgValues = [];
  15.     for (const assessmentName of assessments) {
  16.         const details = application?.criticalRequirements[assessmentName];
  17.         if (!details) continue;
  18.  
  19.         const scores = application?.assessments?.[assessmentName]?.scores || [];
  20.         if (!scores || !scores.length) continue;
  21.  
  22.  
  23.         const result = details.reduce((res, curr, index) => {
  24.             const key = res.find(r => r.key === curr?.question?.qualityAttribute?.key);
  25.             if (scores[index] === null) return res; // skip nulls
  26.  
  27.             const score = { ...curr, score: scores[index] };
  28.             if (key) { key.stack.push(score); } else {
  29.                 res.push({ key: curr?.question?.qualityAttribute?.key, name: curr?.question?.qualityAttribute?.name, stack: [score] });
  30.             }
  31.  
  32.             return res;
  33.         }, []).map(item => {
  34.             const arrayStack = (item?.stack || []).filter(item => item.score);
  35.             const score = arrayStack.reduce((r, it) => r + (parseFloat(it.score) || 0), 0);
  36.             const avg = parseFloat(((score / arrayStack.length) || 0).toFixed(3));
  37.  
  38.             return { ...item, avg };
  39.         }).map(q => _.omit(q, ['stack']));
  40.  
  41.         parsedAvgValues.push(...result);
  42.     }
  43.  
  44.     for (const key in preset) {
  45.         if (stringArray.includes(key)) continue;
  46.         const keyInStack = preset[key];
  47.         const item = parsedAvgValues.find(r => r.key === key);
  48.  
  49.         if (skipProcentage) {
  50.             data[keyInStack] = parseFloat(item?.avg);
  51.         } else {
  52.             data[keyInStack] = parseFloat((((item?.avg || 0) / 7) || 0).toFixed(3));
  53.         }
  54.     }
  55.  
  56.     return data;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement