@asteby/metacore-runtime-react 45.2.0 → 45.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/action-modal-dispatcher.d.ts +29 -0
- package/dist/action-modal-dispatcher.d.ts.map +1 -1
- package/dist/action-modal-dispatcher.js +115 -6
- package/dist/activity-diff.d.ts +2 -0
- package/dist/activity-diff.d.ts.map +1 -1
- package/dist/activity-diff.js +2 -2
- package/dist/business/catalog.d.ts.map +1 -1
- package/dist/business/catalog.js +8 -0
- package/dist/business/index.d.ts +2 -0
- package/dist/business/index.d.ts.map +1 -1
- package/dist/business/index.js +2 -0
- package/dist/business/refund-destination.js +1 -1
- package/dist/business/refund.d.ts +1 -1
- package/dist/business/refund.d.ts.map +1 -1
- package/dist/business/refund.js +1 -0
- package/dist/business/return-wizard.d.ts +23 -0
- package/dist/business/return-wizard.d.ts.map +1 -0
- package/dist/business/return-wizard.js +59 -0
- package/dist/business/return.d.ts +90 -0
- package/dist/business/return.d.ts.map +1 -0
- package/dist/business/return.js +112 -0
- package/dist/dynamic-columns.d.ts.map +1 -1
- package/dist/dynamic-columns.js +3 -1
- package/dist/dynamic-kanban.d.ts +62 -4
- package/dist/dynamic-kanban.d.ts.map +1 -1
- package/dist/dynamic-kanban.js +158 -27
- package/dist/dynamic-relation.d.ts +4 -0
- package/dist/dynamic-relation.d.ts.map +1 -1
- package/dist/dynamic-relation.js +10 -1
- package/dist/dynamic-row-actions.d.ts.map +1 -1
- package/dist/dynamic-row-actions.js +14 -2
- package/dist/dynamic-table.d.ts.map +1 -1
- package/dist/dynamic-table.js +28 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/reason-prompt.d.ts +59 -0
- package/dist/reason-prompt.d.ts.map +1 -0
- package/dist/reason-prompt.js +111 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-addon-settings.d.ts +17 -3
- package/dist/use-addon-settings.d.ts.map +1 -1
- package/dist/use-addon-settings.js +20 -8
- package/package.json +1 -1
- package/src/__tests__/dynamic-kanban.test.tsx +141 -0
- package/src/__tests__/prefill-from-record.test.ts +69 -0
- package/src/__tests__/reason-prompt.test.tsx +121 -0
- package/src/__tests__/use-addon-settings.test.ts +16 -1
- package/src/action-modal-dispatcher.tsx +145 -3
- package/src/activity-diff.tsx +10 -1
- package/src/business/business.test.ts +46 -0
- package/src/business/catalog.ts +8 -0
- package/src/business/index.ts +2 -0
- package/src/business/refund-destination.tsx +2 -2
- package/src/business/refund.ts +2 -1
- package/src/business/return-wizard.tsx +223 -0
- package/src/business/return.ts +181 -0
- package/src/dynamic-columns.tsx +5 -1
- package/src/dynamic-kanban.tsx +197 -19
- package/src/dynamic-relation.tsx +14 -1
- package/src/dynamic-row-actions.tsx +13 -1
- package/src/dynamic-table.tsx +22 -2
- package/src/index.ts +14 -0
- package/src/reason-prompt.tsx +247 -0
- package/src/types.ts +6 -0
- package/src/use-addon-settings.ts +26 -7
|
@@ -38,6 +38,10 @@ import {
|
|
|
38
38
|
isTransitionAllowed,
|
|
39
39
|
applyOptimisticMove,
|
|
40
40
|
selectCardColumns,
|
|
41
|
+
isCardOverdue,
|
|
42
|
+
refLaneSource,
|
|
43
|
+
withRefLanes,
|
|
44
|
+
withGroupBy,
|
|
41
45
|
cardMatchesLaneQuery,
|
|
42
46
|
cardMatchesLaneFunnel,
|
|
43
47
|
laneFunnelCount,
|
|
@@ -226,6 +230,143 @@ describe('selectCardColumns', () => {
|
|
|
226
230
|
})
|
|
227
231
|
})
|
|
228
232
|
|
|
233
|
+
describe('selectCardColumns — explicit card layout (display_config)', () => {
|
|
234
|
+
const col = (key: string, styleConfig?: Record<string, unknown>, extra: object = {}) =>
|
|
235
|
+
({ key, label: key, type: 'text', sortable: false, filterable: false, styleConfig, ...extra }) as any
|
|
236
|
+
const boardMeta = (columns: any[]) => ({ ...meta(), columns }) as any
|
|
237
|
+
|
|
238
|
+
it('shows every flagged field in column order and the flagged title', () => {
|
|
239
|
+
const { title, fields, explicit } = selectCardColumns(
|
|
240
|
+
boardMeta([
|
|
241
|
+
col('stage'),
|
|
242
|
+
col('folio', { card_title: true }),
|
|
243
|
+
col('customer', { card: true }),
|
|
244
|
+
col('plate', { card: true }),
|
|
245
|
+
col('promised_at', { card: true }),
|
|
246
|
+
col('amount', { card: true }),
|
|
247
|
+
col('notes'),
|
|
248
|
+
]),
|
|
249
|
+
)
|
|
250
|
+
expect(explicit).toBe(true)
|
|
251
|
+
expect(title?.key).toBe('folio')
|
|
252
|
+
expect(fields.map((f) => f.key)).toEqual(['customer', 'plate', 'promised_at', 'amount'])
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('keeps the legacy heuristic when no column is flagged', () => {
|
|
256
|
+
expect(selectCardColumns(meta()).explicit).toBe(false)
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
describe('isCardOverdue', () => {
|
|
261
|
+
const now = Date.parse('2026-09-29T12:00:00Z')
|
|
262
|
+
const due = (cfg: Record<string, unknown>) =>
|
|
263
|
+
({ key: 'promised_at', label: 'p', type: 'datetime', styleConfig: cfg }) as any
|
|
264
|
+
|
|
265
|
+
it('flags a past date on an opted-in column', () => {
|
|
266
|
+
expect(isCardOverdue({ promised_at: '2026-09-28T10:00:00Z' }, due({ overdue: true }), now)).toBe(true)
|
|
267
|
+
})
|
|
268
|
+
it('does not flag a future, empty or non-opted-in date', () => {
|
|
269
|
+
expect(isCardOverdue({ promised_at: '2026-09-30T10:00:00Z' }, due({ overdue: true }), now)).toBe(false)
|
|
270
|
+
expect(isCardOverdue({ promised_at: null }, due({ overdue: true }), now)).toBe(false)
|
|
271
|
+
expect(isCardOverdue({ promised_at: '2026-09-28T10:00:00Z' }, due({}), now)).toBe(false)
|
|
272
|
+
})
|
|
273
|
+
it('honours overdue_unless (delivered orders are never late)', () => {
|
|
274
|
+
const cfg = { overdue: true, overdue_unless: { stage: ['delivered'] } }
|
|
275
|
+
expect(isCardOverdue({ promised_at: '2026-09-28T10:00:00Z', stage: 'delivered' }, due(cfg), now)).toBe(false)
|
|
276
|
+
expect(isCardOverdue({ promised_at: '2026-09-28T10:00:00Z', stage: 'in_process' }, due(cfg), now)).toBe(true)
|
|
277
|
+
})
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
describe('withGroupBy (one model, several boards)', () => {
|
|
281
|
+
const boardMeta = (over: object = {}) =>
|
|
282
|
+
({
|
|
283
|
+
columns: [
|
|
284
|
+
{ key: 'stage', label: 'Etapa', type: 'text' },
|
|
285
|
+
{ key: 'assigned_mechanic_id', label: 'Técnico', type: 'text', ref: 'users' },
|
|
286
|
+
],
|
|
287
|
+
// The host projects ONE group_by per model: here another nav entry's.
|
|
288
|
+
group_by: 'assigned_mechanic_id',
|
|
289
|
+
stage_field: 'stage',
|
|
290
|
+
stages: [{ key: 'a', label: 'A' }],
|
|
291
|
+
transitions: [{ from: 'a', to: 'a' }],
|
|
292
|
+
smart_lanes: [{ key: 's', label: 'S', filters: [] }],
|
|
293
|
+
...over,
|
|
294
|
+
}) as any
|
|
295
|
+
|
|
296
|
+
it('is the same object when there is no override or it matches the served group_by', () => {
|
|
297
|
+
const m = boardMeta()
|
|
298
|
+
expect(withGroupBy(m, undefined)).toBe(m)
|
|
299
|
+
expect(withGroupBy(m, 'assigned_mechanic_id')).toBe(m)
|
|
300
|
+
expect(withGroupBy(null, 'stage')).toBeNull()
|
|
301
|
+
})
|
|
302
|
+
it('ignores a column the model does not have', () => {
|
|
303
|
+
const m = boardMeta()
|
|
304
|
+
expect(withGroupBy(m, 'nope')).toBe(m)
|
|
305
|
+
})
|
|
306
|
+
it('keeps the stage machine when grouping by the stage column', () => {
|
|
307
|
+
const out = withGroupBy(boardMeta(), 'stage')!
|
|
308
|
+
expect(out.group_by).toBe('stage')
|
|
309
|
+
expect(out.stages).toHaveLength(1)
|
|
310
|
+
expect(out.transitions).toHaveLength(1)
|
|
311
|
+
expect(out.smart_lanes).toHaveLength(1)
|
|
312
|
+
})
|
|
313
|
+
it('falls back to the served group_by as stage column on hosts without stage_field', () => {
|
|
314
|
+
const out = withGroupBy(boardMeta({ stage_field: undefined, group_by: 'stage' }), 'assigned_mechanic_id')!
|
|
315
|
+
expect(out.stages).toBeUndefined()
|
|
316
|
+
const back = withGroupBy(boardMeta({ stage_field: undefined, group_by: 'assigned_mechanic_id' }), 'stage')
|
|
317
|
+
// no stage_field and the served group_by is not the stage column: cannot tell, machine dropped
|
|
318
|
+
expect(back!.stages).toBeUndefined()
|
|
319
|
+
})
|
|
320
|
+
it('drops the stage machine when grouping by another column', () => {
|
|
321
|
+
const out = withGroupBy(boardMeta({ group_by: 'stage' }), 'assigned_mechanic_id')!
|
|
322
|
+
expect(out.group_by).toBe('assigned_mechanic_id')
|
|
323
|
+
expect(out.stages).toBeUndefined()
|
|
324
|
+
expect(out.transitions).toBeUndefined()
|
|
325
|
+
expect(out.smart_lanes).toBeUndefined()
|
|
326
|
+
expect(refLaneSource(out)).toEqual({ ref: 'users' })
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
describe('reference lanes (group_by on a reference column)', () => {
|
|
331
|
+
const refMeta = (over: object = {}) =>
|
|
332
|
+
({
|
|
333
|
+
columns: [{ key: 'assigned_mechanic_id', label: 'Técnico', type: 'text', ref: 'users' }],
|
|
334
|
+
group_by: 'assigned_mechanic_id',
|
|
335
|
+
...over,
|
|
336
|
+
}) as any
|
|
337
|
+
|
|
338
|
+
it('resolves the lane source from the column ref or search endpoint', () => {
|
|
339
|
+
expect(refLaneSource(refMeta())).toEqual({ ref: 'users' })
|
|
340
|
+
expect(
|
|
341
|
+
refLaneSource(
|
|
342
|
+
refMeta({ columns: [{ key: 'assigned_mechanic_id', label: 't', type: 'text', searchEndpoint: '/options/x?field=id' }] }),
|
|
343
|
+
),
|
|
344
|
+
).toEqual({ endpoint: '/options/x?field=id' })
|
|
345
|
+
})
|
|
346
|
+
it('yields no source when stages or inline options already declare the lanes', () => {
|
|
347
|
+
expect(refLaneSource(refMeta({ stages: [{ key: 'a', label: 'A' }] }))).toBeNull()
|
|
348
|
+
expect(
|
|
349
|
+
refLaneSource(refMeta({ columns: [{ key: 'assigned_mechanic_id', label: 't', type: 'text', ref: 'users', options: [{ value: 'a', label: 'A' }] }] })),
|
|
350
|
+
).toBeNull()
|
|
351
|
+
expect(refLaneSource(null)).toBeNull()
|
|
352
|
+
})
|
|
353
|
+
it('builds one lane per option, alphabetical, with no transitions', () => {
|
|
354
|
+
const out = withRefLanes(refMeta(), [
|
|
355
|
+
{ value: 'u2', label: 'Zoe' },
|
|
356
|
+
{ value: 'u1', label: 'Ana' },
|
|
357
|
+
])
|
|
358
|
+
expect(deriveStages(out).map((s) => [s.key, s.label])).toEqual([
|
|
359
|
+
['u1', 'Ana'],
|
|
360
|
+
['u2', 'Zoe'],
|
|
361
|
+
])
|
|
362
|
+
expect(out.transitions).toBeUndefined()
|
|
363
|
+
})
|
|
364
|
+
it('leaves the metadata alone while the options are still loading', () => {
|
|
365
|
+
const m = refMeta()
|
|
366
|
+
expect(withRefLanes(m, [])).toBe(m)
|
|
367
|
+
})
|
|
368
|
+
})
|
|
369
|
+
|
|
229
370
|
describe('cardMatchesLaneQuery', () => {
|
|
230
371
|
const cols = [
|
|
231
372
|
{ key: 'title', label: 'Title', type: 'text', sortable: false, filterable: false },
|
|
@@ -144,3 +144,72 @@ describe('applyPrefillLock', () => {
|
|
|
144
144
|
expect(applyPrefillLock(field)).toBe(field)
|
|
145
145
|
})
|
|
146
146
|
})
|
|
147
|
+
|
|
148
|
+
// ---- fromField: rows seeded from a document picked in a sibling field --------
|
|
149
|
+
|
|
150
|
+
import { prefillFromFieldRequests, prefillFromFieldRelationRequest } from '../action-modal-dispatcher'
|
|
151
|
+
|
|
152
|
+
const receiptFields = (): ActionFieldDef[] => [
|
|
153
|
+
{ key: 'purchase_order_id', label: 'OC', type: 'dynamic_select', ref: 'PurchaseOrder' } as ActionFieldDef,
|
|
154
|
+
receiveField({
|
|
155
|
+
key: 'items',
|
|
156
|
+
default: {
|
|
157
|
+
$prefillFromRecord: 'items',
|
|
158
|
+
fromField: 'purchase_order_id',
|
|
159
|
+
map: { product_id: 'product_variant_id', ordered: 'qty', received_so_far: 'qty_received' },
|
|
160
|
+
remaining: { target: 'qty_received', of: 'qty', minus: 'qty_received' },
|
|
161
|
+
},
|
|
162
|
+
} as Partial<ActionFieldDef>),
|
|
163
|
+
]
|
|
164
|
+
|
|
165
|
+
describe('prefillFromFieldRequests', () => {
|
|
166
|
+
it('lee el id del campo hermano y el modelo de su ref', () => {
|
|
167
|
+
const reqs = prefillFromFieldRequests(receiptFields(), { purchase_order_id: 'po-1' })
|
|
168
|
+
expect(reqs).toHaveLength(1)
|
|
169
|
+
expect(reqs[0]).toMatchObject({ fieldKey: 'items', refModel: 'PurchaseOrder', refId: 'po-1' })
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('desenvuelve {value,id} y devuelve refId vacío sin selección', () => {
|
|
173
|
+
expect(prefillFromFieldRequests(receiptFields(), { purchase_order_id: { id: 'po-2', label: 'OC-2' } })[0].refId).toBe('po-2')
|
|
174
|
+
expect(prefillFromFieldRequests(receiptFields(), { purchase_order_id: '' })[0].refId).toBe('')
|
|
175
|
+
expect(prefillFromFieldRequests(receiptFields(), {})[0].refId).toBe('')
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it('ignora specs sin fromField o cuyo campo hermano no tiene ref', () => {
|
|
179
|
+
const noFrom = receiveField({ default: { $prefillFromRecord: 'items' } } as Partial<ActionFieldDef>)
|
|
180
|
+
expect(prefillFromFieldRequests([noFrom], {})).toEqual([])
|
|
181
|
+
const fields = receiptFields()
|
|
182
|
+
;(fields[0] as any).ref = undefined
|
|
183
|
+
expect(prefillFromFieldRequests(fields, { purchase_order_id: 'x' })).toEqual([])
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
describe('prefillFromFieldRelationRequest', () => {
|
|
188
|
+
const rels = [
|
|
189
|
+
{ name: 'goods_receipts', kind: 'one_to_many', through: 'GoodsReceipt', foreign_key: 'purchase_order_id' },
|
|
190
|
+
{ name: 'items', kind: 'one_to_many', through: 'PurchaseOrderItem', foreign_key: 'purchase_order_id' },
|
|
191
|
+
]
|
|
192
|
+
it('arma el request de hijos por la relación de la OC elegida', () => {
|
|
193
|
+
const [req] = prefillFromFieldRequests(receiptFields(), { purchase_order_id: 'po-1' })
|
|
194
|
+
expect(prefillFromFieldRelationRequest(req, rels)).toEqual({
|
|
195
|
+
endpoint: '/data/PurchaseOrderItem',
|
|
196
|
+
params: { f_purchase_order_id: 'eq:po-1', per_page: 200 },
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
it('null sin selección o si la relación no existe', () => {
|
|
200
|
+
const [empty] = prefillFromFieldRequests(receiptFields(), {})
|
|
201
|
+
expect(prefillFromFieldRelationRequest(empty, rels)).toBeNull()
|
|
202
|
+
const [req] = prefillFromFieldRequests(receiptFields(), { purchase_order_id: 'po-1' })
|
|
203
|
+
expect(prefillFromFieldRelationRequest(req, [])).toBeNull()
|
|
204
|
+
})
|
|
205
|
+
it('las filas resultantes descartan lo ya recibido por completo (remaining)', () => {
|
|
206
|
+
const [req] = prefillFromFieldRequests(receiptFields(), { purchase_order_id: 'po-1' })
|
|
207
|
+
const rows = buildPrefillRows(req.spec, {
|
|
208
|
+
items: [
|
|
209
|
+
{ product_variant_id: 'a', qty: 10, qty_received: 4 },
|
|
210
|
+
{ product_variant_id: 'b', qty: 5, qty_received: 5 },
|
|
211
|
+
],
|
|
212
|
+
})
|
|
213
|
+
expect(rows).toEqual([{ product_id: 'a', ordered: 10, received_so_far: 4, qty_received: 6 }])
|
|
214
|
+
})
|
|
215
|
+
})
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
//
|
|
3
|
+
// Mandatory reason (PER-4): the request is tried bare; a 422 errors.reason makes
|
|
4
|
+
// the hook ask once and retry with the reason; cancel = nothing ran.
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
6
|
+
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
7
|
+
|
|
8
|
+
vi.mock('react-i18next', () => ({
|
|
9
|
+
useTranslation: () => ({
|
|
10
|
+
t: (k: string, opts?: { defaultValue?: string; min?: number }) =>
|
|
11
|
+
(opts?.defaultValue ?? k).replace('{{min}}', String(opts?.min ?? '')),
|
|
12
|
+
}),
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
import { reasonRequiredInfo, useReasonPrompt } from '../reason-prompt'
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
cleanup()
|
|
19
|
+
vi.clearAllMocks()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const refusal = (code: 'required' | 'min', min?: number) => ({
|
|
23
|
+
response: { status: 422, data: { success: false, errors: { reason: [{ code, params: min ? { min } : undefined }] } } },
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('reasonRequiredInfo', () => {
|
|
27
|
+
it('reads required and min refusals', () => {
|
|
28
|
+
expect(reasonRequiredInfo(refusal('required'))).toEqual({ min: 3, tooShort: false })
|
|
29
|
+
expect(reasonRequiredInfo(refusal('min', 8))).toEqual({ min: 8, tooShort: true })
|
|
30
|
+
})
|
|
31
|
+
it('ignores every other error', () => {
|
|
32
|
+
expect(reasonRequiredInfo(new Error('boom'))).toBeNull()
|
|
33
|
+
expect(reasonRequiredInfo({ response: { data: { errors: { name: [{ code: 'required' }] } } } })).toBeNull()
|
|
34
|
+
expect(reasonRequiredInfo({ response: { data: { errors: { reason: [{ code: 'other' }] } } } })).toBeNull()
|
|
35
|
+
expect(reasonRequiredInfo(undefined)).toBeNull()
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
let result: unknown = 'unset'
|
|
40
|
+
function Probe({ request }: { request: (r?: string) => Promise<string> }) {
|
|
41
|
+
const { run, dialog } = useReasonPrompt()
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<button onClick={async () => { result = await run({ title: 'Eliminar', request }) }}>go</button>
|
|
45
|
+
{dialog}
|
|
46
|
+
</>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('useReasonPrompt', () => {
|
|
51
|
+
it('does not prompt when the server needs no reason', async () => {
|
|
52
|
+
result = 'unset'
|
|
53
|
+
const request = vi.fn().mockResolvedValue('ok')
|
|
54
|
+
render(<Probe request={request} />)
|
|
55
|
+
fireEvent.click(screen.getByText('go'))
|
|
56
|
+
await waitFor(() => expect(result).toBe('ok'))
|
|
57
|
+
expect(request).toHaveBeenCalledTimes(1)
|
|
58
|
+
expect(request).toHaveBeenCalledWith(undefined)
|
|
59
|
+
expect(screen.queryByLabelText('Motivo')).toBeNull()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('asks once on a required refusal and retries with the trimmed reason', async () => {
|
|
63
|
+
result = 'unset'
|
|
64
|
+
const request = vi.fn().mockRejectedValueOnce(refusal('required')).mockResolvedValueOnce('done')
|
|
65
|
+
render(<Probe request={request} />)
|
|
66
|
+
fireEvent.click(screen.getByText('go'))
|
|
67
|
+
const box = await screen.findByLabelText('Motivo')
|
|
68
|
+
const confirm = screen.getByText('Continuar').closest('button') as HTMLButtonElement
|
|
69
|
+
expect(confirm.disabled).toBe(true)
|
|
70
|
+
fireEvent.change(box, { target: { value: ' captura duplicada ' } })
|
|
71
|
+
expect(confirm.disabled).toBe(false)
|
|
72
|
+
fireEvent.click(confirm)
|
|
73
|
+
await waitFor(() => expect(result).toBe('done'))
|
|
74
|
+
expect(request).toHaveBeenNthCalledWith(2, 'captura duplicada')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('stays open with the inline hint when the server wants a longer reason', async () => {
|
|
78
|
+
result = 'unset'
|
|
79
|
+
const request = vi
|
|
80
|
+
.fn()
|
|
81
|
+
.mockRejectedValueOnce(refusal('required'))
|
|
82
|
+
.mockRejectedValueOnce(refusal('min', 10))
|
|
83
|
+
.mockResolvedValueOnce('done')
|
|
84
|
+
render(<Probe request={request} />)
|
|
85
|
+
fireEvent.click(screen.getByText('go'))
|
|
86
|
+
fireEvent.change(await screen.findByLabelText('Motivo'), { target: { value: 'abc' } })
|
|
87
|
+
fireEvent.click(screen.getByText('Continuar'))
|
|
88
|
+
await screen.findByText('Escribe al menos 10 caracteres.')
|
|
89
|
+
fireEvent.change(screen.getByLabelText('Motivo'), { target: { value: 'un motivo bastante largo' } })
|
|
90
|
+
fireEvent.click(screen.getByText('Continuar'))
|
|
91
|
+
await waitFor(() => expect(result).toBe('done'))
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('resolves undefined when the operator cancels', async () => {
|
|
95
|
+
result = 'unset'
|
|
96
|
+
const request = vi.fn().mockRejectedValueOnce(refusal('required'))
|
|
97
|
+
render(<Probe request={request} />)
|
|
98
|
+
fireEvent.click(screen.getByText('go'))
|
|
99
|
+
await screen.findByLabelText('Motivo')
|
|
100
|
+
fireEvent.click(screen.getByText('Cancelar'))
|
|
101
|
+
await waitFor(() => expect(result).toBeUndefined())
|
|
102
|
+
expect(request).toHaveBeenCalledTimes(1)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('rethrows a non-reason error', async () => {
|
|
106
|
+
const request = vi.fn().mockRejectedValue(new Error('network'))
|
|
107
|
+
let caught: unknown
|
|
108
|
+
function P2() {
|
|
109
|
+
const { run, dialog } = useReasonPrompt()
|
|
110
|
+
return (
|
|
111
|
+
<>
|
|
112
|
+
<button onClick={() => run({ title: 'x', request }).catch((e) => { caught = e })}>go</button>
|
|
113
|
+
{dialog}
|
|
114
|
+
</>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
render(<P2 />)
|
|
118
|
+
fireEvent.click(screen.getByText('go'))
|
|
119
|
+
await waitFor(() => expect((caught as Error)?.message).toBe('network'))
|
|
120
|
+
})
|
|
121
|
+
})
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// itself is a thin react-query wrapper over `useApi`; the load-bearing logic
|
|
5
5
|
// is this pure merge, so that's what we test directly.
|
|
6
6
|
import { describe, it, expect } from 'vitest'
|
|
7
|
-
import { mergeAddonSettings, addonSettingsKey } from '../use-addon-settings'
|
|
7
|
+
import { mergeAddonSettings, addonSettingsKey, addonSettingsUrl } from '../use-addon-settings'
|
|
8
8
|
|
|
9
9
|
interface PosSettings extends Record<string, unknown> {
|
|
10
10
|
allowNegativeStock: boolean
|
|
@@ -72,3 +72,18 @@ describe('addonSettingsKey', () => {
|
|
|
72
72
|
expect(addonSettingsKey('inventory')).toEqual(['addon-settings', 'inventory'])
|
|
73
73
|
})
|
|
74
74
|
})
|
|
75
|
+
|
|
76
|
+
describe('branch-scoped settings (POS-1)', () => {
|
|
77
|
+
it('keys and urls carry the branch only when given', () => {
|
|
78
|
+
expect(addonSettingsKey('pos', 'b1')).toEqual(['addon-settings', 'pos', 'b1'])
|
|
79
|
+
expect(addonSettingsKey('pos', null)).toEqual(['addon-settings', 'pos'])
|
|
80
|
+
expect(addonSettingsUrl('pos')).toBe('/api/addons/pos/settings')
|
|
81
|
+
expect(addonSettingsUrl('pos', 'b 1')).toBe('/api/addons/pos/settings?branch_id=b%201')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('an org-key prefix matches every branch read (invalidation)', () => {
|
|
85
|
+
const org = addonSettingsKey('pos')
|
|
86
|
+
const branch = addonSettingsKey('pos', 'b1')
|
|
87
|
+
expect(branch.slice(0, org.length)).toEqual([...org])
|
|
88
|
+
})
|
|
89
|
+
})
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
//
|
|
15
15
|
// The host injects its axios-like client via <ApiProvider>; we no longer
|
|
16
16
|
// depend on a bundler alias to `@/lib/api`.
|
|
17
|
-
import { useState, useEffect, useMemo, useSyncExternalStore } from 'react'
|
|
17
|
+
import { useState, useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
|
|
18
18
|
import { useTranslation } from 'react-i18next'
|
|
19
19
|
import {
|
|
20
20
|
Dialog,
|
|
@@ -57,6 +57,7 @@ import {
|
|
|
57
57
|
} from './field-validation-ui'
|
|
58
58
|
import { validationCatalog } from './validation-catalog'
|
|
59
59
|
import { useApi } from './api-context'
|
|
60
|
+
import { useReasonPrompt } from './reason-prompt'
|
|
60
61
|
import { buildRelationFilterParams } from './dynamic-relation-helpers'
|
|
61
62
|
import { DynamicIcon } from './dynamic-icon'
|
|
62
63
|
import { DynamicLineItems } from './dynamic-line-items'
|
|
@@ -112,6 +113,16 @@ export interface PrefillSpec {
|
|
|
112
113
|
* flow (which carries no prefill spec); only the prefilled action locks them.
|
|
113
114
|
*/
|
|
114
115
|
lock?: string[]
|
|
116
|
+
/**
|
|
117
|
+
* Key of a SIBLING action field (a dynamic_select with a `ref`) whose selected
|
|
118
|
+
* value is the id of the document the rows are seeded from. Without it the
|
|
119
|
+
* rows come from the record the action was opened on (row actions); with it
|
|
120
|
+
* a `create`-placed action — which has no record — seeds its grid as soon as
|
|
121
|
+
* the user picks the source document (e.g. a goods receipt picking its
|
|
122
|
+
* purchase order). `$prefillFromRecord` then names a one_to_many relation of
|
|
123
|
+
* the REFERENCED model. Clearing the selection empties the grid.
|
|
124
|
+
*/
|
|
125
|
+
fromField?: string
|
|
115
126
|
}
|
|
116
127
|
|
|
117
128
|
export function isPrefillSpec(v: unknown): v is PrefillSpec {
|
|
@@ -222,6 +233,64 @@ export function prefillRelationRequests(
|
|
|
222
233
|
return out
|
|
223
234
|
}
|
|
224
235
|
|
|
236
|
+
// ---- prefill lines from a document picked in a sibling field ----------------
|
|
237
|
+
//
|
|
238
|
+
// A create-placed action has no record, so `$prefillFromRecord` alone can only
|
|
239
|
+
// seed an empty grid. With `fromField` the spec follows a sibling ref field:
|
|
240
|
+
// when the user selects the source document the modal loads its relation rows
|
|
241
|
+
// and seeds the grid (see PrefillSpec.fromField).
|
|
242
|
+
|
|
243
|
+
export interface PrefillFromFieldRequest {
|
|
244
|
+
fieldKey: string
|
|
245
|
+
spec: PrefillSpec
|
|
246
|
+
/** Model key the sibling field references (its `ref`). */
|
|
247
|
+
refModel: string
|
|
248
|
+
/** Selected document id; '' when nothing is selected (grid must clear). */
|
|
249
|
+
refId: string
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function prefillFromFieldRequests(
|
|
253
|
+
fields: ActionFieldDef[] | undefined,
|
|
254
|
+
formData: Record<string, any>,
|
|
255
|
+
): PrefillFromFieldRequest[] {
|
|
256
|
+
const out: PrefillFromFieldRequest[] = []
|
|
257
|
+
if (!fields) return out
|
|
258
|
+
for (const field of fields) {
|
|
259
|
+
if (!isLineItemsField(field)) continue
|
|
260
|
+
const spec = lineItemsDefault(field)
|
|
261
|
+
if (!isPrefillSpec(spec) || !spec.fromField) continue
|
|
262
|
+
const source = fields.find((f) => f.key === spec.fromField)
|
|
263
|
+
const refModel = source ? getFieldRef(source) : undefined
|
|
264
|
+
if (!refModel) continue
|
|
265
|
+
const raw = unwrapRecordScalar(formData?.[spec.fromField])
|
|
266
|
+
const refId = raw === undefined || raw === null ? '' : String(raw)
|
|
267
|
+
out.push({ fieldKey: field.key, spec, refModel, refId })
|
|
268
|
+
}
|
|
269
|
+
return out
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// prefillFromFieldRelationRequest resolves the child-rows request for a
|
|
273
|
+
// selected document, from the referenced model's declared relations. Null when
|
|
274
|
+
// the relation is missing or not a plain one_to_many.
|
|
275
|
+
export function prefillFromFieldRelationRequest(
|
|
276
|
+
req: PrefillFromFieldRequest,
|
|
277
|
+
relations: Array<{ name?: string; kind?: string; through?: string; foreign_key?: string; scope?: Record<string, unknown> }>,
|
|
278
|
+
): { endpoint: string; params: Record<string, string | number> } | null {
|
|
279
|
+
if (!req.refId) return null
|
|
280
|
+
const rel = (relations || []).find(
|
|
281
|
+
(r) => r?.name === req.spec.$prefillFromRecord && (r.kind ?? 'one_to_many') === 'one_to_many',
|
|
282
|
+
)
|
|
283
|
+
if (!rel?.through || !rel.foreign_key) return null
|
|
284
|
+
const scope: Record<string, string> = {}
|
|
285
|
+
for (const [k, v] of Object.entries(rel.scope ?? {})) {
|
|
286
|
+
if (v !== undefined && v !== null) scope[k] = String(v)
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
endpoint: `/data/${rel.through}`,
|
|
290
|
+
params: { ...buildRelationFilterParams(rel.foreign_key, req.refId, scope), per_page: 200 },
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
225
294
|
// ---- scalar prefill from the acted-on record --------------------------------
|
|
226
295
|
//
|
|
227
296
|
// Row actions (stamp / refactura / cancel-with-reason) should open with the
|
|
@@ -728,6 +797,9 @@ function ConfirmActionDialog({ open, onOpenChange, action, model, record, endpoi
|
|
|
728
797
|
const { t, i18n } = useTranslation()
|
|
729
798
|
const api = useApi()
|
|
730
799
|
const [executing, setExecuting] = useState(false)
|
|
800
|
+
// Actions listed in the model's `reason_required.actions` (cancel, void…)
|
|
801
|
+
// answer 422 errors.reason without a motive; the prompt asks and retries.
|
|
802
|
+
const reasonPrompt = useReasonPrompt()
|
|
731
803
|
// `action.label` is an addon-contributed i18n key; its locale bundle loads
|
|
732
804
|
// asynchronously, so translate at render (defaultValue keeps an already
|
|
733
805
|
// localized label unchanged).
|
|
@@ -737,7 +809,13 @@ function ConfirmActionDialog({ open, onOpenChange, action, model, record, endpoi
|
|
|
737
809
|
setExecuting(true)
|
|
738
810
|
try {
|
|
739
811
|
const url = buildActionUrl(endpoint, model, record.id, action.key)
|
|
740
|
-
const res = await
|
|
812
|
+
const res = await reasonPrompt.run({
|
|
813
|
+
title: label,
|
|
814
|
+
description: t('actions.reason_desc', { defaultValue: 'Esta acción pide el motivo. Queda en la bitácora del documento.' }),
|
|
815
|
+
request: (reason) => api.post(url, reason ? { reason } : {}),
|
|
816
|
+
})
|
|
817
|
+
// Operator cancelled the reason prompt: nothing ran.
|
|
818
|
+
if (!res) return
|
|
741
819
|
if (res.data.success) {
|
|
742
820
|
toastServerSuccess(res.data, { t })
|
|
743
821
|
onOpenChange(false)
|
|
@@ -753,6 +831,8 @@ function ConfirmActionDialog({ open, onOpenChange, action, model, record, endpoi
|
|
|
753
831
|
}
|
|
754
832
|
|
|
755
833
|
return (
|
|
834
|
+
<>
|
|
835
|
+
{reasonPrompt.dialog}
|
|
756
836
|
<AlertDialog open={open} onOpenChange={onOpenChange}>
|
|
757
837
|
<AlertDialogContent>
|
|
758
838
|
<AlertDialogHeader>
|
|
@@ -778,6 +858,7 @@ function ConfirmActionDialog({ open, onOpenChange, action, model, record, endpoi
|
|
|
778
858
|
</AlertDialogFooter>
|
|
779
859
|
</AlertDialogContent>
|
|
780
860
|
</AlertDialog>
|
|
861
|
+
</>
|
|
781
862
|
)
|
|
782
863
|
}
|
|
783
864
|
|
|
@@ -788,6 +869,7 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
788
869
|
// defaultValue keeps an already-localized string unchanged.
|
|
789
870
|
const tl = (s: string) => t(s, { defaultValue: s })
|
|
790
871
|
const api = useApi()
|
|
872
|
+
const reasonPrompt = useReasonPrompt()
|
|
791
873
|
const branchGate = useBranchCreateGate()
|
|
792
874
|
const [formData, setFormData] = useState<Record<string, any>>({})
|
|
793
875
|
const [executing, setExecuting] = useState(false)
|
|
@@ -850,6 +932,60 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
850
932
|
}
|
|
851
933
|
}, [open, action.fields, record, relations, api])
|
|
852
934
|
|
|
935
|
+
// Lines seeded from a document picked in a sibling field (PrefillSpec.fromField).
|
|
936
|
+
// Re-runs only when the picked id changes; each pick loads the referenced
|
|
937
|
+
// model's relations (cached per model for the life of the modal) and then
|
|
938
|
+
// the child rows.
|
|
939
|
+
const fromFieldReqs = useMemo(
|
|
940
|
+
() => prefillFromFieldRequests(action.fields, formData),
|
|
941
|
+
[action.fields, formData],
|
|
942
|
+
)
|
|
943
|
+
const fromFieldSig = fromFieldReqs.map((r) => `${r.fieldKey}=${r.refModel}:${r.refId}`).join('|')
|
|
944
|
+
const refRelationsCache = useRef<Map<string, Promise<any[]>>>(new Map())
|
|
945
|
+
useEffect(() => {
|
|
946
|
+
if (!open || fromFieldReqs.length === 0) return
|
|
947
|
+
let cancelled = false
|
|
948
|
+
for (const req of fromFieldReqs) {
|
|
949
|
+
if (!req.refId) {
|
|
950
|
+
setFormData((prev: Record<string, any>) =>
|
|
951
|
+
Array.isArray(prev[req.fieldKey]) && prev[req.fieldKey].length === 0
|
|
952
|
+
? prev
|
|
953
|
+
: { ...prev, [req.fieldKey]: [] },
|
|
954
|
+
)
|
|
955
|
+
continue
|
|
956
|
+
}
|
|
957
|
+
let relsPromise = refRelationsCache.current.get(req.refModel)
|
|
958
|
+
if (!relsPromise) {
|
|
959
|
+
relsPromise = api
|
|
960
|
+
.get(`/metadata/table/${req.refModel}`)
|
|
961
|
+
.then((res: any) => {
|
|
962
|
+
const rels = res?.data?.relations ?? res?.data?.data?.relations ?? []
|
|
963
|
+
return Array.isArray(rels) ? rels : []
|
|
964
|
+
})
|
|
965
|
+
.catch(() => [] as any[])
|
|
966
|
+
refRelationsCache.current.set(req.refModel, relsPromise)
|
|
967
|
+
}
|
|
968
|
+
relsPromise
|
|
969
|
+
.then((rels) => {
|
|
970
|
+
const call = prefillFromFieldRelationRequest(req, rels)
|
|
971
|
+
if (!call) return
|
|
972
|
+
return api.get(call.endpoint, { params: call.params }).then((res: any) => {
|
|
973
|
+
if (cancelled) return
|
|
974
|
+
const rows = res?.data?.data ?? res?.data ?? []
|
|
975
|
+
if (!Array.isArray(rows)) return
|
|
976
|
+
const built = buildPrefillRows(req.spec, { [req.spec.$prefillFromRecord]: rows })
|
|
977
|
+
setFormData((prev: Record<string, any>) => ({ ...prev, [req.fieldKey]: built }))
|
|
978
|
+
})
|
|
979
|
+
})
|
|
980
|
+
.catch(() => {})
|
|
981
|
+
}
|
|
982
|
+
return () => {
|
|
983
|
+
cancelled = true
|
|
984
|
+
}
|
|
985
|
+
// fromFieldSig captures every dependency of fromFieldReqs that matters.
|
|
986
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
987
|
+
}, [open, fromFieldSig, api])
|
|
988
|
+
|
|
853
989
|
const updateField = (key: string, value: any) => {
|
|
854
990
|
setFormData((prev: Record<string, any>) => ({ ...prev, [key]: value }))
|
|
855
991
|
setFieldErrors((prev) => clearFieldErrorTree(prev, key))
|
|
@@ -914,7 +1050,12 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
914
1050
|
if (branchId) payload = { ...payload, branch_id: branchId }
|
|
915
1051
|
}
|
|
916
1052
|
const url = buildActionUrl(endpoint, model, record?.id, action.key)
|
|
917
|
-
const res = await
|
|
1053
|
+
const res = await reasonPrompt.run({
|
|
1054
|
+
title: tl(action.label),
|
|
1055
|
+
description: t('actions.reason_desc', { defaultValue: 'Esta acción pide el motivo. Queda en la bitácora del documento.' }),
|
|
1056
|
+
request: (reason) => api.post(url, reason ? { ...payload, reason } : payload),
|
|
1057
|
+
})
|
|
1058
|
+
if (!res) return
|
|
918
1059
|
if (res.data.success) {
|
|
919
1060
|
toastServerSuccess(res.data, { t })
|
|
920
1061
|
onOpenChange(false)
|
|
@@ -1022,6 +1163,7 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
1022
1163
|
{tl(action.label)}
|
|
1023
1164
|
</Button>
|
|
1024
1165
|
</DialogFooter>
|
|
1166
|
+
{reasonPrompt.dialog}
|
|
1025
1167
|
</DialogContent>
|
|
1026
1168
|
</Dialog>
|
|
1027
1169
|
)
|
package/src/activity-diff.tsx
CHANGED
|
@@ -50,6 +50,8 @@ export interface ActivityEvent {
|
|
|
50
50
|
*/
|
|
51
51
|
changes?: Record<string, { from: unknown; to: unknown }> | null
|
|
52
52
|
summary?: string | null
|
|
53
|
+
/** Motive the operator stated for a delete / reason_required action (PER-4). */
|
|
54
|
+
reason?: string | null
|
|
53
55
|
occurred_at: string
|
|
54
56
|
}
|
|
55
57
|
|
|
@@ -204,7 +206,7 @@ export const ActivityDiff: React.FC<ActivityDiffProps> = ({
|
|
|
204
206
|
|
|
205
207
|
const variantBadge = VARIANT_BADGE[variant] ?? VARIANT_BADGE.other
|
|
206
208
|
|
|
207
|
-
if (allKeys.length === 0 && !event.summary) {
|
|
209
|
+
if (allKeys.length === 0 && !event.summary && !event.reason) {
|
|
208
210
|
return (
|
|
209
211
|
<div className={cn('text-sm text-muted-foreground italic py-1', className)}>
|
|
210
212
|
Sin campos registrados.
|
|
@@ -235,6 +237,13 @@ export const ActivityDiff: React.FC<ActivityDiffProps> = ({
|
|
|
235
237
|
)}
|
|
236
238
|
</div>
|
|
237
239
|
|
|
240
|
+
{/* Mandatory-reason motive (delete / cancel with reason_required) */}
|
|
241
|
+
{event.reason && (
|
|
242
|
+
<p className="text-sm" data-testid="activity-reason">
|
|
243
|
+
<span className="font-medium">Motivo:</span> {event.reason}
|
|
244
|
+
</p>
|
|
245
|
+
)}
|
|
246
|
+
|
|
238
247
|
{/* Summary line (if backend provided one) */}
|
|
239
248
|
{event.summary && (
|
|
240
249
|
<p className="text-sm text-muted-foreground italic">{event.summary}</p>
|