Skip to content

Commit

Permalink
Issue #CO-942 merge: Merge pull request #342 from shubhambansaltarent…
Browse files Browse the repository at this point in the history
…o/bug/942-reports

Issue #CO-942: Report header issue
  • Loading branch information
vinukumar-vs authored May 10, 2024
2 parents 59d9212 + f07f83f commit e469e65
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
82 changes: 51 additions & 31 deletions src/helpers/programHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class ProgramServiceHelper {
return axios(option);
}

async getCollectionWithProgramId(program_id, req) {
async getCollectionWithProgramId(program_id, req, frameworkCategories) {
const program = await this.getProgramDetails(program_id);
const queryFilter = {
filters: {
Expand All @@ -149,9 +149,10 @@ class ProgramServiceHelper {
status: ['Draft'],
primaryCategory: program.dataValues.target_collection_category
},
fields: ['name', 'medium', 'gradeLevel', 'subject', 'primaryCategory', 'chapterCount', 'acceptedContents', 'rejectedContents', 'openForContribution', 'chapterCountForContribution', 'mvcContributions'],
fields: ['name', 'primaryCategory', 'chapterCount', 'acceptedContents', 'rejectedContents', 'openForContribution', 'chapterCountForContribution', 'mvcContributions'],
limit: 1000
};
queryFilter.fields = [...queryFilter.fields, ...frameworkCategories];
return this.searchWithProgramId(queryFilter, req);
}

Expand Down Expand Up @@ -250,21 +251,21 @@ class ProgramServiceHelper {
return promise;
}

handleMultiProgramDetails(resGroup, programObjs, targetType = 'collections') {
handleMultiProgramDetails(resGroup, programObjs, targetType = 'collections', frameworkCategories) {
const multiProgramDetails = _.map(resGroup, (resData) => {
try {
if (targetType === 'collections') {
return this.prepareTableData(resData);
return this.prepareTableData(resData, frameworkCategories);
} else if(targetType === 'searchCriteria') {
return this.prepareContentsTableData(resData, programObjs);
return this.prepareContentsTableData(resData, programObjs, frameworkCategories);
}
} catch(err) {
throw err
}
});
return multiProgramDetails;
}
prepareContentsTableData(resData, programObjs) {
prepareContentsTableData(resData, programObjs, frameworkCategories) {
try {
let contents = [];
let tableData = [];
Expand All @@ -280,12 +281,15 @@ class ProgramServiceHelper {
}), (content) => {
const program = programObjs[content.programId];
let result = {};
frameworkCategories.forEach((cat)=>{
if(Array.isArray(content[cat])){
result[cat] = content[cat] && content[cat].length ? content[cat].join(', ') : ''
}else{
result[cat] = content[cat] && content[cat].length ? content[cat] : ''
}
})
result[`Content Name`] = content.name || '';
result['Framework'] = content.framework || '';
result['Board'] = content.board || '';
result['Medium'] = content.medium && content.medium.length ? content.medium.join(', ') : '';
result['Class'] = content.gradeLevel && content.gradeLevel.length ? content.gradeLevel.join(', ') : '';
result['Subject'] = content.subject && content.subject.length ? content.subject.join(', ') : '';
result['Creator'] = content.creator || '';
result['Status'] = null;
if (content.status === 'Live') {
Expand All @@ -309,7 +313,7 @@ class ProgramServiceHelper {
}
}

prepareTableData (resData) {
prepareTableData (resData, frameworkCategories) {
try {
const collectionList = resData[0].data.result && resData[0].data.result.content || [],
sampleContentWithOrgId = resData[1].data.result && resData[1].data.result.aggregations || [],
Expand All @@ -332,10 +336,14 @@ class ProgramServiceHelper {
tableData = _.map(openForContributionCollections, (collection) => {
const result = {};
// sequence of columns in tableData
frameworkCategories.forEach((cat)=>{
if(Array.isArray(collection[cat])){
result[cat] = collection[cat] && collection[cat].length ? collection[cat].join(', ') : ''
}else{
result[cat] = collection[cat] && collection[cat].length ? collection[cat] : ''
}
})
result[`${collection.primaryCategory} Name`] = collection.name || '';
result['Medium'] = collection.medium || '';
result['Class'] = collection.gradeLevel && collection.gradeLevel.length ? collection.gradeLevel.join(', ') : '';
result['Subject'] = collection.subject || '';
result['Number of Chapters'] = collection.chapterCountForContribution || collection.chapterCount || 0;
result['Nominations Received'] = 0;
result['Samples Received'] = 0;
Expand Down Expand Up @@ -455,7 +463,7 @@ class ProgramServiceHelper {
return axios(option);
}

getCollectionHierarchy(req, program_id, openForContribution) {
getCollectionHierarchy(req, program_id, openForContribution, frameworkCategories) {
return new Promise((resolve, reject) => {
this.getCollectionWithProgramId(program_id, req).then((res_collection) => {
const collectionArr = res_collection.data && res_collection.data.result && res_collection.data.result.content || [];
Expand All @@ -475,7 +483,7 @@ class ProgramServiceHelper {
});
}

const contentCount = this.approvedContentCount(hierarchyArr, program_id);
const contentCount = this.approvedContentCount(hierarchyArr, program_id, frameworkCategories);
resolve(contentCount);
} catch (err) {
reject('programServiceException: error in counting the approved contents');
Expand All @@ -489,14 +497,14 @@ class ProgramServiceHelper {
});
}

approvedContentCount(collectionHierarchy, program_id) {
approvedContentCount(collectionHierarchy, program_id, frameworkCategories) {
const collectionWithApprovedContent = _.map(collectionHierarchy, collection => {
this.acceptedContents = _.uniq(collection.acceptedContents) || [];
this.rejectedContents = _.uniq(collection.rejectedContents) || [];
this.collectionData = {};
this.collectionData['totalContentsReviewed'] = (_.union(this.acceptedContents, this.rejectedContents)).length;
this.collectionData['contributionsReceived'] = 0;
this.collectionLevelCount(collection);
this.collectionLevelCount(collection, frameworkCategories);

// Count of contribution

Expand All @@ -515,14 +523,18 @@ class ProgramServiceHelper {
return {program_id: program_id, collection: collectionWithApprovedContent};
}

collectionLevelCount(data) {
collectionLevelCount(data, frameworkCategories) {
const self = this;
if ((data.primaryCategory === 'Digital Textbook' || data.primaryCategory === 'Course' || data.primaryCategory === 'Content Playlist') && data.visibility === 'Default') {
frameworkCategories.forEach((cat)=>{
if(Array.isArray(content[cat])){
result[cat] = collection[cat] && collection[cat].length ? collection[cat].join(', ') : ''
}else{
result[cat] = collection[cat] && collection[cat].length ? collection[cat] : ''
}
})
this.collectionData['name'] = data.name;
this.collectionData['identifier'] = data.identifier;
this.collectionData['grade'] = _.isArray(data.gradeLevel) ? data.gradeLevel.join(", ") : data.gradeLevel || '';
this.collectionData['medium'] = _.isArray(data.medium) ? data.medium.join(", ") : data.medium || '';
this.collectionData['subject'] = _.isArray(data.subject) ? data.subject.join(", ") : data.subject || '';
this.collectionData['count'] = this.acceptedContents.length;
this.collectionData['chapter'] = [];
this.recursive = true;
Expand Down Expand Up @@ -552,7 +564,7 @@ class ProgramServiceHelper {
}
if (data.children && this.recursive) {
this.recursive = false;
_.forEach(data.children, child => self.collectionLevelCount(child));
_.forEach(data.children, child => self.collectionLevelCount(child, frameworkCategories));
}
}

Expand All @@ -579,7 +591,7 @@ class ProgramServiceHelper {
}
}

textbookLevelContentMetrics(collectedData) {
textbookLevelContentMetrics(collectedData, frameworkCategories) {
return new Promise((resolve, reject) => {
forkJoin(..._.map(collectedData, data => this.getProgramDetails(data.program_id))).subscribe(details => {
try {
Expand All @@ -592,10 +604,14 @@ class ProgramServiceHelper {
const overalData = _.map(collectedData, data => {
if (data.collection && data.collection.length) {
const tableObj = _.map(data.collection, (collection) => {
frameworkCategories.forEach((cat)=>{
if(Array.isArray(content[cat])){
result[cat] = collection[cat] && collection[cat].length ? collection[cat].join(', ') : ''
}else{
result[cat] = collection[cat] && collection[cat].length ? collection[cat] : ''
}
})
const final = {};
final['Medium'] = collection.medium;
final['Grade'] = collection.grade;
final['Subject'] = collection.subject;
final[`${collection.primaryCategory} Name`] = collection.name;
final['Total Number of Chapters'] = collection.chapter ? collection.chapter.length : 0;
final['Total Contents Contributed'] = collection.contributionsReceived ? collection.contributionsReceived : 0;
Expand Down Expand Up @@ -623,7 +639,7 @@ class ProgramServiceHelper {
});
}

chapterLevelContentMetrics(collectedData) {
chapterLevelContentMetrics(collectedData, frameworkCategories) {
return new Promise((resolve, reject) => {
forkJoin(..._.map(collectedData, data => this.getProgramDetails(data.program_id))).subscribe(details => {
try {
Expand All @@ -637,10 +653,14 @@ class ProgramServiceHelper {
if (data.collection && data.collection.length) {
const tableObj = _.map(data.collection, (collection) => {
const unitDetails = _.map(collection.chapter, unit => {
frameworkCategories.forEach((cat)=>{
if(Array.isArray(content[cat])){
result[cat] = collection[cat] && collection[cat].length ? collection[cat].join(', ') : ''
}else{
result[cat] = collection[cat] && collection[cat].length ? collection[cat] : ''
}
})
const final = {};
final['Medium'] = collection.medium;
final['Grade'] = collection.grade;
final['Subject'] = collection.subject;
final[`${collection.primaryCategory} Name`] = collection.name;
final['Chapter Name'] = unit.name;
final['Total Contents Contributed'] = unit.contentsContributed || 0;
Expand Down
16 changes: 8 additions & 8 deletions src/service/programService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ function getNominationsList(req, response) {
}else if (data.request.limit === 0) {
model.nomination.findAll({
where: {
...findQuery
...findQuery
},
attributes: [...data.request.fields || []]
}).then(async (result) => {
Expand Down Expand Up @@ -1456,7 +1456,7 @@ async function downloadProgramDetails(req, res) {
}
promiseRequests = _.map(filteredPrograms, (program) => {
if (!data.request.filters.targetType || data.request.filters.targetType === 'collections') {
return [programServiceHelper.getCollectionWithProgramId(program, req), programServiceHelper.getSampleContentWithOrgId(program, req),programServiceHelper.getSampleContentWithCreatedBy(program, req), programServiceHelper.getContributionWithProgramId(program, req), programServiceHelper.getNominationWithProgramId(program), programServiceHelper.getOveralNominationData(program)];
return [programServiceHelper.getCollectionWithProgramId(program, req, data.request.filters.frameworkCategories), programServiceHelper.getSampleContentWithOrgId(program, req),programServiceHelper.getSampleContentWithCreatedBy(program, req), programServiceHelper.getContributionWithProgramId(program, req), programServiceHelper.getNominationWithProgramId(program), programServiceHelper.getOveralNominationData(program)];
} else if(data.request.filters.targetType === 'searchCriteria') {
return[programServiceHelper.getContentContributionsWithProgramId(program, req)];
}
Expand All @@ -1466,7 +1466,7 @@ async function downloadProgramDetails(req, res) {
try{
const chunkNumber = (!data.request.filters.targetType || data.request.filters.targetType === 'collections') ? 6 : 1;
const combainedRes = _.chunk(responseData, chunkNumber);
const programDetailsArray = programServiceHelper.handleMultiProgramDetails(combainedRes, programObjs, data.request.filters.targetType);
const programDetailsArray = programServiceHelper.handleMultiProgramDetails(combainedRes, programObjs, data.request.filters.targetType, data.request.filters.frameworkCategories);
const tableData = _.reduce(programDetailsArray, (final, data, index) => {
final.push({program_id: filteredPrograms[index], values: data});
return final;
Expand Down Expand Up @@ -2439,7 +2439,7 @@ async function generateApprovedContentReport(req, res) {
if (filteredPrograms.length) {
try {
const openForContribution = data.request.filters.openForContribution || false;
const requests = _.map(filteredPrograms, program => programServiceHelper.getCollectionHierarchy(req, program, openForContribution));
const requests = _.map(filteredPrograms, program => programServiceHelper.getCollectionHierarchy(req, program, openForContribution), data.request.filters.frameworkCategories);
const aggregatedResult = await Promise.all(requests);
_.forEach(aggregatedResult, result => {
cacheManager_programReport.set({ key: `approvedContentCount_${result.program_id}`, value: result },
Expand All @@ -2453,15 +2453,15 @@ async function generateApprovedContentReport(req, res) {
});

if (data.request.filters.report === 'textbookLevelReport') {
const textbookLevelReport = await programServiceHelper.textbookLevelContentMetrics([...aggregatedResult, ...cacheData]);
const textbookLevelReport = await programServiceHelper.textbookLevelContentMetrics([...aggregatedResult, ...cacheData], data.request.filters.frameworkCategories);
rspObj.result = {
tableData: textbookLevelReport
}
rspObj.responseCode = 'OK'
loggerService.exitLog({responseCode: rspObj.responseCode}, logObject);
return res.status(200).send(successResponse(rspObj));
} else if (data.request.filters.report === 'chapterLevelReport') {
const chapterLevelReport = await programServiceHelper.chapterLevelContentMetrics([...aggregatedResult, ...cacheData]);
const chapterLevelReport = await programServiceHelper.chapterLevelContentMetrics([...aggregatedResult, ...cacheData], data.request.filters.frameworkCategories);
rspObj.result = {
tableData: chapterLevelReport
}
Expand All @@ -2482,15 +2482,15 @@ async function generateApprovedContentReport(req, res) {
} else {
try {
if (data.request.filters.report === 'textbookLevelReport') {
const textbookLevelReport = await programServiceHelper.textbookLevelContentMetrics([...cacheData]);
const textbookLevelReport = await programServiceHelper.textbookLevelContentMetrics([...cacheData], frameworkCategories);
rspObj.result = {
tableData: textbookLevelReport
}
rspObj.responseCode = 'OK'
loggerService.exitLog({responseCode: rspObj.responseCode}, logObject);
return res.status(200).send(successResponse(rspObj));
} else if (data.request.filters.report === 'chapterLevelReport') {
const chapterLevelReport = await programServiceHelper.chapterLevelContentMetrics([...cacheData]);
const chapterLevelReport = await programServiceHelper.chapterLevelContentMetrics([...cacheData], frameworkCategories);
rspObj.result = {
tableData: chapterLevelReport
}
Expand Down

0 comments on commit e469e65

Please sign in to comment.