diff --git a/generics/kafka/consumers/submissions.js b/generics/kafka/consumers/submissions.js index 9c56c4d3..da043375 100644 --- a/generics/kafka/consumers/submissions.js +++ b/generics/kafka/consumers/submissions.js @@ -25,7 +25,7 @@ var messageReceived = function (message) { try { let parsedMessage = JSON.parse(message.value); - + console.log(parsedMessage ,"messageReceived from kafka") let submissionDocument = { "_id" : parsedMessage._id.toString(), "status" : parsedMessage.status, @@ -59,6 +59,7 @@ var errorTriggered = function (error) { return new Promise(function (resolve, reject) { try { + console.log(error ,"errorTriggered from kafka") return resolve(error); } catch (error) { return reject(error); diff --git a/module/userProjects/helper.js b/module/userProjects/helper.js index 8f290ec1..00b514de 100644 --- a/module/userProjects/helper.js +++ b/module/userProjects/helper.js @@ -803,64 +803,49 @@ module.exports = class UserProjectsHelper { return new Promise(async (resolve, reject) => { try { - let update = {}; - let tasksUpdated; + let updateSubmission = []; let projectDocument = await projectQueries.projectDocument( { _id: projectId, "tasks._id": taskId }, [ - "tasks.$.submissions" + "tasks" ]); - let tasks = projectDocument[0].tasks; - if ( tasks && tasks.length > 0 ) { - tasks = projectDocument[0].tasks[0]; - } - - let submissions = tasks.submissions; - let pushToSubmissionArray = false; - - if ( !submissions && !submissions.length > 0 ){ - pushToSubmissionArray = true; - } - - let checkSubmissionExist = submissions.filter(submission => submission._id == updatedData._id); - - if ( !checkSubmissionExist.length > 0 ) { - pushToSubmissionArray = true; + let currentTask = projectDocument[0].tasks.find(task => task._id == taskId); + let submissions = currentTask.submissions && currentTask.submissions.length > 0 ? currentTask.submissions : [] ; + + // if submission array is empty + if ( !submissions && !submissions.length > 0 ) { + updateSubmission.push(updatedData); } + + // submission not exist + let checkSubmissionExist = submissions.findIndex(submission => submission._id == updatedData._id); - if ( pushToSubmissionArray === true ) { - - update["tasks.$." + "submissions"] = updatedData; + if ( checkSubmissionExist == -1 ) { - tasksUpdated = - await projectQueries.findOneAndUpdate({ - _id: projectId, - "tasks._id": taskId - }, { $push: update }); + updateSubmission = submissions; + updateSubmission.push(updatedData); } else { + //submission exist + submissions[checkSubmissionExist] = updatedData; + updateSubmission = submissions; + } - const index = submissions.indexOf(checkSubmissionExist[0]); - - if ( submissions[index].status !== CONSTANTS.common.COMPLETED_STATUS ) { - submissions[index] = updatedData; - } - - update["tasks.$." + "submissions"] = submissions; - tasksUpdated = - await projectQueries.findOneAndUpdate({ - _id: projectId, - "tasks._id": taskId - }, { $set: update }); + let tasksUpdated = await projectQueries.findOneAndUpdate({ + "_id": projectId, + "tasks._id": taskId + }, { + $set: { + "tasks.$.submissions": updateSubmission + } + }); - } - return resolve(tasksUpdated); - + } catch (error) { return reject(error); }