Skip to content

Commit

Permalink
feat: Update ObjectiveDetails type, add groups field | NPG-6555 (#411)
Browse files Browse the repository at this point in the history
# Description

Added a new `groups` field to the `ObjectiveDetails` type, updated db
queries.
  • Loading branch information
Mr-Leshiy committed May 29, 2023
1 parent 19cd452 commit 6241e37
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 46 deletions.
9 changes: 5 additions & 4 deletions Makefiles/db.toml
Expand Up @@ -48,10 +48,11 @@ psql -U catalyst-event-dev -d CatalystEventDev -f test_data/03_voter_table.sql $
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/04_contribution_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/05_goal_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/06_objective_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/07_proposal_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/08_proposal_review_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/09_review_rating_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/10_objective_review_metric_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/07_voteplan_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/08_proposal_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/09_proposal_review_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/10_review_rating_table.sql ${@}
psql -U catalyst-event-dev -d CatalystEventDev -f test_data/11_objective_review_metric_table.sql ${@}
'''

# Install historic data for past events
Expand Down
7 changes: 6 additions & 1 deletion book/src/07_web_api/openapi/core_backend_api.yaml
Expand Up @@ -869,6 +869,11 @@ components:
description: Individual Objective Details
type: object
properties:
groups:
type: array
description: The valid voter groups for this voting event.
items:
$ref: '#/components/schemas/VoterGroup'
reward:
$ref: '#/components/schemas/RewardDefinition'
description: |-
Expand Down Expand Up @@ -1270,7 +1275,7 @@ components:
VoterGroup:
type: object
properties:
id:
group:
$ref: '#/components/schemas/VoterGroupId'
voting_token:
type: string
Expand Down
24 changes: 23 additions & 1 deletion src/cat-data-service/src/service/v1/event/objective/mod.rs
Expand Up @@ -66,7 +66,7 @@ mod tests {
};
use event_db::types::event::objective::{
ObjectiveDetails, ObjectiveId, ObjectiveSummary, ObjectiveSupplementalData, ObjectiveType,
RewardDefintion,
RewardDefintion, VoterGroup,
};
use serde_json::json;
use tower::ServiceExt;
Expand Down Expand Up @@ -97,6 +97,16 @@ mod tests {
description: "description 1".to_string(),
},
details: ObjectiveDetails {
groups: vec![
VoterGroup {
group: Some("direct".to_string()),
voting_token: Some("voting token 1".to_string()),
},
VoterGroup {
group: Some("rep".to_string()),
voting_token: Some("voting token 2".to_string()),
}
],
reward: Some(RewardDefintion {
currency: "ADA".to_string(),
value: 100
Expand All @@ -121,6 +131,7 @@ mod tests {
description: "description 2".to_string(),
},
details: ObjectiveDetails {
groups: Vec::new(),
reward: None,
supplemental: None,
}
Expand Down Expand Up @@ -149,6 +160,16 @@ mod tests {
description: "description 1".to_string(),
},
details: ObjectiveDetails {
groups: vec![
VoterGroup {
group: Some("direct".to_string()),
voting_token: Some("voting token 1".to_string()),
},
VoterGroup {
group: Some("rep".to_string()),
voting_token: Some("voting token 2".to_string()),
}
],
reward: Some(RewardDefintion {
currency: "ADA".to_string(),
value: 100
Expand Down Expand Up @@ -185,6 +206,7 @@ mod tests {
description: "description 2".to_string(),
},
details: ObjectiveDetails {
groups: Vec::new(),
reward: None,
supplemental: None,
}
Expand Down
48 changes: 46 additions & 2 deletions src/event-db/src/queries/event/objective.rs
Expand Up @@ -3,7 +3,7 @@ use crate::{
types::event::{
objective::{
Objective, ObjectiveDetails, ObjectiveId, ObjectiveSummary, ObjectiveSupplementalData,
ObjectiveType, RewardDefintion,
ObjectiveType, RewardDefintion, VoterGroup,
},
EventId,
},
Expand All @@ -23,12 +23,17 @@ pub trait ObjectiveQueries: Sync + Send + 'static {

impl EventDB {
const OBJECTIVES_QUERY: &'static str =
"SELECT objective.id, objective.title, objective.description, objective.rewards_currency, objective.rewards_total, objective.extra,
"SELECT objective.row_id, objective.id, objective.title, objective.description, objective.rewards_currency, objective.rewards_total, objective.extra,
objective_category.name, objective_category.description as objective_category_description
FROM objective
INNER JOIN objective_category on objective.category = objective_category.name
WHERE objective.event = $1
LIMIT $2 OFFSET $3;";

const VOTING_GROPUS_QEURY: &'static str =
"SELECT voteplan.group_id as group, voteplan.token_id as voting_token
FROM voteplan
WHERE objective_id = $1;";
}

#[async_trait]
Expand All @@ -50,6 +55,7 @@ impl ObjectiveQueries for EventDB {

let mut objectives = Vec::new();
for row in rows {
let row_id: i32 = row.try_get("row_id")?;
let summary = ObjectiveSummary {
id: ObjectiveId(row.try_get("id")?),
objective_type: ObjectiveType {
Expand All @@ -65,7 +71,23 @@ impl ObjectiveQueries for EventDB {
(Some(currency), Some(value)) => Some(RewardDefintion { currency, value }),
_ => None,
};

let mut groups = Vec::new();
let rows = conn.query(Self::VOTING_GROPUS_QEURY, &[&row_id]).await?;
for row in rows {
let group: Option<_> = row.try_get("group")?;
let voting_token: Option<_> = row.try_get("voting_token")?;
match (group, voting_token) {
(None, None) => {}
(group, voting_token) => groups.push(VoterGroup {
group,
voting_token,
}),
}
}

let details = ObjectiveDetails {
groups,
reward,
supplemental: row
.try_get::<_, Option<serde_json::Value>>("extra")?
Expand Down Expand Up @@ -122,6 +144,16 @@ mod tests {
description: "description 1".to_string(),
},
details: ObjectiveDetails {
groups: vec![
VoterGroup {
group: Some("direct".to_string()),
voting_token: Some("voting token 1".to_string()),
},
VoterGroup {
group: Some("rep".to_string()),
voting_token: Some("voting token 2".to_string()),
}
],
reward: Some(RewardDefintion {
currency: "ADA".to_string(),
value: 100
Expand All @@ -146,6 +178,7 @@ mod tests {
description: "description 2".to_string(),
},
details: ObjectiveDetails {
groups: Vec::new(),
reward: None,
supplemental: None,
}
Expand All @@ -170,6 +203,16 @@ mod tests {
description: "description 1".to_string(),
},
details: ObjectiveDetails {
groups: vec![
VoterGroup {
group: Some("direct".to_string()),
voting_token: Some("voting token 1".to_string()),
},
VoterGroup {
group: Some("rep".to_string()),
voting_token: Some("voting token 2".to_string()),
}
],
reward: Some(RewardDefintion {
currency: "ADA".to_string(),
value: 100
Expand Down Expand Up @@ -202,6 +245,7 @@ mod tests {
description: "description 2".to_string(),
},
details: ObjectiveDetails {
groups: Vec::new(),
reward: None,
supplemental: None,
}
Expand Down
25 changes: 0 additions & 25 deletions src/event-db/src/types/event/mod.rs
Expand Up @@ -122,12 +122,6 @@ pub struct EventSchedule {
pub tallying_end: Option<DateTime<Utc>>,
}

#[derive(Debug, Serialize, Clone, PartialEq, Eq, Deserialize)]
pub struct VoterGroup {
pub id: String,
pub voting_token: String,
}

#[derive(Debug, Serialize, Clone, PartialEq, Eq)]
pub struct EventDetails {
pub voting_power: VotingPowerSettings,
Expand Down Expand Up @@ -416,25 +410,6 @@ mod tests {
);
}

#[test]
fn voter_group_json_test() {
let voter_group = VoterGroup {
id: "rep".to_string(),
voting_token: "voting token 1".to_string(),
};

let json = serde_json::to_value(&voter_group).unwrap();
assert_eq!(
json,
json!(
{
"id": "rep",
"voting_token": "voting token 1",
}
)
);
}

#[test]
fn event_details_json_test() {
let event_details = EventDetails {
Expand Down
41 changes: 28 additions & 13 deletions src/event-db/src/types/event/objective.rs
Expand Up @@ -25,16 +25,19 @@ pub struct RewardDefintion {
pub value: i64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct GroupBallotType {
pub group: String,
pub ballot: String,
#[derive(Debug, Serialize, Clone, PartialEq, Eq, Deserialize)]
pub struct VoterGroup {
#[serde(skip_serializing_if = "Option::is_none")]
pub group: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub voting_token: Option<String>,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ObjectiveSupplementalData(pub Value);
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ObjectiveDetails {
pub groups: Vec<VoterGroup>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reward: Option<RewardDefintion>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -122,25 +125,31 @@ mod tests {
}

#[test]
fn group_ballot_type_json_test() {
let group_ballot_type = GroupBallotType {
group: "rep".to_string(),
ballot: "public".to_string(),
fn voter_group_json_test() {
let voter_group = VoterGroup {
group: Some("rep".to_string()),
voting_token: Some("voting token 1".to_string()),
};

let json = serde_json::to_value(&group_ballot_type).unwrap();
let json = serde_json::to_value(&voter_group).unwrap();
assert_eq!(
json,
json!({
"group": "rep",
"ballot": "public",
})
json!(
{
"group": "rep",
"voting_token": "voting token 1",
}
)
);
}

#[test]
fn objective_details_json_test() {
let objective_details = ObjectiveDetails {
groups: vec![VoterGroup {
group: Some("rep".to_string()),
voting_token: Some("voting token 1".to_string()),
}],
reward: Some(RewardDefintion {
currency: "ADA".to_string(),
value: 100,
Expand All @@ -159,6 +168,12 @@ mod tests {
json,
json!(
{
"groups": [
{
"group": "rep",
"voting_token": "voting token 1",
}
],
"reward": {
"currency": "ADA",
"value": 100,
Expand Down
7 changes: 7 additions & 0 deletions src/event-db/test_data/07_voteplan_table.sql
@@ -0,0 +1,7 @@
INSERT INTO voteplan
(row_id, objective_id, id, category, group_id, token_id)
VALUES
(1, 1, '1', 'public', 'direct', 'voting token 1'),
(2, 1, '2', 'public', 'rep', 'voting token 2'),
(3, 2, '2', 'public', NULL, NULL),
(4, 2, '2', 'public', NULL, NULL);

0 comments on commit 6241e37

Please sign in to comment.