From b781083964baf82ccb23f13c6afcd5b2b466e009 Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Wed, 12 Jan 2022 13:21:01 +0530 Subject: [PATCH] project submission fix --- generics/kafka/consumers/submissions.js | 3 +- module/userProjects/helper.js | 69 ++++++++++--------------- 2 files changed, 29 insertions(+), 43 deletions(-) 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 40cb8f74..08fddadd 100644 --- a/module/userProjects/helper.js +++ b/module/userProjects/helper.js @@ -795,64 +795,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); }