| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use sqlx::PgPool; |
| 23 |
|
| 24 |
use super::enums::AckKind; |
| 25 |
use super::id_types::{PendingAcknowledgementId, UserId}; |
| 26 |
use crate::error::Result; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#[derive(Debug, Clone, sqlx::FromRow)] |
| 32 |
pub struct AcknowledgementView { |
| 33 |
pub kind: AckKind, |
| 34 |
pub details: serde_json::Value, |
| 35 |
pub acknowledged: bool, |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
#[derive(Debug, Clone, sqlx::FromRow)] |
| 40 |
pub struct PendingAcknowledgement { |
| 41 |
pub id: PendingAcknowledgementId, |
| 42 |
pub user_id: UserId, |
| 43 |
pub kind: AckKind, |
| 44 |
pub dedup_key: String, |
| 45 |
pub details: serde_json::Value, |
| 46 |
pub notify_count: i32, |
| 47 |
pub email: String, |
| 48 |
pub display_name: Option<String>, |
| 49 |
} |
| 50 |
|
| 51 |
impl PendingAcknowledgement { |
| 52 |
|
| 53 |
|
| 54 |
pub fn token(&self, signing_secret: &str) -> String { |
| 55 |
crate::email::acknowledgement_token( |
| 56 |
self.user_id, |
| 57 |
&self.kind.to_string(), |
| 58 |
&self.dedup_key, |
| 59 |
signing_secret, |
| 60 |
) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#[tracing::instrument(skip_all)] |
| 75 |
pub async fn open( |
| 76 |
pool: &PgPool, |
| 77 |
user_id: UserId, |
| 78 |
kind: AckKind, |
| 79 |
dedup_key: &str, |
| 80 |
details: serde_json::Value, |
| 81 |
signing_secret: &str, |
| 82 |
) -> Result<bool> { |
| 83 |
let token_hash = crate::email::hash_opaque_token(&crate::email::acknowledgement_token( |
| 84 |
user_id, |
| 85 |
&kind.to_string(), |
| 86 |
dedup_key, |
| 87 |
signing_secret, |
| 88 |
)); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
let inserted = sqlx::query_scalar::<_, PendingAcknowledgementId>( |
| 94 |
"INSERT INTO pending_acknowledgements (user_id, kind, dedup_key, details, token_hash) \ |
| 95 |
VALUES ($1, $2, $3, $4, $5) \ |
| 96 |
ON CONFLICT (user_id, kind, dedup_key) WHERE acknowledged_at IS NULL \ |
| 97 |
DO NOTHING \ |
| 98 |
RETURNING id", |
| 99 |
) |
| 100 |
.bind(user_id) |
| 101 |
.bind(kind.to_string()) |
| 102 |
.bind(dedup_key) |
| 103 |
.bind(&details) |
| 104 |
.bind(&token_hash) |
| 105 |
.fetch_optional(pool) |
| 106 |
.await?; |
| 107 |
|
| 108 |
Ok(inserted.is_some()) |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#[tracing::instrument(skip_all)] |
| 118 |
pub async fn due( |
| 119 |
pool: &PgPool, |
| 120 |
repeat_days: i64, |
| 121 |
limit: i64, |
| 122 |
) -> Result<Vec<PendingAcknowledgement>> { |
| 123 |
let rows = sqlx::query_as::<_, PendingAcknowledgement>( |
| 124 |
"SELECT pa.id, pa.user_id, pa.kind, pa.dedup_key, pa.details, pa.notify_count, \ |
| 125 |
u.email, u.display_name \ |
| 126 |
FROM pending_acknowledgements pa \ |
| 127 |
JOIN users u ON u.id = pa.user_id \ |
| 128 |
WHERE pa.acknowledged_at IS NULL \ |
| 129 |
AND pa.escalated_at IS NULL \ |
| 130 |
AND u.suspended_at IS NULL \ |
| 131 |
AND (pa.last_notified_at IS NULL \ |
| 132 |
OR pa.last_notified_at < NOW() - make_interval(days => $1::int)) \ |
| 133 |
ORDER BY pa.last_notified_at ASC NULLS FIRST \ |
| 134 |
LIMIT $2", |
| 135 |
) |
| 136 |
.bind(i32::try_from(repeat_days).unwrap_or(7)) |
| 137 |
.bind(limit) |
| 138 |
.fetch_all(pool) |
| 139 |
.await?; |
| 140 |
Ok(rows) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[tracing::instrument(skip_all)] |
| 148 |
pub async fn mark_notified(pool: &PgPool, id: PendingAcknowledgementId) -> Result<()> { |
| 149 |
sqlx::query( |
| 150 |
"UPDATE pending_acknowledgements \ |
| 151 |
SET last_notified_at = NOW(), notify_count = notify_count + 1 \ |
| 152 |
WHERE id = $1", |
| 153 |
) |
| 154 |
.bind(id) |
| 155 |
.execute(pool) |
| 156 |
.await?; |
| 157 |
Ok(()) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#[tracing::instrument(skip_all)] |
| 165 |
pub async fn escalate(pool: &PgPool, id: PendingAcknowledgementId) -> Result<bool> { |
| 166 |
let moved = sqlx::query( |
| 167 |
"UPDATE pending_acknowledgements SET escalated_at = NOW() \ |
| 168 |
WHERE id = $1 AND escalated_at IS NULL AND acknowledged_at IS NULL", |
| 169 |
) |
| 170 |
.bind(id) |
| 171 |
.execute(pool) |
| 172 |
.await? |
| 173 |
.rows_affected() |
| 174 |
> 0; |
| 175 |
Ok(moved) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
#[tracing::instrument(skip_all)] |
| 184 |
pub async fn find_by_token(pool: &PgPool, token: &str) -> Result<Option<AcknowledgementView>> { |
| 185 |
let hash = crate::email::hash_opaque_token(token); |
| 186 |
let row = sqlx::query_as::<_, AcknowledgementView>( |
| 187 |
"SELECT pa.kind, pa.details, (pa.acknowledged_at IS NOT NULL) AS acknowledged \ |
| 188 |
FROM pending_acknowledgements pa \ |
| 189 |
WHERE pa.token_hash = $1", |
| 190 |
) |
| 191 |
.bind(&hash) |
| 192 |
.fetch_optional(pool) |
| 193 |
.await?; |
| 194 |
Ok(row) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
#[tracing::instrument(skip_all)] |
| 200 |
pub async fn acknowledge(pool: &PgPool, token: &str) -> Result<bool> { |
| 201 |
let hash = crate::email::hash_opaque_token(token); |
| 202 |
let moved = sqlx::query( |
| 203 |
"UPDATE pending_acknowledgements SET acknowledged_at = NOW() \ |
| 204 |
WHERE token_hash = $1 AND acknowledged_at IS NULL", |
| 205 |
) |
| 206 |
.bind(&hash) |
| 207 |
.execute(pool) |
| 208 |
.await? |
| 209 |
.rows_affected() |
| 210 |
> 0; |
| 211 |
Ok(moved) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
pub fn detail_copy(kind: AckKind, details: &serde_json::Value) -> String { |
| 224 |
match kind { |
| 225 |
AckKind::SettlementCurrencyChanged => { |
| 226 |
let from = details["from"].as_str().unwrap_or("its old currency"); |
| 227 |
let to = details["to"].as_str().unwrap_or("a new currency"); |
| 228 |
format!( |
| 229 |
"Your Stripe account now settles in {to}, where it used to settle in {from}.\n\n\ |
| 230 |
Every price you have already set is stored as a plain number, so those numbers \ |
| 231 |
now mean {to}. A price that was 10 {from} is now 10 {to}. We have not converted \ |
| 232 |
anything and we have not changed any of your prices, because guessing an \ |
| 233 |
exchange rate on your behalf is not ours to do.\n\n\ |
| 234 |
Please check your prices." |
| 235 |
) |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
#[cfg(test)] |
| 241 |
mod tests { |
| 242 |
use super::*; |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn kind_round_trip() { |
| 246 |
for k in AckKind::ALL { |
| 247 |
assert_eq!(k.to_string().parse::<AckKind>().unwrap(), *k); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
#[test] |
| 259 |
fn every_kind_is_allowed_by_the_check_constraint() { |
| 260 |
const SQL: &str = include_str!("../../migrations/196_pending_acknowledgements.sql"); |
| 261 |
let clause = SQL |
| 262 |
.split_once("kind TEXT NOT NULL CHECK (kind IN (") |
| 263 |
.expect("the constraint has the expected shape") |
| 264 |
.1 |
| 265 |
.split_once("))") |
| 266 |
.expect("the constraint list is closed") |
| 267 |
.0; |
| 268 |
let allowed: Vec<&str> = clause |
| 269 |
.split(',') |
| 270 |
.map(|s| s.trim().trim_matches('\'').trim()) |
| 271 |
.filter(|s| !s.is_empty()) |
| 272 |
.collect(); |
| 273 |
|
| 274 |
for kind in AckKind::ALL { |
| 275 |
let s = kind.to_string(); |
| 276 |
assert!( |
| 277 |
allowed.contains(&s.as_str()), |
| 278 |
"AckKind::{kind:?} (\"{s}\") is not in the pending_acknowledgements kind \ |
| 279 |
constraint. Adding a kind takes a migration as well as an enum variant.", |
| 280 |
); |
| 281 |
} |
| 282 |
assert_eq!(allowed.len(), AckKind::ALL.len()); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
#[test] |
| 287 |
fn titles_are_clean_copy() { |
| 288 |
for k in AckKind::ALL { |
| 289 |
let t = k.title(); |
| 290 |
assert!(!t.is_empty()); |
| 291 |
assert!( |
| 292 |
!t.contains('\u{2014}') && !t.contains(" -- "), |
| 293 |
"{t}: no connective dashes in user-facing copy" |
| 294 |
); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|