feat(supabase): clients M2 with RPCs, search and per-workspace budget

V1-1.2 (contract V1-1_OWNERS_ANIMALS.md §5, §9, §10). public.clients (person or organization, display name only, optional contact, folded search keys), create/update/set_archived as definer RPCs (identical not_found, idempotent create with divergent-replay refusal, revisions, archive read-only), search_clients invoker with keyset pages and phone search, write budget per workspace in UTC hour/day windows under a row lock, 20 000-client cap, ids-only audit hidden from platform staff (N9), nothing for anon or service_role. pgTAP 013 (130), field-limits parity test, sandbox suite (concurrency, budget races, 20 000-client search), types regenerated.
This commit is contained in:
bchanot committed 2026-10-25 21:09:45 +01:00
1 parent 2c0531d638
commit 4adbfcb608
9 files changed
+1916 -2

No files matched your search

+136
View File
@@ -308,6 +308,84 @@ export type Database = {
},
]
}
clients: {
Row: {
address_line1: string | null
address_line2: string | null
archived_at: string | null
city: string | null
country: string | null
create_request_digest: string
create_request_id: string
created_at: string
display_name: string
email: string | null
id: string
kind: Database['public']['Enums']['client_kind']
name_key: string | null
phone: string | null
phone_digits: string | null
postal_code: string | null
revision: number
search_key: string | null
updated_at: string
workspace_id: string
}
ComputedFields: never
Insert: {
address_line1?: string | null
address_line2?: string | null
archived_at?: string | null
city?: string | null
country?: string | null
create_request_digest: string
create_request_id: string
created_at?: string
display_name: string
email?: string | null
id?: string
kind: Database['public']['Enums']['client_kind']
name_key?: never
phone?: string | null
phone_digits?: never
postal_code?: string | null
revision?: number
search_key?: never
updated_at?: string
workspace_id: string
}
Update: {
address_line1?: string | null
address_line2?: string | null
archived_at?: string | null
city?: string | null
country?: string | null
create_request_digest?: string
create_request_id?: string
created_at?: string
display_name?: string
email?: string | null
id?: string
kind?: Database['public']['Enums']['client_kind']
name_key?: never
phone?: string | null
phone_digits?: never
postal_code?: string | null
revision?: number
search_key?: never
updated_at?: string
workspace_id?: string
}
Relationships: [
{
foreignKeyName: 'clients_workspace_id_fkey'
columns: ['workspace_id']
isOneToOne: false
referencedRelation: 'workspaces'
referencedColumns: ['id']
},
]
}
profile_roles: {
Row: {
granted_at: string
@@ -576,6 +654,22 @@ export type Database = {
}
Returns: string
}
create_client: {
Args: {
address_line1?: string
address_line2?: string
city?: string
country?: string
create_request_id: string
display_name: string
email?: string
kind: string
phone?: string
postal_code?: string
workspace_id: string
}
Returns: Json
}
create_sheet_draft: {
Args: {
change_note?: string
@@ -636,6 +730,30 @@ export type Database = {
}
Returns: string
}
search_clients: {
Args: {
after_id?: string
after_name_key?: string
archived?: boolean
page_size?: number
query?: string
workspace_id: string
}
Returns: {
archived_at: string
city: string
display_name: string
email: string
id: string
kind: Database['public']['Enums']['client_kind']
name_key: string
phone: string
}[]
}
set_client_archived: {
Args: { archived: boolean; client_id: string }
Returns: Json
}
set_sheet_visibility: {
Args: {
sheet_id: string
@@ -643,6 +761,22 @@ export type Database = {
}
Returns: string
}
update_client: {
Args: {
address_line1: string
address_line2: string
city: string
client_id: string
country: string
display_name: string
email: string
expected_revision: number
kind: string
phone: string
postal_code: string
}
Returns: Json
}
}
Enums: {
asset_status:
@@ -651,6 +785,7 @@ export type Database = {
| 'clean'
| 'rejected'
| 'deleted'
client_kind: 'person' | 'organization'
platform_role: 'owner' | 'admin' | 'moderator' | 'support'
sheet_status: 'active' | 'archived' | 'deleted'
sheet_version_status: 'draft' | 'published' | 'deprecated'
@@ -791,6 +926,7 @@ export const Constants = {
'rejected',
'deleted',
],
client_kind: ['person', 'organization'],
platform_role: ['owner', 'admin', 'moderator', 'support'],
sheet_status: ['active', 'archived', 'deleted'],
sheet_version_status: ['draft', 'published', 'deprecated'],
+45
View File
@@ -0,0 +1,45 @@
/**
* Bounds and patterns of the V1-1 record fields, shared by the forms (Zod
* schemas, D-R7) and proven equal to the SQL CHECK constraints by
* tests/db/field-limits.test.ts (behaviour on the migrated schema, never by
* parsing SQL). Contract: docs/development/V1-1_OWNERS_ANIMALS.md §5, §12.2.
*
* Lengths count code points, as PostgreSQL's char_length does: use
* codePointLength, never String.length (UTF-16 units) or Zod's .max() alone.
*/
export const CLIENT_FIELD_LIMITS = {
displayName: 120,
email: 254,
addressLine: 120,
postalCode: 16,
city: 80,
} as const
/** U+0000 to U+001F, U+007F to U+009F, U+2028, U+2029 (the SQL literal starts at U+0001: text cannot hold U+0000). */
export const CONTROL_CHARACTERS = /[\u0000-\u001F\u007F-\u009F\u2028\u2029]/u
/** Applied to the lower-cased value. JavaScript's \s is wider than SQL's, so the form refuses at least what SQL refuses. */
export const EMAIL_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/u
export const PHONE_E164_PATTERN = /^\+[1-9][0-9]{6,14}$/
export const COUNTRY_CODE_PATTERN = /^[A-Z]{2}$/
export function codePointLength(value: string): number {
return [...value].length
}
/**
* The text rule of contract §5: 1 to max code points, no leading or trailing
* space (U+0020, as SQL btrim checks), no control character.
*/
export function isCleanText(value: string, max: number): boolean {
const length = codePointLength(value)
return (
length >= 1 &&
length <= max &&
value === value.replace(/^ +| +$/g, '') &&
!CONTROL_CHARACTERS.test(value)
)
}