forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB08aa13eAhmet Kaan Gümüş2 months ago378 commits

fee_plan_assignment.rs64 lines, 2.1 KiBHistoryRawDownload
1//! The `fee_plan_assignment` table: the reads over placements. The row is
2//! written by [`crate::service::fee_plan_assignment::assign`], whose claim
3//! ([`crate::db::cap::claim_and_create`]) is the write that freezes the plan.
4
5use surrealdb::types::RecordId;
6
7use crate::database::Database;
8use crate::db::page::PagedList;
9use crate::domain::fee_plan::FeePlanId;
10use crate::domain::fee_plan_assignment::{FeePlanAssignment, FeePlanAssignmentId};
11use crate::domain::user::UserId;
12use crate::error::AppError;
13
14pub async fn read(
15 db: &Database,
16 id: &FeePlanAssignmentId,
17) -> Result<Option<FeePlanAssignment>, AppError> {
18 Ok(db.select(id.record()).await?)
19}
20
21/// Who is on this plan, newest first.
22pub async fn list_for_plan(
23 db: &Database,
24 plan: &FeePlanId,
25 limit: Option<i64>,
26 offset: i64,
27) -> Result<(Vec<FeePlanAssignment>, i64), AppError> {
28 PagedList::new(
29 "fee_plan_assignment WHERE plan = $plan",
30 "ORDER BY created_at DESC, id DESC",
31 )
32 .bind("plan", plan.record())
33 .run(limit, offset, db)
34 .await
35}
36
37/// Is anybody on this plan? A scan, and no longer a guard: the edit and
38/// delete guards read the plan's own refcount, which no concurrent assign
39/// can be behind. Kept because a *test* asserting the rows and the counter
40/// agree is the only thing that would catch the counter drifting.
41pub async fn exists_for_plan(db: &Database, plan: &FeePlanId) -> Result<bool, AppError> {
42 let mut result = db
43 .query("SELECT VALUE id FROM fee_plan_assignment WHERE plan = $plan LIMIT 1")
44 .bind(("plan", plan.record()))
45 .await?
46 .check()?;
47 Ok(!result.take::<Vec<RecordId>>(0)?.is_empty())
48}
49
50/// Which plans a student is on — one student may hold several.
51pub async fn list_for_student(
52 db: &Database,
53 student: &UserId,
54 limit: Option<i64>,
55 offset: i64,
56) -> Result<(Vec<FeePlanAssignment>, i64), AppError> {
57 PagedList::new(
58 "fee_plan_assignment WHERE student = $student",
59 "ORDER BY created_at DESC, id DESC",
60 )
61 .bind("student", student.record())
62 .run(limit, offset, db)
63 .await
64}