Skip to content

Commit 0b9008b

Browse files
committed
feat(data-objectstack): carry a write answer's notice on the write-warning event
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibwGdCkvhAHHVnAyVG1
1 parent 6285203 commit 0b9008b

2 files changed

Lines changed: 88 additions & 4 deletions

File tree

‎ui/packages/data-objectstack/src/index.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,13 +2335,21 @@ function notifyWithheldMentions(
23352335
* present only when at least one entry parsed: an answer without the key —
23362336
* including one from a runtime that predates it — leaves the event exactly as
23372337
* it was.
2338+
*
2339+
* `notice` is the same answer's `notice` (objectstack-ai/objectstack#28000):
2340+
* the one message a hook on the write set for the person who made it, info
2341+
* severity, the server's text as written. It rides this event like `warnings`
2342+
* does, so the record save surface that claims its write's answer feedback
2343+
* shows it on that write's one toast (objectstack-ai/objectstack#28252). It
2344+
* is present only when the answer carried a non-empty string.
23382345
*/
23392346
export interface WriteWarningEvent {
23402347
operation: 'create' | 'update';
23412348
resource: string;
23422349
id?: string | number;
23432350
droppedFields: DroppedFieldsNotice[];
23442351
warnings?: ValidateDataIssue[];
2352+
notice?: string;
23452353
}
23462354

23472355
/** Event listener type for write-warning (dropped-fields) events. */
@@ -2810,6 +2818,17 @@ function readWriteWarnings(result: unknown): ValidateDataIssue[] {
28102818
return out;
28112819
}
28122820

2821+
/**
2822+
* Read a single-record write answer's `notice` — the message a hook set for
2823+
* the requester (objectstack-ai/objectstack#28000). The spec declares it a
2824+
* non-empty string; anything else, the key's absence included, answers
2825+
* `undefined`, so an answer from a runtime that predates it adds nothing.
2826+
*/
2827+
function readWriteNotice(result: unknown): string | undefined {
2828+
const raw = (result as { notice?: unknown } | null | undefined)?.notice;
2829+
return typeof raw === 'string' && raw !== '' ? raw : undefined;
2830+
}
2831+
28132832
/**
28142833
* Drop the fields whose strip changed nothing — the caller supplied exactly
28152834
* the value the record already holds (objectui#3484).
@@ -4092,9 +4111,11 @@ export class ObjectStackAdapter<T = unknown> implements DataSource<T> {
40924111
* `object` healed from `resource` — never asserted into the union, and the
40934112
* entry itself never dropped for them (objectui#4934, `f75810e7c`).
40944113
*
4095-
* The same answer's `warnings` (objectui#12107) rides this one event: it is
4096-
* emitted when either list is non-empty, so a write whose only news is an
4097-
* advisory rule hit still reaches the subscriber, with `droppedFields: []`.
4114+
* The same answer's `warnings` (objectui#12107) and `notice`
4115+
* (objectstack-ai/objectstack#28000) ride this one event: it is emitted when
4116+
* any of the three is present, so a write whose only news is an advisory rule
4117+
* hit or a hook's notice still reaches the subscriber, with
4118+
* `droppedFields: []`.
40984119
*/
40994120
private notifyDroppedFields(
41004121
operation: 'create' | 'update',
@@ -4114,13 +4135,15 @@ export class ObjectStackAdapter<T = unknown> implements DataSource<T> {
41144135
droppedFields = withoutNoOpDrops(valid, sent, stored);
41154136
}
41164137
const warnings = readWriteWarnings(result);
4117-
if (droppedFields.length === 0 && warnings.length === 0) return;
4138+
const notice = readWriteNotice(result);
4139+
if (droppedFields.length === 0 && warnings.length === 0 && notice === undefined) return;
41184140
this.emitWriteWarning({
41194141
operation,
41204142
resource,
41214143
...(id !== undefined ? { id } : {}),
41224144
droppedFields,
41234145
...(warnings.length > 0 ? { warnings } : {}),
4146+
...(notice !== undefined ? { notice } : {}),
41244147
});
41254148
}
41264149

‎ui/packages/data-objectstack/src/onWriteWarning.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,64 @@ describe('ObjectStackAdapter.onWriteWarning — write answer warnings (objectui#
372372
]);
373373
});
374374
});
375+
376+
// ── a write answer's `notice` (objectstack-ai/objectstack#28252) ───────────
377+
//
378+
// The spec declares `notice` (one non-empty string a hook set for the
379+
// requester, objectstack-ai/objectstack#28000) on the single-record
380+
// create/update answers. It rides THIS event the way `warnings` does, and
381+
// what the write resolves stays the record.
382+
383+
describe('ObjectStackAdapter.onWriteWarning — write answer notice (objectstack-ai/objectstack#28252)', () => {
384+
it('emits a create write-warning carrying the notice, and the create still resolves the record', async () => {
385+
const create = vi.fn().mockResolvedValue({ record: { id: 'r1', stage: 'won' }, notice: 'Sent for approval' });
386+
const ds = makeDS({ create });
387+
const events: WriteWarningEvent[] = [];
388+
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
389+
390+
const rec = await ds.create('deal', { stage: 'won' });
391+
392+
expect(rec).toEqual({ id: 'r1', stage: 'won' });
393+
expect(events).toEqual([
394+
{ operation: 'create', resource: 'deal', id: 'r1', droppedFields: [], notice: 'Sent for approval' },
395+
]);
396+
});
397+
398+
it('emits an update write-warning carrying the notice beside the answer warnings', async () => {
399+
const update = vi
400+
.fn()
401+
.mockResolvedValue({ record: { id: 'r1' }, warnings: ONE_WARNING, notice: 'Sent for approval' });
402+
const ds = makeDS({ update });
403+
const events: WriteWarningEvent[] = [];
404+
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
405+
406+
const rec = await ds.update('deal', 'r1', { stage: 'won' });
407+
408+
expect(rec).toEqual({ id: 'r1' });
409+
expect(events).toEqual([
410+
{
411+
operation: 'update',
412+
resource: 'deal',
413+
id: 'r1',
414+
droppedFields: [],
415+
warnings: ONE_WARNING,
416+
notice: 'Sent for approval',
417+
},
418+
]);
419+
});
420+
421+
it('CONTROL: an empty or non-string notice emits nothing', async () => {
422+
const create = vi
423+
.fn()
424+
.mockResolvedValueOnce({ record: { id: 'r1' }, notice: '' })
425+
.mockResolvedValueOnce({ record: { id: 'r2' }, notice: 42 });
426+
const ds = makeDS({ create });
427+
const events: WriteWarningEvent[] = [];
428+
ds.onWriteWarning((e: WriteWarningEvent) => events.push(e));
429+
430+
await ds.create('deal', {});
431+
await ds.create('deal', {});
432+
433+
expect(events).toEqual([]);
434+
});
435+
});

0 commit comments

Comments
 (0)