forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

work_entry.rs90 lines, 2.9 KiBHistoryRawDownload
1use uuid::Uuid;
2
3use crate::domain::monotonic_id::next_uuid;
4use crate::domain::timestamp::Timestamp;
5use crate::domain::user::UserId;
6use crate::error::{AppError, ValidationError};
7
8/// Typed work-entry row id. A UUIDv7 minted by the process-wide monotonic
9/// generator, so `id` order is mint order.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)]
11#[sqlx(transparent)]
12pub struct WorkEntryId(Uuid);
13
14impl WorkEntryId {
15 /// Minted from the process-wide monotonic generator, not a random v4:
16 /// the log sorts `check_in DESC, id DESC` and the id breaks the tie between
17 /// two stints checked in at the same instant.
18 pub fn generate() -> Self {
19 Self(next_uuid())
20 }
21
22 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
23 /// cannot take the newtype. Static `query!` binds take `self` directly.
24 pub fn uuid(&self) -> Uuid {
25 self.0
26 }
27
28 /// Parse a wire key. A key that parses as no UUID — a malformed path
29 /// segment — reads as the nil id, which matches no row: exactly the 404 a
30 /// dangling record key produced under the old store, without turning a
31 /// typo into a panic.
32 pub fn from_key(key: &str) -> Self {
33 Self(Uuid::parse_str(key).unwrap_or(Uuid::nil()))
34 }
35
36 /// The hyphenated wire form.
37 pub fn key(&self) -> String {
38 self.0.to_string()
39 }
40}
41
42/// One work stint of a staff member: server-stamped `check_in`, and
43/// `check_out` once closed. The wall clock is read server-side only — a
44/// client can never supply its own instants (managers correct closed entries
45/// through an explicit endpoint instead).
46///
47/// "At most one open stint per staff member" is no longer carried by a
48/// deterministic key: it is a partial unique index on the table
49/// (`work_entry_open`) over rows whose `check_out` is NULL, so the database
50/// itself refuses a second check-in.
51#[derive(Debug, Clone, sqlx::FromRow)]
52pub struct WorkEntry {
53 pub(crate) id: WorkEntryId,
54 #[sqlx(rename = "app_user")]
55 pub(crate) user: UserId,
56 pub(crate) check_in: Timestamp,
57 pub(crate) check_out: Option<Timestamp>,
58}
59
60impl WorkEntry {
61 pub fn get_id(&self) -> &WorkEntryId {
62 &self.id
63 }
64
65 pub fn get_user(&self) -> &UserId {
66 &self.user
67 }
68
69 pub fn get_check_in(&self) -> Timestamp {
70 self.check_in
71 }
72
73 pub fn get_check_out(&self) -> Option<Timestamp> {
74 self.check_out
75 }
76
77 pub fn is_open(&self) -> bool {
78 self.check_out.is_none()
79 }
80}
81
82/// The one ordering refusal both the web handler's pre-flight and the db
83/// layer's write-time guard owe for a corrected stint: the correction must
84/// still order as *stored* — check-out at or after check-in.
85pub(crate) fn out_before_in_error() -> AppError {
86 AppError::Validation(ValidationError::Invalid {
87 field: "check_out",
88 reason: "must be at or after check_in",
89 })
90}