Skip to content

Commit

Permalink
refactor, #1034
Browse files Browse the repository at this point in the history
  • Loading branch information
hyifeng committed Sep 3, 2024
1 parent f70002d commit 8a0334a
Show file tree
Hide file tree
Showing 51 changed files with 634 additions and 445 deletions.
8 changes: 8 additions & 0 deletions backend/packages/backend/src/consts/space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Accessibility = {
PUBLIC: "public",
SOCIETY: "society",
};

module.exports = {
Accessibility,
};
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,
};
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 backend/packages/backend/src/features/proposals/createProposal.js
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 backend/packages/backend/src/features/proposals/getAddressVote.js
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 backend/packages/backend/src/features/proposals/getComments.js
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 backend/packages/backend/src/features/proposals/getProposalById.js
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 backend/packages/backend/src/features/proposals/getProposals.js
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 backend/packages/backend/src/features/proposals/getStats.js
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,
};
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,
};
Loading

0 comments on commit 8a0334a

Please sign in to comment.