Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Audit logs are kept by plan: a week on free, 90 days on the plan, and what staff set for an account in sudo | 1 | //! How long each workspace's audit log is kept. |
| 2 | //! | |
| 3 | //! A free workspace keeps `FREE_AUDIT_RETENTION_DAYS` (7); the g1t plan, | |
| 4 | //! g1t's own workspaces and an enterprise's keep `AUDIT_RETENTION_DAYS` | |
| 5 | //! (90). Staff can set an account's own number in sudo, such as for an | |
| 6 | //! organization that pays for longer, up to `AUDIT_MAX_DAYS` (400). What | |
| 7 | //! staff set wins over the plan's either way. The events service asks for | |
| 8 | //! these once a day (`audit_retention`) and deletes what is older. | |
| 9 | ||
| 10 | use futures_util::future::try_join_all; | |
| 11 | use g1t_contracts::billing::{AuditRetention, AuditRetentionArgs, PlanKind}; | |
| 12 | use worker::Result; | |
| 13 | ||
| 14 | use crate::Billing; | |
| 15 | use crate::credits::Config; | |
| 16 | ||
| 17 | /// Days of audit log a workspace keeps: what staff set for its account, | |
| 18 | /// or else its plan's. | |
| 19 | pub(crate) fn effective_days(plans: &Config, plan: PlanKind, custom: Option<u32>) -> u32 { | |
| 20 | custom.unwrap_or(match plan { | |
| 21 | PlanKind::Free => plans.free_audit_days, | |
| 22 | PlanKind::Paid | PlanKind::Internal | PlanKind::Enterprise => plans.audit_days, | |
| 23 | }) | |
| 24 | } | |
| 25 | ||
| 26 | /// Why a number staff typed cannot be an account's retention, or None | |
| 27 | /// when it can. | |
| 28 | pub(crate) fn invalid_days(plans: &Config, days: Option<u32>) -> Option<String> { | |
| 29 | days.filter(|d| !(1..=plans.audit_max_days).contains(d)) | |
| 30 | .map(|_| format!("Audit log days is between 1 and {}, or empty for the plan's.", plans.audit_max_days)) | |
| 31 | } | |
| 32 | ||
| 33 | impl Billing { | |
| 34 | /// `audit_retention`: each workspace's days, its account and plan read | |
| 35 | /// once and all of them at the same time. | |
| 36 | pub(crate) async fn audit_retention(&self, a: AuditRetentionArgs) -> Result<Vec<AuditRetention>> { | |
| 37 | try_join_all(a.workspaces.iter().map(|workspace| async move { | |
| 38 | let workspace = workspace.to_lowercase(); | |
| 39 | let account = self.account_of(&workspace).await?; | |
| 40 | let plan = self.plan_kind_for(&workspace, &account).await?; | |
| 41 | let days = effective_days(&self.plans, plan, account.allowances.audit_retention_days); | |
| 42 | Ok::<_, worker::Error>(AuditRetention { workspace, days }) | |
| 43 | })) | |
| 44 | .await | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | #[cfg(test)] | |
| 49 | mod tests { | |
| 50 | use super::*; | |
| 51 | ||
| 52 | #[test] | |
| 53 | fn free_keeps_a_week_and_the_plan_ninety_days() { | |
| 54 | let plans = Config::default(); | |
| 55 | assert_eq!(effective_days(&plans, PlanKind::Free, None), 7); | |
| 56 | assert_eq!(effective_days(&plans, PlanKind::Paid, None), 90); | |
| 57 | assert_eq!(effective_days(&plans, PlanKind::Internal, None), 90); | |
| 58 | assert_eq!(effective_days(&plans, PlanKind::Enterprise, None), 90); | |
| 59 | } | |
| 60 | ||
| 61 | #[test] | |
| 62 | fn what_staff_set_wins_either_way() { | |
| 63 | let plans = Config::default(); | |
| 64 | assert_eq!(effective_days(&plans, PlanKind::Free, Some(30)), 30); | |
| 65 | assert_eq!(effective_days(&plans, PlanKind::Paid, Some(365)), 365); | |
| 66 | assert_eq!(effective_days(&plans, PlanKind::Enterprise, Some(14)), 14); | |
| 67 | } | |
| 68 | ||
| 69 | #[test] | |
| 70 | fn staff_set_between_one_day_and_the_most() { | |
| 71 | let plans = Config::default(); | |
| 72 | assert_eq!(invalid_days(&plans, None), None); | |
| 73 | assert_eq!(invalid_days(&plans, Some(1)), None); | |
| 74 | assert_eq!(invalid_days(&plans, Some(400)), None); | |
| 75 | assert_eq!( | |
| 76 | invalid_days(&plans, Some(0)).as_deref(), | |
| 77 | Some("Audit log days is between 1 and 400, or empty for the plan's.") | |
| 78 | ); | |
| 79 | assert!(invalid_days(&plans, Some(401)).is_some()); | |
| 80 | } | |
| 81 | } |