1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::Result;
use fake::faker::name::en::Name;
use fake::Fake;
use rand::rngs::OsRng;
use rand::RngCore;
use vit_servicing_station_lib::db::models::community_advisors_reviews::ReviewRanking;
use vit_servicing_station_tests::common::data::ProposalTemplate;
use vit_servicing_station_tests::common::data::ReviewTemplate;

pub struct ReviewGenerator {
    generator: OsRng,
    current_id: u32,
    assessors_per_proposal_count: u32,
}

impl ReviewGenerator {
    pub fn new(assessors_per_proposal_count: u32) -> ReviewGenerator {
        ReviewGenerator {
            generator: OsRng,
            current_id: 1,
            assessors_per_proposal_count,
        }
    }

    pub fn generate(&mut self, proposals: Vec<ProposalTemplate>) -> Result<Vec<ReviewTemplate>> {
        let mut reviews = Vec::new();
        self.current_id = 1;
        for proposal in proposals.iter() {
            for _ in 0..self.assessors_per_proposal_count {
                let assessor = self.generate_assessor_name();
                if let Some(ref challenge_id) = &proposal.challenge_id {
                    if challenge_id.parse::<u32>().unwrap() == 1 {
                        reviews.push(self.generate_community_reviews(proposal, assessor));
                    } else {
                        reviews.push(self.generate_standard_reviews(proposal, assessor));
                    }
                    self.increment_id();
                } else {
                    return Err(crate::error::Error::NoChallengeIdFound {
                        proposal_id: proposal.proposal_id.clone(),
                    });
                }
            }
        }
        Ok(reviews)
    }

    fn increment_id(&mut self) {
        self.current_id += 1;
    }

    fn generate_assessor_name(&mut self) -> String {
        format!(
            "{}_{}",
            Name().fake::<String>().to_lowercase().replace("' '", "_"),
            (self.generator.next_u32() % 100)
        )
    }

    fn generate_community_reviews<S: Into<String>>(
        &mut self,
        proposal: &ProposalTemplate,
        assessor: S,
    ) -> ReviewTemplate {
        let assessor = assessor.into();
        self.generate_review(proposal, &assessor)
    }

    fn generate_standard_reviews<S: Into<String>>(
        &mut self,
        proposal: &ProposalTemplate,
        assessor: S,
    ) -> ReviewTemplate {
        let assessor = assessor.into();
        self.generate_review(proposal, &assessor)
    }

    fn generate_review<S: Into<String>>(
        &mut self,
        proposal: &ProposalTemplate,
        assessor: S,
    ) -> ReviewTemplate {
        ReviewTemplate {
            id: Some(self.current_id.to_string()),
            proposal_id: proposal.proposal_id.parse().unwrap(),
            impact_alignment_rating_given: (self.generator.next_u32() % 5) as i32,
            impact_alignment_note: fake::faker::lorem::en::Sentence(0..10).fake::<String>(),
            assessor: assessor.into(),
            auditability_note: fake::faker::lorem::en::Sentence(0..10).fake::<String>(),
            auditability_rating_given: (self.generator.next_u32() % 5) as i32,
            feasibility_note: fake::faker::lorem::en::Sentence(0..10).fake::<String>(),
            feasibility_rating_given: (self.generator.next_u32() % 5) as i32,
            ranking: match self.generator.next_u32() % 2 {
                0 => ReviewRanking::Excellent,
                _ => ReviewRanking::Good,
            },
        }
    }
}