| 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 |
use chrono::{DateTime, Duration, Utc}; |
| 33 |
use uuid::Uuid; |
| 34 |
|
| 35 |
use super::projects::SeededProject; |
| 36 |
use super::{EXAMPLE_EMAIL_DOMAIN, SeedError}; |
| 37 |
use crate::db::{self}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub const BUYER_POOL: usize = 14; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
const BUYER_ID_BASE: u128 = 0x0000_0000_0000_0000_0000_0000_0000_b101; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
const UNUSABLE_PASSWORD_HASH: &str = "!seed-background-buyer-no-login"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pub const BUYER_HANDLE_PREFIX: &str = "demo_buyer_"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub async fn seed_sales(pool: &sqlx::PgPool, projects: &[SeededProject]) -> Result<(), SeedError> { |
| 74 |
let buyers = seed_buyer_pool(pool).await?; |
| 75 |
|
| 76 |
let mut sold = 0; |
| 77 |
|
| 78 |
|
| 79 |
let mut offset = 0; |
| 80 |
for project in projects { |
| 81 |
let items = db::items::get_items_by_project(pool, project.project.id).await?; |
| 82 |
for spec in project.spec.items { |
| 83 |
let Some(item) = items.iter().find(|i| i.title == spec.title) else { |
| 84 |
tracing::warn!( |
| 85 |
title = spec.title, |
| 86 |
"example seed: item missing at the sales phase; it will report zero sales" |
| 87 |
); |
| 88 |
continue; |
| 89 |
}; |
| 90 |
for n in 0..spec.other_sales as usize { |
| 91 |
let buyer = buyers[(offset + n) % BUYER_POOL]; |
| 92 |
let purchased_at = purchase_date(spec.released_days_ago, n); |
| 93 |
let amount_cents = amount_for(item, n); |
| 94 |
record_purchase(pool, project, item, buyer, amount_cents, purchased_at).await?; |
| 95 |
sold += 1; |
| 96 |
} |
| 97 |
offset += spec.other_sales as usize + 1; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
let reconciled = reconcile_sales_counts(pool).await?; |
| 102 |
tracing::info!( |
| 103 |
purchases = sold, |
| 104 |
items = reconciled, |
| 105 |
"example seed: background sales seeded" |
| 106 |
); |
| 107 |
Ok(()) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
async fn seed_buyer_pool(pool: &sqlx::PgPool) -> Result<Vec<Uuid>, SeedError> { |
| 112 |
let mut ids = Vec::with_capacity(BUYER_POOL); |
| 113 |
for n in 0..BUYER_POOL { |
| 114 |
let id = Uuid::from_u128(BUYER_ID_BASE + n as u128); |
| 115 |
let handle = format!("{BUYER_HANDLE_PREFIX}{n:02}"); |
| 116 |
let email = format!("{}@{EXAMPLE_EMAIL_DOMAIN}", handle.replace('_', "-")); |
| 117 |
sqlx::query( |
| 118 |
r" |
| 119 |
INSERT INTO users ( |
| 120 |
id, username, email, password_hash, display_name, |
| 121 |
can_create_projects, email_verified |
| 122 |
) |
| 123 |
VALUES ($1, $2, $3, $4, $5, FALSE, TRUE) |
| 124 |
ON CONFLICT (id) DO UPDATE SET |
| 125 |
username = EXCLUDED.username, |
| 126 |
email = EXCLUDED.email |
| 127 |
", |
| 128 |
) |
| 129 |
.bind(id) |
| 130 |
.bind(&handle) |
| 131 |
.bind(&email) |
| 132 |
.bind(UNUSABLE_PASSWORD_HASH) |
| 133 |
.bind(format!("Demo Buyer {n:02}")) |
| 134 |
.execute(pool) |
| 135 |
.await?; |
| 136 |
ids.push(id); |
| 137 |
} |
| 138 |
Ok(ids) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
fn purchase_date(released_days_ago: i64, n: usize) -> DateTime<Utc> { |
| 148 |
|
| 149 |
|
| 150 |
let window = (released_days_ago - 2).max(1); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
let offset = (n as i64 * 7) % window; |
| 155 |
Utc::now() - Duration::days(released_days_ago - 1 - offset) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn amount_for(item: &db::DbItem, n: usize) -> i32 { |
| 165 |
if item.pwyw_enabled { |
| 166 |
|
| 167 |
|
| 168 |
let tip = [0, 0, 250, 0, 100, 500, 0, 150][n % 8]; |
| 169 |
return item.pwyw_min_cents.unwrap_or(0) + tip; |
| 170 |
} |
| 171 |
item.price_cents |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
pub(super) async fn record_purchase( |
| 187 |
pool: &sqlx::PgPool, |
| 188 |
project: &SeededProject, |
| 189 |
item: &db::DbItem, |
| 190 |
buyer_id: Uuid, |
| 191 |
amount_cents: i32, |
| 192 |
purchased_at: DateTime<Utc>, |
| 193 |
) -> Result<Uuid, SeedError> { |
| 194 |
let (seller_username, currency): (String, String) = sqlx::query_as( |
| 195 |
"SELECT username, lower(settlement_currency::text) FROM users WHERE id = $1", |
| 196 |
) |
| 197 |
.bind(project.user_id) |
| 198 |
.fetch_one(pool) |
| 199 |
.await?; |
| 200 |
|
| 201 |
let transaction_id: Uuid = sqlx::query_scalar( |
| 202 |
r" |
| 203 |
INSERT INTO transactions ( |
| 204 |
buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, |
| 205 |
currency, status, stripe_payment_intent_id, |
| 206 |
created_at, completed_at, item_title, seller_username |
| 207 |
) |
| 208 |
VALUES ($1, $2, $3, $4, 0, $5, 'completed', $6, $7, $7, $8, $9) |
| 209 |
RETURNING id |
| 210 |
", |
| 211 |
) |
| 212 |
.bind(buyer_id) |
| 213 |
.bind(project.user_id) |
| 214 |
.bind(item.id) |
| 215 |
.bind(amount_cents) |
| 216 |
.bind(¤cy) |
| 217 |
.bind(format!("pi_demo_{}_{}", item.id, buyer_id.simple())) |
| 218 |
.bind(purchased_at) |
| 219 |
.bind(&item.title) |
| 220 |
.bind(&seller_username) |
| 221 |
.fetch_one(pool) |
| 222 |
.await?; |
| 223 |
|
| 224 |
Ok(transaction_id) |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
async fn reconcile_sales_counts(pool: &sqlx::PgPool) -> Result<u64, SeedError> { |
| 235 |
let res = sqlx::query( |
| 236 |
r" |
| 237 |
UPDATE items i SET sales_count = ( |
| 238 |
SELECT COUNT(*) FROM transactions t |
| 239 |
WHERE t.item_id = i.id AND t.status = 'completed' |
| 240 |
) |
| 241 |
FROM projects p, users u |
| 242 |
WHERE i.project_id = p.id |
| 243 |
AND p.user_id = u.id |
| 244 |
AND lower(u.email) LIKE $1 |
| 245 |
", |
| 246 |
) |
| 247 |
.bind(format!("%@{EXAMPLE_EMAIL_DOMAIN}")) |
| 248 |
.execute(pool) |
| 249 |
.await?; |
| 250 |
Ok(res.rows_affected()) |
| 251 |
} |
| 252 |
|
| 253 |
#[cfg(test)] |
| 254 |
mod tests { |
| 255 |
use super::*; |
| 256 |
use crate::seed::creators::ROSTER; |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn other_sales_fit_the_buyer_pool() { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
for creator in ROSTER { |
| 264 |
for item in creator.project.items { |
| 265 |
assert!( |
| 266 |
item.other_sales as usize <= BUYER_POOL, |
| 267 |
"{}: other_sales {} exceeds the pool of {BUYER_POOL}", |
| 268 |
item.title, |
| 269 |
item.other_sales |
| 270 |
); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn every_background_sale_postdates_its_release() { |
| 277 |
for creator in ROSTER { |
| 278 |
for item in creator.project.items { |
| 279 |
for n in 0..item.other_sales as usize { |
| 280 |
let released = Utc::now() - Duration::days(item.released_days_ago); |
| 281 |
let bought = purchase_date(item.released_days_ago, n); |
| 282 |
assert!( |
| 283 |
bought > released, |
| 284 |
"{}: background sale {n} predates its release", |
| 285 |
item.title |
| 286 |
); |
| 287 |
assert!( |
| 288 |
bought < Utc::now(), |
| 289 |
"{}: background sale {n} is in the future", |
| 290 |
item.title |
| 291 |
); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
#[test] |
| 298 |
fn demo_buyer_purchases_postdate_release() { |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
for creator in ROSTER { |
| 304 |
for item in creator.project.items { |
| 305 |
let Some(purchase) = crate::seed::buyer::PURCHASES |
| 306 |
.iter() |
| 307 |
.find(|p| p.title == item.title) |
| 308 |
else { |
| 309 |
continue; |
| 310 |
}; |
| 311 |
assert!( |
| 312 |
purchase.days_ago < item.released_days_ago, |
| 313 |
"{}: bought {} days ago but released only {} days ago", |
| 314 |
item.title, |
| 315 |
purchase.days_ago, |
| 316 |
item.released_days_ago |
| 317 |
); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn total_sales_stay_within_the_pool_plus_the_demo_buyer() { |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
for creator in ROSTER { |
| 328 |
for item in creator.project.items { |
| 329 |
let demo = crate::seed::buyer::PURCHASES |
| 330 |
.iter() |
| 331 |
.any(|p| p.title == item.title) as usize; |
| 332 |
assert!( |
| 333 |
item.other_sales as usize + demo <= BUYER_POOL + 1, |
| 334 |
"{}: more buyers than accounts exist", |
| 335 |
item.title |
| 336 |
); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn release_dates_are_spread_rather_than_stamped() { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
let days: Vec<i64> = ROSTER |
| 347 |
.iter() |
| 348 |
.flat_map(|c| c.project.items.iter().map(|i| i.released_days_ago)) |
| 349 |
.collect(); |
| 350 |
let mut distinct = days.clone(); |
| 351 |
distinct.sort_unstable(); |
| 352 |
distinct.dedup(); |
| 353 |
assert!( |
| 354 |
distinct.len() >= days.len() * 3 / 4, |
| 355 |
"release dates are bunched: {} distinct across {} items", |
| 356 |
distinct.len(), |
| 357 |
days.len() |
| 358 |
); |
| 359 |
let oldest = days.iter().max().copied().unwrap_or(0); |
| 360 |
assert!( |
| 361 |
oldest >= 180, |
| 362 |
"the catalog should have a history: oldest release is {oldest} days old" |
| 363 |
); |
| 364 |
} |
| 365 |
} |
| 366 |
|