@@ -204,13 +204,21 @@ export async function defaultRecordPageSaveRefusal(
204204 verb : 'saved' | 'published' | 'restored' = 'saved' ,
205205) : Promise < ( Error & { code : 'DEFAULT_RECORD_PAGE_CONFLICT' ; status : 422 } ) | undefined > {
206206 if ( type !== 'page' ) return undefined ;
207+ // Every slot the page holds: its object's default (no app), and the record
208+ // page of its object inside each app its `apps` names (ruling A on
209+ // objectstack#28979) — each judged by the one reading, `defaultRecordPageObject`.
210+ const slots : Array < { object : string ; app ?: string } > = [ ] ;
207211 const object = defaultRecordPageObject ( item ) ;
208- if ( object === undefined ) return undefined ;
212+ if ( object !== undefined ) slots . push ( { object } ) ;
213+ const apps = ( item as { apps ?: unknown } | null | undefined ) ?. apps ;
214+ if ( Array . isArray ( apps ) ) {
215+ for ( const app of new Set ( apps . filter ( ( a ) : a is string => typeof a === 'string' ) ) ) {
216+ const scoped = defaultRecordPageObject ( item , app ) ;
217+ if ( scoped !== undefined ) slots . push ( { object : scoped , app } ) ;
218+ }
219+ }
220+ if ( slots . length === 0 ) return undefined ;
209221
210- // `shipped`: a page a package ships through the registry, which no save
211- // can edit; a stored row (bound to a package or not) is editable here.
212- type Holder = { page : string ; packageId ?: string ; shipped : boolean } ;
213- const holders : Holder [ ] = [ ] ;
214222 let records : any [ ] = [ ] ;
215223 try {
216224 // [ADR-0131 D6] A save lands environment-wide, so the pages it meets
@@ -221,52 +229,75 @@ export async function defaultRecordPageSaveRefusal(
221229 }
222230 const stored = this . storedOverlayEntries ( { type } , records ) ;
223231 const storedNames = new Set ( stored . map ( ( entry ) => entry . name ) ) ;
224- for ( const entry of stored ) {
225- if ( entry . name === saveName || defaultRecordPageObject ( entry . data ) !== object ) continue ;
226- holders . push ( entry . packageId
227- ? { page : entry . name , packageId : entry . packageId , shipped : false }
228- : { page : entry . name , shipped : false } ) ;
229- }
230232 const registry = this . engine . registry ;
231- if ( typeof registry ?. defaultRecordPageHoldersOf === 'function' ) {
232- const registered = registry . defaultRecordPageHoldersOf ( object ) as Array <
233- { kind : 'package' ; packageId : string ; page : string } | { kind : 'environment' ; page : string }
234- > ;
235- for ( const holder of registered ) {
236- if ( holder . page === saveName || storedNames . has ( holder . page ) ) continue ;
237- holders . push ( holder . kind === 'package'
238- ? { page : holder . page , packageId : holder . packageId , shipped : true }
239- : { page : holder . page , shipped : false } ) ;
233+
234+ // `shipped`: a page a package ships through the registry, which no save
235+ // can edit; a stored row (bound to a package or not) is editable here.
236+ type Holder = { page : string ; packageId ?: string ; shipped : boolean } ;
237+ for ( const { object, app } of slots ) {
238+ const holders : Holder [ ] = [ ] ;
239+ for ( const entry of stored ) {
240+ if ( entry . name === saveName || defaultRecordPageObject ( entry . data , app ) !== object ) continue ;
241+ holders . push ( entry . packageId
242+ ? { page : entry . name , packageId : entry . packageId , shipped : false }
243+ : { page : entry . name , shipped : false } ) ;
240244 }
245+ if ( typeof registry ?. defaultRecordPageHoldersOf === 'function' ) {
246+ const registered = ( app === undefined
247+ ? registry . defaultRecordPageHoldersOf ( object )
248+ : registry . defaultRecordPageHoldersOf ( object , app ) ) as Array <
249+ { kind : 'package' ; packageId : string ; page : string } | { kind : 'environment' ; page : string }
250+ > ;
251+ for ( const holder of registered ) {
252+ if ( holder . page === saveName || storedNames . has ( holder . page ) ) continue ;
253+ holders . push ( holder . kind === 'package'
254+ ? { page : holder . page , packageId : holder . packageId , shipped : true }
255+ : { page : holder . page , shipped : false } ) ;
256+ }
257+ }
258+ if ( holders . length === 0 ) continue ;
259+
260+ const seen = new Set < string > ( ) ;
261+ const unique = holders . filter ( ( h ) => {
262+ const key = `${ h . packageId ?? '' } |${ h . page } ` ;
263+ if ( seen . has ( key ) ) return false ;
264+ seen . add ( key ) ;
265+ return true ;
266+ } ) ;
267+ const describe = ( h : Holder ) =>
268+ h . packageId ? `page '${ h . page } ' of package '${ h . packageId } '` : `page '${ h . page } ', authored in this system` ;
269+ const named = unique . map ( describe ) ;
270+ const others = named . length === 1 ? `${ named [ 0 ] } already is` : `${ named . join ( ' and ' ) } already are` ;
271+ const message = app === undefined
272+ ? `Default record page conflict: page '${ saveName } ' cannot be ${ verb } as the default record page of object `
273+ + `'${ object } ': ${ others } . `
274+ + ( unique . some ( ( h ) => h . shipped )
275+ ? `Save '${ saveName } ' with \`isDefault: false\`: a page a package ships stays the default unless the `
276+ + `deployment is composed without that package's page.`
277+ : `Save '${ saveName } ' with \`isDefault: false\`, or first save ${ unique . map ( ( h ) => `'${ h . page } '` ) . join ( ' and ' ) } `
278+ + `with \`isDefault: false\` to make '${ saveName } ' the default instead.` )
279+ + ` At most one \`isDefault: true\` record page may exist for an object `
280+ + `in a deployment: the record view opens that page with no tie-break.`
281+ : `Default record page conflict: page '${ saveName } ' cannot be ${ verb } as the record page of object `
282+ + `'${ object } ' in app '${ app } ': ${ others } . `
283+ + ( unique . some ( ( h ) => h . shipped )
284+ ? `Save '${ saveName } ' without '${ app } ' in \`apps\`: a page a package ships stays that app's page unless the `
285+ + `deployment is composed without that package's page.`
286+ : `Save '${ saveName } ' without '${ app } ' in \`apps\`, or first save ${ unique . map ( ( h ) => `'${ h . page } '` ) . join ( ' and ' ) } `
287+ + `without it to make '${ saveName } ' that app's page instead.` )
288+ + ` At most one record page may name an app in \`apps\` for an object: that app's record view `
289+ + `opens it with no tie-break.` ;
290+ const err = new Error ( message ) as Error & {
291+ code : 'DEFAULT_RECORD_PAGE_CONFLICT' ; status : 422 ; object : string ; app ?: string ; holders : readonly Holder [ ] ;
292+ } ;
293+ err . code = 'DEFAULT_RECORD_PAGE_CONFLICT' ;
294+ err . status = 422 ;
295+ err . object = object ;
296+ if ( app !== undefined ) err . app = app ;
297+ err . holders = unique ;
298+ return err ;
241299 }
242- if ( holders . length === 0 ) return undefined ;
243-
244- const seen = new Set < string > ( ) ;
245- const unique = holders . filter ( ( h ) => {
246- const key = `${ h . packageId ?? '' } |${ h . page } ` ;
247- if ( seen . has ( key ) ) return false ;
248- seen . add ( key ) ;
249- return true ;
250- } ) ;
251- const describe = ( h : Holder ) =>
252- h . packageId ? `page '${ h . page } ' of package '${ h . packageId } '` : `page '${ h . page } ', authored in this system` ;
253- const named = unique . map ( describe ) ;
254- const others = named . length === 1 ? `${ named [ 0 ] } already is` : `${ named . join ( ' and ' ) } already are` ;
255- const remedy = unique . some ( ( h ) => h . shipped )
256- ? `Save '${ saveName } ' with \`isDefault: false\`: a page a package ships stays the default unless the `
257- + `deployment is composed without that package's page.`
258- : `Save '${ saveName } ' with \`isDefault: false\`, or first save ${ unique . map ( ( h ) => `'${ h . page } '` ) . join ( ' and ' ) } `
259- + `with \`isDefault: false\` to make '${ saveName } ' the default instead.` ;
260- const err = new Error (
261- `Default record page conflict: page '${ saveName } ' cannot be ${ verb } as the default record page of object `
262- + `'${ object } ': ${ others } . ${ remedy } At most one \`isDefault: true\` record page may exist for an object `
263- + `in a deployment: the record view opens that page with no tie-break.` ,
264- ) as Error & { code : 'DEFAULT_RECORD_PAGE_CONFLICT' ; status : 422 ; object : string ; holders : readonly Holder [ ] } ;
265- err . code = 'DEFAULT_RECORD_PAGE_CONFLICT' ;
266- err . status = 422 ;
267- err . object = object ;
268- err . holders = unique ;
269- return err ;
300+ return undefined ;
270301}
271302
272303/**
0 commit comments