Skip to content

Commit e99cf2c

Browse files
committed
fix(plugin-approvals): keep the single lock door's readability anchor; type lockSource in the client SDK
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzeiHatzB4rRx1t4Php
1 parent 00e32ec commit e99cf2c

8 files changed

Lines changed: 95 additions & 59 deletions

File tree

‎.changeset/37151-record-lock-source.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'@objectstack/spec': patch
33
'@objectstack/plugin-approvals': patch
44
'@objectstack/rest': patch
5+
'@objectstack/client': patch
56
---
67

78
feat(plugin-approvals): the record lock reads say whether a row is held by its own approval or by its master's
@@ -18,4 +19,6 @@ When both hold (a line with a pending request of its own, under a held master),
1819

1920
The `IApprovalService` return types gain the optional `lockSource?: 'record' | 'master'`, so an existing implementation that answers `{ locked }` alone still satisfies the contract; the REST doors then serve `{ locked: true }` without a kind.
2021

21-
Both doors now count a row as readable by one rule: the caller's read must hand back the row whose id is the asked id, spelled as asked. The single-record door used to accept any row the read handed back, so an id the database matched loosely (a different letter case under a case-insensitive collation, `07` for a numeric `7`) answered the lock there and `null` in the batch door. It now answers `null` at both. A client asking with the ids the server gave it sees no change.
22+
Which rows count as readable is unchanged at both doors. The two doors keep different readability anchors on purpose: the single door counts any returned row, the batch door only the row whose id is the asked id; aligning them would narrow a published answer (seat ruling on PR #37191). So an id the database matches only under another spelling (a different letter case, `07` for a numeric `7`) answers its lock at the single-record door and `null` in the batch door; a client asking with the ids the server gave it gets the same answer from both.
23+
24+
`@objectstack/client`: `client.approvals.getRecordLock` and `getRecordLocks` type the answer with the optional `lockSource?: 'record' | 'master'`.

‎content/docs/automation/approvals.mdx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,7 @@ A line under a locked record that also has a pending request of its own answers
470470
`lockSource`.
471471

472472
The record is read as the caller: a record they cannot read answers
473-
`404 RECORD_NOT_FOUND`, the same body a record id that does not exist gets, and
474-
so does an id spelled differently from the record's own (a different letter
475-
case, `07` for `7`), even where the database matches it. The
473+
`404 RECORD_NOT_FOUND`, the same body a record id that does not exist gets. The
476474
SDK call is `client.approvals.getRecordLock(object, recordId)`, and the service
477475
member is `IApprovalService.getRecordLock`. An approvals service without that
478476
member answers `501`.
@@ -492,7 +490,10 @@ curl -b cookies.txt -X POST -H 'Content-Type: application/json' \
492490

493491
Each entry is what the single-record read answers for that id, in the order the
494492
ids were sent; `null` stands where that read answers `404`, so a record the
495-
caller cannot read and one that does not exist look the same. One request
493+
caller cannot read and one that does not exist look the same. Send the ids as
494+
the server spelled them: an id the database matches only under another spelling
495+
(a different letter case, `07` for `7`) answers `null` here, where the
496+
single-record read answers its lock. One request
496497
carries at most 200 ids (`APPROVAL_RECORD_LOCKS_MAX_RECORD_IDS`); a longer list,
497498
an empty one or any other body key is refused with `400 VALIDATION_ERROR`. The
498499
SDK call is `client.approvals.getRecordLocks(object, recordIds)`, the service

‎packages/client/src/client.test.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,19 +758,29 @@ describe('Approvals namespace (ADR-0019)', () => {
758758
});
759759

760760
it('[#26314] reads a record\'s approval lock off the envelope, path segments encoded', async () => {
761-
const { client, fetchMock } = createMockClient({ success: true, data: { locked: true } });
761+
const { client, fetchMock } = createMockClient({ success: true, data: { locked: true, lockSource: 'record' } });
762762
const result = await client.approvals.getRecordLock('order', 'rec/1');
763-
expect(result).toEqual({ locked: true });
763+
expect(result).toEqual({ locked: true, lockSource: 'record' });
764+
// The answer types the kind (the test program compiles this line).
765+
const kind: 'record' | 'master' | undefined = result.lockSource;
766+
expect(kind).toBe('record');
764767
const [url, opts] = fetchMock.mock.calls[0];
765768
expect(String(url)).toBe('http://localhost:3000/api/v1/approvals/records/order/rec%2F1/lock');
766769
expect(opts?.method ?? 'GET').toBe('GET');
767770
});
768771

769772
it('reads the approval locks of a page of records in one POST, object segment encoded', async () => {
770-
const records = [{ recordId: 'rec_1', locked: true }, { recordId: 'rec_2', locked: null }];
773+
const records = [
774+
{ recordId: 'rec_1', locked: true, lockSource: 'master' },
775+
{ recordId: 'rec_2', locked: null },
776+
{ recordId: 'rec_3', locked: false },
777+
];
771778
const { client, fetchMock } = createMockClient({ success: true, data: { records } });
772779
const result = await client.approvals.getRecordLocks('sales order', ['rec_1', 'rec_2']);
773780
expect(result).toEqual({ records });
781+
// The entry types the kind (the test program compiles this line).
782+
const kinds: Array<'record' | 'master' | undefined> = result.records.map((entry) => entry.lockSource);
783+
expect(kinds).toEqual(['master', undefined, undefined]);
774784
expect(fetchMock).toHaveBeenCalledTimes(1);
775785
const [url, opts] = fetchMock.mock.calls[0];
776786
expect(String(url)).toBe('http://localhost:3000/api/v1/approvals/records/sales%20order/locks');

‎packages/client/src/index.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7148,18 +7148,25 @@ export class ObjectStackClient {
71487148
},
71497149

71507150
/**
7151-
* Whether a record is held by a locking approval — `{ locked }` and
7151+
* Whether a record is held by a locking approval — `{ locked }`, plus on a
7152+
* locked record which approval holds it, `lockSource`: `'record'` (its own;
7153+
* deleting it cancels the request) or `'master'` (the record a
7154+
* master-detail line belongs to; the server refuses the line's delete too) —
7155+
* and
71527156
* nothing else, answered to every caller who can read the record, whether
71537157
* or not they take part in the approval (`IApprovalService.getRecordLock`).
71547158
* A record the caller cannot read answers 404, exactly as a record id that
71557159
* does not exist; a deployment without the read answers 501.
71567160
*/
7157-
getRecordLock: async (object: string, recordId: string): Promise<{ locked: boolean }> => {
7161+
getRecordLock: async (
7162+
object: string,
7163+
recordId: string,
7164+
): Promise<{ locked: boolean; lockSource?: 'record' | 'master' }> => {
71587165
const route = this.getRoute('approvals');
71597166
const res = await this.fetch(
71607167
`${this.baseUrl}${route}/records/${encodeURIComponent(object)}/${encodeURIComponent(recordId)}/lock`,
71617168
);
7162-
return this.unwrapResponse<{ locked: boolean }>(res);
7169+
return this.unwrapResponse<{ locked: boolean; lockSource?: 'record' | 'master' }>(res);
71637170
},
71647171

71657172
/**
@@ -7173,13 +7180,15 @@ export class ObjectStackClient {
71737180
getRecordLocks: async (
71747181
object: string,
71757182
recordIds: readonly string[],
7176-
): Promise<{ records: Array<{ recordId: string; locked: boolean | null }> }> => {
7183+
): Promise<{ records: Array<{ recordId: string; locked: boolean | null; lockSource?: 'record' | 'master' }> }> => {
71777184
const route = this.getRoute('approvals');
71787185
const res = await this.fetch(`${this.baseUrl}${route}/records/${encodeURIComponent(object)}/locks`, {
71797186
method: 'POST',
71807187
body: JSON.stringify({ recordIds: [...recordIds] }),
71817188
});
7182-
return this.unwrapResponse<{ records: Array<{ recordId: string; locked: boolean | null }> }>(res);
7189+
return this.unwrapResponse<{
7190+
records: Array<{ recordId: string; locked: boolean | null; lockSource?: 'record' | 'master' }>;
7191+
}>(res);
71837192
},
71847193

71857194
/**

‎packages/plugins/plugin-approvals/src/approval-service.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11333,11 +11333,18 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1133311333
* it does at {@link getRecordLock}, with the same `lockSource`. Entry `i`
1133411334
* answers `recordIds[i]`.
1133511335
*
11336-
* ## Which rows count as readable — one rule at both doors
11337-
*
11338-
* A row is readable when the caller's read hands back a row whose id IS the
11339-
* asked id, spelled as asked — the rule {@link getRecordLock} applies too
11340-
* (see its doc block for why a loosely matched id answers `null` at both).
11336+
* ## Which rows count as readable — the row whose id IS the asked id
11337+
*
11338+
* A row is readable here when the caller's read hands back a row whose id
11339+
* IS the asked id, spelled as asked: one `$in` read cannot say which asked
11340+
* id a row the engine spells differently answers (a case-insensitive
11341+
* collation, a numeric id asked as `'07'`), so such an id answers `null`
11342+
* here. {@link getRecordLock} counts any row its read hands back, so for
11343+
* such an id the two doors differ, deliberately: the two doors keep
11344+
* different readability anchors on purpose, and aligning them would narrow
11345+
* a published answer (seat ruling on PR #37191). For an id spelled as the
11346+
* engine spells it — what a list holds — entry `i` is exactly the single
11347+
* door's answer.
1134111348
*
1134211349
* The cap is the door's to refuse loudly; here a blank id answers `null`.
1134311350
*/
@@ -11462,15 +11469,11 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1146211469
* id, existing or not. ⚠️ Probing with {@link SYSTEM_CTX} here would admit
1146311470
* every authenticated user in the tenant.
1146411471
*
11465-
* The row read must be the asked id's own: a row the engine hands back under
11466-
* another spelling (a case-insensitive collation, a numeric id asked as
11467-
* `'07'`) answers `null`, as at {@link getRecordLocks}. That is the one rule
11468-
* both doors can apply alike: one `$in` read for a page cannot say which
11469-
* asked id a row spelled otherwise answers — a page asking two spellings of
11470-
* one row reads exactly like a page asking a missing id — and resolving it
11471-
* would cost a read per row. The ids a client holds are the ones the engine
11472-
* spelled, so nothing it asks is refused by this; and the lock is judged
11473-
* under the row's own id, never under another spelling of it.
11472+
* Any row the read hands back counts, whatever spelling of the id the
11473+
* engine hands back (a case-insensitive collation, a numeric id asked as
11474+
* `'07'`); {@link getRecordLocks} counts only the row whose id IS the asked
11475+
* id. The two doors keep different readability anchors on purpose:
11476+
* aligning them would narrow a published answer (seat ruling on PR #37191).
1147411477
*
1147511478
* ## What is answered — the lock and its kind, nothing about the request
1147611479
*
@@ -11498,7 +11501,7 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
1149811501
const readable = await this.engine.find(objectName, {
1149911502
where: { id }, fields: ['id'], limit: 1, context,
1150011503
});
11501-
if (!Array.isArray(readable) || !readable.some((row: any) => String(row?.id) === id)) return null;
11504+
if (!Array.isArray(readable) || readable.length === 0) return null;
1150211505
if (await detailRowHeldByLockingApproval(this.engine, objectName, id)) return recordLockAnswer(true, false);
1150311506
return recordLockAnswer(false, await recordHeldByLockingApproval(this.engine, objectName, id));
1150411507
}

‎packages/plugins/plugin-approvals/src/record-lock-read.integration.test.ts‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
* - `lockSource` says which approval holds a locked row — its own
2323
* (`'record'`) or its master's (`'master'`, winning when both hold) — and
2424
* so which delete the server refuses; the page read gives each row the
25-
* single read's kind, and counts a row readable by the single read's rule,
26-
* a loosely matched id included (ruling 7148480184).
25+
* single read's kind (ruling 7148480184);
26+
* - the two doors keep their own readability anchors (seat ruling on PR
27+
* #37191): the single read counts any row its read hands back, the page
28+
* read only the row whose id IS the asked id.
2729
*
2830
* The REST door's half (one 404 for both `null`s, the closed query set, the
2931
* `{ locked, lockSource }`-only payload) is pinned in `@objectstack/rest`; the whole path
@@ -521,14 +523,14 @@ describe('lockSource — which approval holds a locked row, the same at the sing
521523
});
522524

523525
/**
524-
* The readability anchor, one rule at both doors (#37151 comment 7148285178):
525-
* a row counts as readable when the caller's read hands back the row whose id
526-
* IS the asked id. `getRecordLock` used to count any row the read handed back,
527-
* so a loosely matched id (a case-insensitive collation, `'07'` for `'7'`)
528-
* answered the lock there and `null` at the page read, which cannot say which
529-
* asked id a row spelled otherwise answers without a read per row.
526+
* The readability anchors, kept apart on purpose (seat ruling on PR #37191):
527+
* `getRecordLock` counts any row its read hands back, whatever spelling of the
528+
* id the engine hands back; `getRecordLocks` counts only the row whose id IS
529+
* the asked id. Aligning them would narrow a published answer, so each door's
530+
* own anchor is pinned here, and the two doors agree for every id spelled as
531+
* the engine spells it.
530532
*/
531-
describe('both lock reads count a row readable by one rule — a loosely matched id included', () => {
533+
describe('the two doors keep their own readability anchors — a loosely matched id', () => {
532534
const LINE = lineOf(HELD);
533535

534536
/** Rewrite every `id` value in a read's predicate to lower case: a case-insensitive collation. */
@@ -555,7 +557,7 @@ describe('both lock reads count a row readable by one rule — a loosely matched
555557
}, { object });
556558
}
557559

558-
it('answers a loosely matched id `null` at both reads, and the echoed spelling of the same row its lock', async () => {
560+
it('the single door answers the lock for a loosely spelled id; the page door answers it `null`', async () => {
559561
const rig = await boot();
560562
caseInsensitiveIds(rig.engine, LINE);
561563
caseInsensitiveIds(rig.engine, HELD);
@@ -564,24 +566,25 @@ describe('both lock reads count a row readable by one rule — a loosely matched
564566
const found = await rig.engine.find(LINE, { where: { id: loose(recordA(LINE)) }, fields: ['id'], context: SYSTEM });
565567
expect((found as any[]).map((row) => row.id)).toEqual([recordA(LINE)]);
566568

567-
expect(await rig.svc.getRecordLock(LINE, loose(recordA(LINE)), asUser(SUBMITTER))).toBeNull();
568-
expect(await rig.svc.getRecordLock(LINE, recordA(LINE), asUser(SUBMITTER))).toEqual(BYMASTER);
569+
// The single door: any row the read hands back counts — the line of a held master answers its lock.
570+
expect(await rig.svc.getRecordLock(LINE, loose(recordA(LINE)), asUser(SUBMITTER))).toEqual(BYMASTER);
571+
expect(await rig.svc.getRecordLock(LINE, loose(recordB(LINE)), asUser(SUBMITTER))).toEqual({ locked: false });
572+
// …and still `null` for a row the caller cannot read, however it is spelled.
573+
expect(await rig.svc.getRecordLock(LINE, loose(recordB(LINE)), asUser(READER))).toBeNull();
574+
575+
// The page door: only the row whose id IS the asked id — a loose spelling answers `null`,
576+
// alone and beside the echoed spelling of the same row.
569577
expect(await rig.svc.getRecordLocks(LINE, [loose(recordA(LINE))], asUser(SUBMITTER))).toEqual([null]);
570-
// Beside the echoed spelling of the same row, beside a missing id: one answer per spelling.
571-
const lineIds = [loose(recordA(LINE)), recordB(LINE), MISSING, loose(recordB(LINE)), recordA(LINE)];
572-
expect(await rig.svc.getRecordLocks(LINE, lineIds, asUser(SUBMITTER)))
573-
.toEqual([null, { locked: false }, null, null, BYMASTER]);
578+
expect(await rig.svc.getRecordLocks(LINE, [loose(recordA(LINE)), recordA(LINE), MISSING], asUser(SUBMITTER)))
579+
.toEqual([null, BYMASTER, null]);
580+
581+
// Every id spelled as the engine spells it: the two doors agree, for every caller.
582+
const echoed = [recordA(LINE), recordB(LINE), MISSING, recordA(LINE)];
574583
for (const caller of [SUBMITTER, APPROVER, READER]) {
575-
for (const [object, ids] of [
576-
[LINE, lineIds],
577-
[LINE, [loose(recordA(LINE))]],
578-
[HELD, [loose(recordA(HELD)), recordB(HELD), loose(MISSING), recordA(HELD)]],
579-
] as Array<[string, string[]]>) {
580-
const page = await rig.svc.getRecordLocks(object, ids, asUser(caller));
581-
const oneByOne = [];
582-
for (const id of ids) oneByOne.push(await rig.svc.getRecordLock(object, id, asUser(caller)));
583-
expect(page, `${caller} ${object} ${ids.join(',')}`).toEqual(oneByOne);
584-
}
584+
const page = await rig.svc.getRecordLocks(LINE, echoed, asUser(caller));
585+
const oneByOne = [];
586+
for (const id of echoed) oneByOne.push(await rig.svc.getRecordLock(LINE, id, asUser(caller)));
587+
expect(page, caller).toEqual(oneByOne);
585588
}
586589
});
587590

‎packages/rest/src/approvals-routes.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,10 @@ export function registerApprovalsEndpoints(this: ApprovalsRouteHost, basePath: s
549549

550550
// The lock of a PAGE of records, in one request: what a list asks before it
551551
// opens a cell editor, so moving through the rows sends no request per row.
552-
// Each entry is exactly the single-record door's answer for that id —
552+
// Each entry is exactly the single-record door's answer for that id, as
553+
// the engine spells it (an id matched only under another spelling answers
554+
// `locked: null` here: the two doors keep different readability anchors
555+
// on purpose, seat ruling on PR #37191) —
553556
// `locked`, and `lockSource` on a locked row — the 404 folded to
554557
// `locked: null` (unreadable or missing, told apart by nothing); a service
555558
// without `getRecordLocks` answers 501, never one `getRecordLock` per id.

‎packages/spec/src/contracts/approval-service.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,7 @@ export interface IApprovalService {
11051105
* the carrier.
11061106
*
11071107
* - **Who is answered:** every caller who can READ the record, judged by
1108-
* reading it AS THE CALLER (object read, then row-level access); the row
1109-
* read must be the asked id's own, spelled as asked. A caller
1108+
* reading it AS THE CALLER (object read, then row-level access). A caller
11101109
* who cannot read it gets `null` — exactly the answer for a record id that
11111110
* does not exist, ⛔ never one that tells the two apart. A caller refused
11121111
* the object's read outright is refused the same way for every id.
@@ -1151,15 +1150,20 @@ export interface IApprovalService {
11511150
* per row.
11521151
*
11531152
* Entry `i` of the answer answers `recordIds[i]` with exactly what
1154-
* {@link getRecordLock} answers for that id: `{ locked, lockSource }` for a
1153+
* {@link getRecordLock} answers for that id, spelled as the engine spells it: `{ locked, lockSource }` for a
11551154
* locked record and `{ locked: false }` for an unlocked one when the caller
11561155
* can read the record, `null` when it cannot or when it does not exist,
11571156
* ⛔ never an entry that tells the two apart. The same judgement, off the
11581157
* same snapshot, the record-lock `beforeUpdate` hook enforces — and, for a
11591158
* master-detail line, the detail-row lock's: a line answers `true` while a
11601159
* record it belongs to is so held, with `lockSource: 'master'` (which wins
11611160
* when the line is held by a request of its own too). A row counts as
1162-
* readable by the same rule as at {@link getRecordLock}. A repeated id
1161+
* readable only when the read hands back the row whose id IS the asked id;
1162+
* {@link getRecordLock} counts any row its read hands back, so an id the
1163+
* engine matches under another spelling answers `null` here and the lock
1164+
* there. The two doors keep different readability anchors on purpose:
1165+
* aligning them would narrow a published answer (seat ruling on PR #37191).
1166+
* A repeated id
11631167
* is answered at each of its positions. A caller refused the object's read
11641168
* outright is refused the same way for every id.
11651169
*

0 commit comments

Comments
 (0)