diff --git a/.gitignore b/.gitignore index 369b002..809514e 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,6 @@ yarn-error.log* .idea .cosine -yarn.lock \ No newline at end of file +yarn.lock +test.js +.vscode \ No newline at end of file diff --git a/components/choices/index.js b/components/choices/index.js index 1ff7fb9..d57d6ba 100644 --- a/components/choices/index.js +++ b/components/choices/index.js @@ -42,6 +42,7 @@ const updateChoiceById = async (req, response) => { let i = 0; try { + let oldVote = null; const values = getInputFromSigPayload(payloadBytes); const payloadDate = getTimestampFromPayloadBytes(payloadBytes); @@ -114,17 +115,19 @@ const updateChoiceById = async (req, response) => { const isVoted = await db_connect .collection('Choices') .find({ - pollID: poll._id, + pollID: poll._id, walletAddresses: { $elemMatch: { address: address } }, }) .toArray(); + if (isVoted.length > 0) { - const oldVote = await db_connect.collection("Choices").findOne({ - _id: ObjectId(isVoted[0].walletAddresses[0].choiceId), + const oldVoteObj = isVoted[0].walletAddresses.find(x => x.address === address); + oldVote = await db_connect.collection("Choices").findOne({ + _id: ObjectId(oldVoteObj.choiceId), }); - const oldSignaturePayload = oldVote.walletAddresses[0].payloadBytes; + const oldSignaturePayload = oldVote.walletAddresses[0].payloadBytes if (oldSignaturePayload) { const oldSignatureDate = getTimestampFromPayloadBytes(oldSignaturePayload); @@ -135,14 +138,16 @@ const updateChoiceById = async (req, response) => { } } - const cidLink = await uploadToIPFS( - getIPFSProofFromPayload(payloadBytes, signature) - ); - if (!cidLink) { - throw new Error( - "Could not upload proof to IPFS, Vote was not registered. Please try again later" - ); - } + // const ipfsProof = getIPFSProofFromPayload(payloadBytes, signature) + // const cidLink = await uploadToIPFS(ipfsProof).catch(error => { + // console.error('IPFS Error', error) + // return null; + // }); + // if (!cidLink) { + // throw new Error( + // "Could not upload proof to IPFS, Vote was not registered. Please try again later" + // ); + // } // TODO: Optimize this Promise.all await Promise.all( @@ -157,7 +162,8 @@ const updateChoiceById = async (req, response) => { signature, }; - walletVote.cidLink = cidLink; + // TODO: Enable this when the IPFS CID is added to the walletVote object + // walletVote.cidLink = cidLink; const choice = await db_connect .collection("Choices") @@ -186,14 +192,16 @@ const updateChoiceById = async (req, response) => { const coll1 = db_connect.collection("Choices"); // const coll2 = db_connect.collection("Polls"); - + // Important:: You must pass the session to the operations - await coll1.updateOne( - { _id: ObjectId(oldVote._id) }, - remove, - { remove: true }, - { session } - ); + if (oldVote) { + await coll1.updateOne( + { _id: ObjectId(oldVote._id) }, + remove, + { remove: true }, + { session } + ); + } await coll1.updateOne({ _id: ObjectId(choice._id) }, newData, { session, @@ -285,7 +293,7 @@ const updateChoiceById = async (req, response) => { response.json({ success: true }); } catch (error) { - console.log("error: ", error.message); + console.log("error: ", error); response.status(400).send({ message: error.message, }); diff --git a/package.json b/package.json index ecaec54..cc78c5f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "./server.js", "scripts": { "test": "jest", - "start":"node ./server.js", + "start": "node ./server.js", "dev": "nodemon ./server.js" }, "jest": {