Skip to content

Commit

Permalink
Merge pull request #29 from dOrgTech/fix/voting-on
Browse files Browse the repository at this point in the history
Fixed Voting on Proposal
  • Loading branch information
EightRice authored Jun 19, 2024
2 parents a332836 + b684261 commit 17dbb6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ yarn-error.log*

.idea
.cosine
yarn.lock
yarn.lock
test.js
.vscode
50 changes: 29 additions & 21 deletions components/choices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const updateChoiceById = async (req, response) => {
let i = 0;

try {
let oldVote = null;
const values = getInputFromSigPayload(payloadBytes);

const payloadDate = getTimestampFromPayloadBytes(payloadBytes);
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand 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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "./server.js",
"scripts": {
"test": "jest",
"start":"node ./server.js",
"start": "node ./server.js",
"dev": "nodemon ./server.js"
},
"jest": {
Expand Down

0 comments on commit 17dbb6d

Please sign in to comment.