• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divviup / divviup-api / 12741016175

13 Jan 2025 05:08AM UTC coverage: 55.774% (-0.2%) from 55.925%
12741016175

Pull #1493

github

web-flow
Merge 66aa8f0f2 into 500555ff5
Pull Request #1493: Bump alpine from 3.21.0 to 3.21.2

3878 of 6953 relevant lines covered (55.77%)

86.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.31
/src/entity/task/update_task.rs
1
use janus_messages::Time as JanusTime;
2
use sea_orm::{ActiveModelTrait, ActiveValue, IntoActiveModel};
3
use serde::Deserialize;
4
use time::OffsetDateTime;
5
use tokio::try_join;
6
use trillium_client::Client;
7
use validator::{Validate, ValidationError};
8

9
use crate::{deserialize_some, entity::Aggregator, handler::Error, Crypter, Db};
10

11
use super::assert_same;
12

13
#[derive(Default, Deserialize, Validate, Debug)]
×
14
pub struct UpdateTask {
15
    #[validate(custom(function = "validate_name"))]
16
    name: Option<String>,
17
    #[serde(default, deserialize_with = "deserialize_some")]
18
    expiration: Option<Expiration>,
19
}
20

21
#[derive(Debug, Deserialize, Clone, Copy)]
×
22
struct Expiration(#[serde(default, with = "time::serde::rfc3339::option")] Option<OffsetDateTime>);
23

24
fn validate_name(name: &str) -> Result<(), ValidationError> {
7✔
25
    if name.is_empty() {
7✔
26
        return Err(ValidationError::new("name-too-short"));
1✔
27
    }
6✔
28
    Ok(())
6✔
29
}
7✔
30

31
impl UpdateTask {
32
    pub async fn update_aggregator_expiration(
26✔
33
        &self,
26✔
34
        aggregator: Aggregator,
26✔
35
        task_id: &str,
26✔
36
        http_client: &Client,
26✔
37
        crypter: &Crypter,
26✔
38
    ) -> Result<(), Error> {
26✔
39
        let expiration = self.expiration.as_ref().unwrap().0.map(|expiration| {
26✔
40
            JanusTime::from_seconds_since_epoch(expiration.unix_timestamp().try_into().unwrap())
22✔
41
        });
26✔
42
        let response = aggregator
26✔
43
            .client(http_client.clone(), crypter)?
26✔
44
            .update_task_expiration(task_id, expiration)
26✔
45
            .await?;
26✔
46
        assert_same(expiration, response.task_expiration, "expiration")?;
24✔
47
        Ok(())
24✔
48
    }
26✔
49

50
    /// Validates the request. Updates task definitions in the aggregators, if necessary. Returns
51
    /// an [`ActiveModel`] for committing to the database.
52
    pub async fn update(
11✔
53
        self,
11✔
54
        http_client: &Client,
11✔
55
        db: &Db,
11✔
56
        crypter: &Crypter,
11✔
57
        model: super::Model,
11✔
58
    ) -> Result<super::ActiveModel, Error> {
11✔
59
        self.validate()?;
11✔
60
        let mut am = model.clone().into_active_model();
10✔
61
        if let Some(ref name) = self.name {
10✔
62
            am.name = ActiveValue::Set(name.clone());
6✔
63
        }
6✔
64
        if let Some(ref expiration) = self.expiration {
10✔
65
            try_join!(
5✔
66
                self.update_aggregator_expiration(
5✔
67
                    model.leader_aggregator(db).await?,
5✔
68
                    &model.id,
5✔
69
                    http_client,
5✔
70
                    crypter
5✔
71
                ),
5✔
72
                self.update_aggregator_expiration(
5✔
73
                    model.helper_aggregator(db).await?,
5✔
74
                    &model.id,
5✔
75
                    http_client,
5✔
76
                    crypter
5✔
77
                )
78
            )?;
×
79
            am.expiration = ActiveValue::set(expiration.0);
5✔
80
        }
5✔
81
        if am.is_changed() {
10✔
82
            am.updated_at = ActiveValue::Set(OffsetDateTime::now_utc());
10✔
83
        }
10✔
84
        Ok(am)
10✔
85
    }
11✔
86

87
    pub fn expiration(expiration: Option<OffsetDateTime>) -> Self {
8✔
88
        Self {
8✔
89
            expiration: Some(Expiration(expiration)),
8✔
90
            ..Default::default()
8✔
91
        }
8✔
92
    }
8✔
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc