The legal practice: clients, lawyers, contracts, papers, the journal
Lanna Desk's larger work is contracts, general ones and artists', for clients who are Thai or foreign, people or organizations, with several lawyers in the team. The back office gets a Legal section (phase 9 in docs/PLAN.md): - Lawyers: license number, contact, the login of one who uses the app (then "My contracts"), in the team or left. - Clients, as record cards: person or organization, names in English and Thai, stage name or brand, a Thai ID number (checked), a passport or a registration number, the lawyer in charge. - Contracts: code LD-<year>-<n>, the general kinds and the artist kinds (recording, management, publishing, performance, license, merchandise), other party, status, lawyer, dates and value; a contract in force shows its notice day and its end ahead. Filters: general or artists, a lawyer, mine, those over. - Versions: a text kept here, a file (PDF or scan, encrypted as the client's document) or both. They never change: the database refuses UPDATE and DELETE, a version's file cannot be deleted and no document's checksum can change. Any version reads against the one before as a line diff. - Paper copies: the id <code>-P<n> to write on the paper or print as a 90 x 50 mm label with a QR code; where it lies, its state and holder, each move journaled; found again by its id. - The journal: every change in words, in the change's transaction, each entry chained to the one before by SHA-256. The database refuses to change, delete or truncate it; /journal checks the chain and every version's text and fingerprints against its entry, shows the last hash and exports CSV. The sidebar groups its sections: Legal, Visas and stays, Office. Demo data (seed-demo) adds three lawyers, six clients and seven contracts with versions, papers and moves. Documents take a third owner, the client (migration 0009). Checks: fmt, clippy, 115 unit and 62 integration tests (new: versions and the journal, the database refusing changes, a change around the app breaking the chain, paper ids and moves, the demo checking whole); screenshots of the new pages in light, dark and on a phone.
This commit is contained in:
1 parent
424f212b23
commit
16e4cdc73d
32 files changed
+4758
-45
No files matched your search
+38
-15
@@ -20,20 +20,29 @@ use crate::s3::Put;
|
||||
pub enum Owner {
|
||||
Person(i64),
|
||||
Company(i64),
|
||||
/// A client of the legal practice: the files of their contracts.
|
||||
Client(i64),
|
||||
}
|
||||
|
||||
impl Owner {
|
||||
pub fn person(self) -> Option<i64> {
|
||||
match self {
|
||||
Owner::Person(id) => Some(id),
|
||||
Owner::Company(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn company(self) -> Option<i64> {
|
||||
match self {
|
||||
Owner::Company(id) => Some(id),
|
||||
Owner::Person(_) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn client(self) -> Option<i64> {
|
||||
match self {
|
||||
Owner::Client(id) => Some(id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +51,7 @@ impl Owner {
|
||||
match self {
|
||||
Owner::Person(id) => format!("/people/{id}/documents"),
|
||||
Owner::Company(id) => format!("/companies/{id}/documents"),
|
||||
Owner::Client(id) => format!("/clients/{id}/documents"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +59,7 @@ impl Owner {
|
||||
match self {
|
||||
Owner::Person(_) => PERSON_KINDS,
|
||||
Owner::Company(_) => COMPANY_KINDS,
|
||||
Owner::Client(_) => CLIENT_KINDS,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +120,12 @@ pub const COMPANY_KINDS: &[Kind] = &[
|
||||
k("other", "Other", None),
|
||||
];
|
||||
|
||||
/// A client's documents: the files of their contracts' versions.
|
||||
pub const CLIENT_KINDS: &[Kind] = &[
|
||||
k("contract", "Contract version", None),
|
||||
k("other", "Other", None),
|
||||
];
|
||||
|
||||
/// The kinds that are cards with two sides: uploaded as two pictures at
|
||||
/// once, the first is the front and the second the back, and the pack
|
||||
/// prints both on one A4 sheet.
|
||||
@@ -375,11 +392,12 @@ pub async fn set_back(tx: &mut Transaction<'_, Postgres>, back: i64, front: i64)
|
||||
|
||||
pub async fn list(db: &PgPool, owner: Owner) -> Result<Vec<Document>> {
|
||||
let docs: Vec<Document> = sqlx::query_as(
|
||||
"SELECT * FROM documents \
|
||||
WHERE person_id IS NOT DISTINCT FROM $1 AND company_id IS NOT DISTINCT FROM $2",
|
||||
"SELECT * FROM documents WHERE person_id IS NOT DISTINCT FROM $1 \
|
||||
AND company_id IS NOT DISTINCT FROM $2 AND client_id IS NOT DISTINCT FROM $3",
|
||||
)
|
||||
.bind(owner.person())
|
||||
.bind(owner.company())
|
||||
.bind(owner.client())
|
||||
.fetch_all(db)
|
||||
.await?;
|
||||
Ok(sorted(owner, docs))
|
||||
@@ -387,12 +405,13 @@ pub async fn list(db: &PgPool, owner: Owner) -> Result<Vec<Document>> {
|
||||
|
||||
pub async fn get(db: &PgPool, owner: Owner, id: i64) -> Result<Option<Document>> {
|
||||
Ok(sqlx::query_as(
|
||||
"SELECT * FROM documents WHERE id = $1 \
|
||||
AND person_id IS NOT DISTINCT FROM $2 AND company_id IS NOT DISTINCT FROM $3",
|
||||
"SELECT * FROM documents WHERE id = $1 AND person_id IS NOT DISTINCT FROM $2 \
|
||||
AND company_id IS NOT DISTINCT FROM $3 AND client_id IS NOT DISTINCT FROM $4",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(owner.person())
|
||||
.bind(owner.company())
|
||||
.bind(owner.client())
|
||||
.fetch_optional(db)
|
||||
.await?)
|
||||
}
|
||||
@@ -402,11 +421,12 @@ pub async fn same_file(db: &PgPool, owner: Owner, sha256: &str) -> Result<Option
|
||||
Ok(sqlx::query_as(
|
||||
"SELECT * FROM documents WHERE sha256 = $1 \
|
||||
AND person_id IS NOT DISTINCT FROM $2 AND company_id IS NOT DISTINCT FROM $3 \
|
||||
ORDER BY id LIMIT 1",
|
||||
AND client_id IS NOT DISTINCT FROM $4 ORDER BY id LIMIT 1",
|
||||
)
|
||||
.bind(sha256)
|
||||
.bind(owner.person())
|
||||
.bind(owner.company())
|
||||
.bind(owner.client())
|
||||
.fetch_optional(db)
|
||||
.await?)
|
||||
}
|
||||
@@ -426,7 +446,7 @@ pub async fn link_is_theirs(db: &PgPool, person: i64, link: &Link, id: i64) -> R
|
||||
}
|
||||
|
||||
pub async fn insert(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
conn: &mut sqlx::PgConnection,
|
||||
owner: Owner,
|
||||
meta: &Meta,
|
||||
file: &NewFile<'_>,
|
||||
@@ -436,9 +456,9 @@ pub async fn insert(
|
||||
let id = sqlx::query_scalar(
|
||||
"INSERT INTO documents (person_id, company_id, kind, passport_id, visa_id, \
|
||||
arrival_id, extension_id, reentry_permit_id, report_id, address_id, work_permit_id, \
|
||||
dated_on, note, file_name, content_type, size_bytes, sha256, uploaded_by) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18) \
|
||||
RETURNING id",
|
||||
dated_on, note, file_name, content_type, size_bytes, sha256, uploaded_by, client_id) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, \
|
||||
$19) RETURNING id",
|
||||
)
|
||||
.bind(owner.person())
|
||||
.bind(owner.company())
|
||||
@@ -458,7 +478,8 @@ pub async fn insert(
|
||||
.bind(file.size)
|
||||
.bind(file.sha256)
|
||||
.bind(by)
|
||||
.fetch_one(&mut **tx)
|
||||
.bind(owner.client())
|
||||
.fetch_one(conn)
|
||||
.await?;
|
||||
Ok(id)
|
||||
}
|
||||
@@ -475,7 +496,7 @@ pub async fn update(
|
||||
extension_id = $8, reentry_permit_id = $9, report_id = $10, address_id = $11, \
|
||||
work_permit_id = $12, dated_on = $13, note = $14, updated_at = now() \
|
||||
WHERE id = $1 AND person_id IS NOT DISTINCT FROM $2 \
|
||||
AND company_id IS NOT DISTINCT FROM $3",
|
||||
AND company_id IS NOT DISTINCT FROM $3 AND client_id IS NOT DISTINCT FROM $15",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(owner.person())
|
||||
@@ -491,6 +512,7 @@ pub async fn update(
|
||||
.bind(ids[7])
|
||||
.bind(meta.dated_on)
|
||||
.bind(&meta.note)
|
||||
.bind(owner.client())
|
||||
.execute(&mut **tx)
|
||||
.await?;
|
||||
Ok(())
|
||||
@@ -498,12 +520,13 @@ pub async fn update(
|
||||
|
||||
pub async fn delete(tx: &mut Transaction<'_, Postgres>, owner: Owner, id: i64) -> Result<bool> {
|
||||
let n = sqlx::query(
|
||||
"DELETE FROM documents WHERE id = $1 \
|
||||
AND person_id IS NOT DISTINCT FROM $2 AND company_id IS NOT DISTINCT FROM $3",
|
||||
"DELETE FROM documents WHERE id = $1 AND person_id IS NOT DISTINCT FROM $2 \
|
||||
AND company_id IS NOT DISTINCT FROM $3 AND client_id IS NOT DISTINCT FROM $4",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(owner.person())
|
||||
.bind(owner.company())
|
||||
.bind(owner.client())
|
||||
.execute(&mut **tx)
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
||||
Reference in new issue
Block a user