Skip to content

Commit

Permalink
[#566] add optional 'search' query param
Browse files Browse the repository at this point in the history
Allow for searching for specific GA by title, motivation, about and rationale

Signed-off-by: jankun4 <michaljankun@gmail.com>
  • Loading branch information
jankun4 committed Mar 26, 2024
1 parent aa63ed7 commit 5191399
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions govtool/backend/sql/list-proposals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 23 additions & 2 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,6 +128,7 @@ proposalToResponse Types.Proposal {..} =
proposalResponseAbout = proposalAbout,
proposalResponseMotivation = proposalMotivaiton,
proposalResponseRationale = proposalRationale,
proposalResponseMetadata = GovernanceActionMetadata <$> proposalMetadata,
proposalResponseYesVotes = proposalYesVotes,
proposalResponseNoVotes = proposalNoVotes,
proposalResponseAbstainVotes = proposalAbstainVotes
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions govtool/backend/src/VVA/Proposal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ getProposals mProposalIds = withPool $ \conn -> do
, about'
, motivation'
, rationale'
, metadataJson'
, yesVotes'
, noVotes'
, abstainVotes'
Expand All @@ -108,6 +109,7 @@ getProposals mProposalIds = withPool $ \conn -> do
about'
motivation'
rationale'
metadataJson'
(floor @Scientific yesVotes')
(floor @Scientific noVotes')
(floor @Scientific abstainVotes')
Expand Down
1 change: 1 addition & 0 deletions govtool/backend/src/VVA/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5191399

Please sign in to comment.