From 51913991dc49777659898038dad7018c10e02e07 Mon Sep 17 00:00:00 2001 From: jankun4 Date: Tue, 26 Mar 2024 18:23:45 +0100 Subject: [PATCH] [#566] add optional 'search' query param Allow for searching for specific GA by title, motivation, about and rationale Signed-off-by: jankun4 --- CHANGELOG.md | 1 + govtool/backend/sql/list-proposals.sql | 2 ++ govtool/backend/src/VVA/API.hs | 25 +++++++++++++++++++++++-- govtool/backend/src/VVA/API/Types.hs | 23 +++++++++++++++++++++++ govtool/backend/src/VVA/Proposal.hs | 2 ++ govtool/backend/src/VVA/Types.hs | 1 + 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a4ce807a..aeab28c58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ changes. ### Fixed +- proposal/list now takes optional `search` query param [Issue 566](https://github.com/IntersectMBO/govtool/issues/566) - Fix possible sql error when there would be no predefined drep voting pwoer [Issue 501](https://github.com/IntersectMBO/govtool/issues/501) - Fix drep type detection when changing metadata [Issue 333](https://github.com/IntersectMBO/govtool/issues/333) - Fix make button disble when wallet tries connect [Issue 265](https://github.com/IntersectMBO/govtool/issues/265) diff --git a/govtool/backend/sql/list-proposals.sql b/govtool/backend/sql/list-proposals.sql index 94cd4849e..f0bc3cc13 100644 --- a/govtool/backend/sql/list-proposals.sql +++ b/govtool/backend/sql/list-proposals.sql @@ -59,6 +59,7 @@ SELECT off_chain_vote_data.abstract, off_chain_vote_data.motivation, off_chain_vote_data.rationale, + off_chain_vote_data.json, coalesce(Sum(ldd.amount) FILTER (WHERE voting_procedure.vote::text = 'Yes'), 0) +( CASE WHEN gov_action_proposal.type = 'NoConfidence' THEN always_no_confidence_voting_power.amount @@ -109,6 +110,7 @@ GROUP BY off_chain_vote_data.abstract, off_chain_vote_data.motivation, off_chain_vote_data.rationale, + off_chain_vote_data.json, gov_action_proposal.index, creator_tx.hash, creator_block.time, diff --git a/govtool/backend/src/VVA/API.hs b/govtool/backend/src/VVA/API.hs index aba121ba6..0bbbc7e42 100644 --- a/govtool/backend/src/VVA/API.hs +++ b/govtool/backend/src/VVA/API.hs @@ -47,6 +47,7 @@ type VVAApi = :> QueryParam "page" Natural :> QueryParam "pageSize" Natural :> QueryParam "drepId" HexText + :> QueryParam "search" Text :> Get '[JSON] ListProposalsResponse :<|> "proposal" :> "get" :> Capture "proposalId" GovActionId :> QueryParam "drepId" HexText :> Get '[JSON] GetProposalResponse :<|> "epoch" :> "params" :> Get '[JSON] GetCurrentEpochParamsResponse @@ -127,6 +128,7 @@ proposalToResponse Types.Proposal {..} = proposalResponseAbout = proposalAbout, proposalResponseMotivation = proposalMotivaiton, proposalResponseRationale = proposalRationale, + proposalResponseMetadata = GovernanceActionMetadata <$> proposalMetadata, proposalResponseYesVotes = proposalYesVotes, proposalResponseNoVotes = proposalNoVotes, proposalResponseAbstainVotes = proposalAbstainVotes @@ -217,8 +219,9 @@ listProposals -> Maybe Natural -> Maybe Natural -> Maybe HexText + -> Maybe Text -> m ListProposalsResponse -listProposals selectedTypes sortMode mPage mPageSize mDrepRaw = do +listProposals selectedTypes sortMode mPage mPageSize mDrepRaw mSearchQuery = do let page = (fromIntegral $ fromMaybe 0 mPage) :: Int pageSize = (fromIntegral $ fromMaybe 10 mPageSize) :: Int @@ -229,11 +232,29 @@ listProposals selectedTypes sortMode mPage mPageSize mDrepRaw = do map (voteParamsProposalId . voteResponseVote) <$> getVotes drepId [] Nothing + + + let filterF ProposalResponse{..} = case mSearchQuery of + Nothing -> True + Just searchQuery -> fromMaybe False $ do + title <- proposalResponseTitle + about <- proposalResponseAbout + motivation <- proposalResponseMotivation + rationale <- proposalResponseRationale + + let result = searchQuery `isInfixOf` title + || searchQuery `isInfixOf` about + || searchQuery `isInfixOf` motivation + || searchQuery `isInfixOf` rationale + + pure result + CacheEnv {proposalListCache} <- asks vvaCache mappedAndSortedProposals <- filter - ( \ProposalResponse {proposalResponseId} -> + ( \p@ProposalResponse {proposalResponseId} -> proposalResponseId `notElem` proposalsToRemove + && filterF p ) <$> mapSortAndFilterProposals selectedTypes sortMode diff --git a/govtool/backend/src/VVA/API/Types.hs b/govtool/backend/src/VVA/API/Types.hs index 5080cd339..cff955349 100644 --- a/govtool/backend/src/VVA/API/Types.hs +++ b/govtool/backend/src/VVA/API/Types.hs @@ -233,6 +233,27 @@ instance ToSchema GovernanceActionDetails where ?~ toJSON ("{\"some_key\": \"some value\", \"some_key2\": [1,2,3]}" :: Text) + +newtype GovernanceActionMetadata = GovernanceActionMetadata Value + deriving newtype (Show) + +instance FromJSON GovernanceActionMetadata where + parseJSON v@(Aeson.Object o) = pure (GovernanceActionMetadata v) + parseJSON _ = fail "GovernanceActionMetadata has to be an object" + +instance ToJSON GovernanceActionMetadata where + toJSON (GovernanceActionMetadata g) = g + +instance ToSchema GovernanceActionMetadata where + declareNamedSchema _ = pure $ NamedSchema (Just "GovernanceActionMetadata") $ mempty + & type_ ?~ OpenApiObject + & description ?~ "A Governance Action metadata" + & example + ?~ toJSON + ("{\"some_key\": \"some value\", \"some_key2\": [1,2,3]}" :: Text) + + + data ProposalResponse = ProposalResponse { proposalResponseId :: Text, proposalResponseTxHash :: HexText, @@ -249,6 +270,7 @@ data ProposalResponse = ProposalResponse proposalResponseAbout :: Maybe Text, proposalResponseMotivation :: Maybe Text, proposalResponseRationale :: Maybe Text, + proposalResponseMetadata :: Maybe GovernanceActionMetadata, proposalResponseYesVotes :: Integer, proposalResponseNoVotes :: Integer, proposalResponseAbstainVotes :: Integer @@ -273,6 +295,7 @@ exampleProposalResponse = "{ \"id\": \"proposalId123\"," <> "\"about\": \"Proposal About\"," <> "\"motivation\": \"Proposal Motivation\"," <> "\"rationale\": \"Proposal Rationale\"," + <> "\"metadata\": {\"key\": \"value\"}," <> "\"yesVotes\": 0," <> "\"noVotes\": 0," <> "\"abstainVotes\": 0}" diff --git a/govtool/backend/src/VVA/Proposal.hs b/govtool/backend/src/VVA/Proposal.hs index 45980af7d..0b221ce2b 100644 --- a/govtool/backend/src/VVA/Proposal.hs +++ b/govtool/backend/src/VVA/Proposal.hs @@ -85,6 +85,7 @@ getProposals mProposalIds = withPool $ \conn -> do , about' , motivation' , rationale' + , metadataJson' , yesVotes' , noVotes' , abstainVotes' @@ -108,6 +109,7 @@ getProposals mProposalIds = withPool $ \conn -> do about' motivation' rationale' + metadataJson' (floor @Scientific yesVotes') (floor @Scientific noVotes') (floor @Scientific abstainVotes') diff --git a/govtool/backend/src/VVA/Types.hs b/govtool/backend/src/VVA/Types.hs index 499bac20a..4b552a0a0 100644 --- a/govtool/backend/src/VVA/Types.hs +++ b/govtool/backend/src/VVA/Types.hs @@ -105,6 +105,7 @@ data Proposal = Proposal proposalAbout :: Maybe Text, proposalMotivaiton :: Maybe Text, proposalRationale :: Maybe Text, + proposalMetadata :: Maybe Value, proposalYesVotes :: Integer, proposalNoVotes :: Integer, proposalAbstainVotes :: Integer