async.map(students, function(student, callback){ var newObject = student.toObject(); newObject.xsre = { "gradeLevel": /*"N/A"*/"", "schoolYear": /*"N/A"*/"", "schoolName": /*"N/A"*/"", "attendanceCount": [], "behaviorCount": [], "riskFlag": [], "onTrackToGraduate": /*"N/A"*/"", "latestDate": "" }; var realKey = cacheService.getKeyForStudentSummary(student.get("_id"), orgId); cache.get(realKey, function(err, studentFromCache){ if(err){ return callback(null, newObject); } if (_.isUndefined(studentFromCache)){ //the student is in the mongoDB but not the Redis Cache; request the xSre from HostedZone and update the cache. var brokerRequest = new Request({ externalServiceId: organization.externalServiceId, personnelId: organization.personnelId, authorizedEntityId: organization.authorizedEntityId }); StudentController.refreshStudentSummary(brokerRequest, newObject, req.params.organizationId, function(refreshedStudent) { if(!_.isUndefined(refreshedStudent) && !refreshedStudent.isUnavailable){ newObject.xsre = refreshedStudent; //these students probably don't have names in the DB; if so we need to update them from the xsre data if (!newObject.first_name && refreshedStudent.firstName || !newObject.last_name && refreshedStudent.lastName) { newObject.first_name = newObject.first_name || refreshedStudent.firstName; newObject.last_name = newObject.last_name || refreshedStudent.lastName; Student.findOneAndUpdate({_id: newObject._id}, newObject, {upsert: true}, function(err, results){ if (err) { console.log(err); } }); } return callback(null, newObject); } }); } else if (studentFromCache.isUnavailable) { return callback(null, undefined); } else { newObject.xsre = studentFromCache; callback(null, newObject); } }); }, function(err, results){
user.save(function(err){ if(err){ return res.sendError(err); } var key = cacheService.getKeyForStudentSummary(req.params.studentId, orgId); cache.get(key, function(err, xsre){ if(err || !xsre){ return res.sendSuccess(res.__('data_deleted')); } if(studentId.toString() in xsre){ delete xsre[studentId.toString()]; /** * Restore cache again */ cache.set(key, xsre, { ttl: 86400 }, function(){ res.sendSuccess(res.__('data_deleted')); }); } else{ res.sendSuccess(res.__('data_deleted')); } }); });