-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
634 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const Accessibility = { | ||
PUBLIC: "public", | ||
SOCIETY: "society", | ||
}; | ||
|
||
module.exports = { | ||
Accessibility, | ||
}; |
29 changes: 29 additions & 0 deletions
29
backend/packages/backend/src/features/proposals/checkProposalContent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { HttpError } = require("../../exc"); | ||
const { ContentType } = require("../../constants"); | ||
|
||
function checkProposalContent(data) { | ||
const { title, content, contentType } = data; | ||
|
||
if (!title) { | ||
throw new HttpError(400, { title: ["Title is missing"] }); | ||
} | ||
|
||
if (!content) { | ||
throw new HttpError(400, { content: ["Content is missing"] }); | ||
} | ||
|
||
if (!contentType) { | ||
throw new HttpError(400, { contentType: ["Content type is missing"] }); | ||
} | ||
|
||
if ( | ||
contentType !== ContentType.Markdown && | ||
contentType !== ContentType.Html | ||
) { | ||
throw new HttpError(400, { contentType: ["Invalid content type"] }); | ||
} | ||
} | ||
|
||
module.exports = { | ||
checkProposalContent, | ||
}; |
4 changes: 2 additions & 2 deletions
4
backend/packages/backend/src/features/proposals/commonRoutes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
const Router = require("koa-router"); | ||
const proposalController = require("./proposal.controller"); | ||
const { getProposalById } = require("./getProposalById"); | ||
|
||
const router = new Router(); | ||
router.get("/proposal/:proposalId", proposalController.getProposalById); | ||
router.get("/proposal/:proposalId", getProposalById); | ||
|
||
module.exports = router; |
150 changes: 150 additions & 0 deletions
150
backend/packages/backend/src/features/proposals/createProposal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
const { HttpError } = require("../../exc"); | ||
const proposalService = require("../../services/proposal.service"); | ||
const { ChoiceType } = require("../../constants"); | ||
const isEmpty = require("lodash.isempty"); | ||
const { spaces: spaceServices } = require("../../spaces"); | ||
const { Accessibility } = require("../../consts/space"); | ||
const { checkProposalContent } = require("./checkProposalContent"); | ||
|
||
function checkProposalChoices(data) { | ||
const { choiceType, choices } = data; | ||
|
||
if (!choiceType) { | ||
throw new HttpError(400, { content: ["Choice type is missing"] }); | ||
} | ||
|
||
if (![ChoiceType.Single, ChoiceType.Multiple].includes(choiceType)) { | ||
throw new HttpError(400, { choiceType: ["Unknown choice type"] }); | ||
} | ||
|
||
if (!choices) { | ||
throw new HttpError(400, { choices: ["Choices is missing"] }); | ||
} | ||
|
||
if ( | ||
!Array.isArray(choices) || | ||
choices.length < 2 || | ||
choices.some((item) => typeof item !== "string") | ||
) { | ||
throw new HttpError(400, { | ||
choices: ["Choices must be array of string with at least 2 items"], | ||
}); | ||
} | ||
|
||
if (new Set(choices).size < choices.length) { | ||
throw new HttpError(400, { choices: ["All choices should be different"] }); | ||
} | ||
} | ||
|
||
function checkProposalDate(data) { | ||
const { startDate, endDate } = data; | ||
|
||
if (!startDate) { | ||
throw new HttpError(400, { content: ["Start date is missing"] }); | ||
} | ||
|
||
if (!endDate) { | ||
throw new HttpError(400, { content: ["End date is missing"] }); | ||
} | ||
} | ||
|
||
function checkProposalOptions(data) { | ||
const { networksConfig, snapshotHeights, proposerNetwork } = data; | ||
|
||
if (!snapshotHeights) { | ||
throw new HttpError(400, { content: ["Snapshot height is missing"] }); | ||
} | ||
|
||
if (isEmpty(networksConfig)) { | ||
throw new HttpError(400, { | ||
networksConfig: ["Networks config is missing"], | ||
}); | ||
} | ||
|
||
if (!proposerNetwork) { | ||
throw new HttpError(400, { | ||
proposerNetwork: ["Proposer network is missing"], | ||
}); | ||
} | ||
|
||
checkProposalContent(data); | ||
|
||
checkProposalDate(data); | ||
|
||
checkProposalChoices(data); | ||
} | ||
|
||
async function createProposal(ctx) { | ||
const { data, address, signature } = ctx.request.body; | ||
const { | ||
space, | ||
networksConfig, | ||
title, | ||
content, | ||
contentType, | ||
choiceType, | ||
choices, | ||
startDate, | ||
endDate, | ||
snapshotHeights, | ||
realProposer, | ||
proposerNetwork, | ||
banner, | ||
} = data; | ||
|
||
if (!space) { | ||
throw new HttpError(400, { space: ["Space is missing"] }); | ||
} | ||
|
||
const spaceConfig = spaceServices[space]; | ||
if (!spaceConfig) { | ||
throw new HttpError(400, { space: ["Unknown space"] }); | ||
} | ||
|
||
checkProposalOptions(data); | ||
|
||
if (spaceConfig.accessibility === Accessibility.SOCIETY) { | ||
ctx.body = await proposalService.createSocietyProposal({ | ||
space, | ||
networksConfig, | ||
title, | ||
content, | ||
contentType, | ||
choiceType, | ||
choices, | ||
startDate, | ||
endDate, | ||
snapshotHeights, | ||
realProposer, | ||
proposerNetwork, | ||
banner, | ||
data, | ||
address, | ||
signature, | ||
}); | ||
return; | ||
} | ||
|
||
ctx.body = await proposalService.createProposal({ | ||
space, | ||
networksConfig, | ||
title, | ||
content, | ||
contentType, | ||
choiceType, | ||
choices, | ||
startDate, | ||
endDate, | ||
snapshotHeights, | ||
realProposer, | ||
proposerNetwork, | ||
banner, | ||
data, | ||
address, | ||
signature, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
createProposal, | ||
}; |
11 changes: 11 additions & 0 deletions
11
backend/packages/backend/src/features/proposals/getAddressVote.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const proposalService = require("../../services/proposal.service"); | ||
|
||
async function getAddressVote(ctx) { | ||
const { proposalCid, address } = ctx.params; | ||
|
||
ctx.body = await proposalService.getAddressVote(proposalCid, address); | ||
} | ||
|
||
module.exports = { | ||
getAddressVote, | ||
}; |
17 changes: 17 additions & 0 deletions
17
backend/packages/backend/src/features/proposals/getComments.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const proposalService = require("../../services/proposal.service"); | ||
const { extractPage } = require("../../utils"); | ||
|
||
async function getComments(ctx) { | ||
const { page, pageSize } = extractPage(ctx); | ||
if (pageSize === 0 || page < 1) { | ||
ctx.status = 400; | ||
return; | ||
} | ||
|
||
const { proposalCid } = ctx.params; | ||
ctx.body = await proposalService.getComments(proposalCid, page, pageSize); | ||
} | ||
|
||
module.exports = { | ||
getComments, | ||
}; |
11 changes: 11 additions & 0 deletions
11
backend/packages/backend/src/features/proposals/getProposalById.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const proposalService = require("../../services/proposal.service"); | ||
|
||
async function getProposalById(ctx) { | ||
const { proposalId } = ctx.params; | ||
|
||
ctx.body = await proposalService.getProposalById(proposalId); | ||
} | ||
|
||
module.exports = { | ||
getProposalById, | ||
}; |
91 changes: 91 additions & 0 deletions
91
backend/packages/backend/src/features/proposals/getProposals.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const { | ||
queryProposals, | ||
} = require("../../services/proposal.service/proposalQuery"); | ||
const { extractPage } = require("../../utils"); | ||
|
||
async function getProposals(ctx) { | ||
const { space } = ctx.params; | ||
|
||
const { page, pageSize } = extractPage(ctx); | ||
if (pageSize === 0 || page < 1) { | ||
ctx.status = 400; | ||
return; | ||
} | ||
|
||
const q = { space }; | ||
ctx.body = await queryProposals( | ||
q, | ||
{ terminatedOrEndedAt: -1 }, | ||
page, | ||
pageSize, | ||
); | ||
} | ||
|
||
async function getPendingProposals(ctx) { | ||
const { space } = ctx.params; | ||
|
||
const { page, pageSize } = extractPage(ctx); | ||
if (pageSize === 0 || page < 1) { | ||
ctx.status = 400; | ||
return; | ||
} | ||
|
||
const now = Date.now(); | ||
const q = { | ||
space, | ||
startDate: { $gt: now }, | ||
terminated: null, | ||
}; | ||
|
||
ctx.body = await queryProposals(q, { startDate: 1 }, page, pageSize); | ||
} | ||
|
||
async function getActiveProposals(ctx) { | ||
const { space } = ctx.params; | ||
|
||
const { page, pageSize } = extractPage(ctx); | ||
if (pageSize === 0 || page < 1) { | ||
ctx.status = 400; | ||
return; | ||
} | ||
|
||
const now = Date.now(); | ||
const q = { | ||
space, | ||
startDate: { $lte: now }, | ||
endDate: { $gt: now }, | ||
terminated: null, | ||
}; | ||
|
||
ctx.body = await queryProposals(q, { endDate: 1 }, page, pageSize); | ||
} | ||
|
||
async function getClosedProposals(ctx) { | ||
const { space } = ctx.params; | ||
|
||
const { page, pageSize } = extractPage(ctx); | ||
if (pageSize === 0 || page < 1) { | ||
ctx.status = 400; | ||
return; | ||
} | ||
|
||
const now = Date.now(); | ||
const q = { | ||
space, | ||
$or: [{ endDate: { $lte: now } }, { terminated: { $ne: null } }], | ||
}; | ||
|
||
ctx.body = await queryProposals( | ||
q, | ||
{ terminatedOrEndedAt: -1 }, | ||
page, | ||
pageSize, | ||
); | ||
} | ||
|
||
module.exports = { | ||
getProposals, | ||
getPendingProposals, | ||
getActiveProposals, | ||
getClosedProposals, | ||
}; |
10 changes: 10 additions & 0 deletions
10
backend/packages/backend/src/features/proposals/getStats.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const proposalService = require("../../services/proposal.service"); | ||
|
||
async function getStats(ctx) { | ||
const { proposalCid } = ctx.params; | ||
ctx.body = await proposalService.getStats(proposalCid); | ||
} | ||
|
||
module.exports = { | ||
getStats, | ||
}; |
14 changes: 14 additions & 0 deletions
14
backend/packages/backend/src/features/proposals/getVoteByNetworkAddress.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const proposalService = require("../../services/proposal.service"); | ||
|
||
async function getVoteByNetworkAddress(ctx) { | ||
const { proposalCid, network, address } = ctx.params; | ||
ctx.body = await proposalService.getAddressVote( | ||
proposalCid, | ||
address, | ||
network, | ||
); | ||
} | ||
|
||
module.exports = { | ||
getVoteByNetworkAddress, | ||
}; |
Oops, something went wrong.