API : un nouvel enfant se place après ses frères et soeurs

Sans ordre fourni, un lien parent-enfant créé (route, contribution, import
GEDCOM) prend l'ordre suivant celui de la fratrie, pour qu'un enfant ajouté
après un réordonnancement ne passe pas devant les autres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
IliesAllaliandClaude Opus 5.5 committed 2026-10-16 18:53:23 +02:00
1 parent 1caa090efd
commit 588ac0c4e9
5 files changed
+45 -2

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
import type { Prisma, PrismaClient } from '@prisma/client'
type LinkReader = Pick<PrismaClient, 'parentChildLink'> | Pick<Prisma.TransactionClient, 'parentChildLink'>
// Ordre d'un nouvel enfant : après ses frères et sœurs, pour qu'une fratrie déjà réordonnée
// ne voie pas le dernier arrivé passer devant (le placement trie les enfants sur displayOrder).
export async function nextSiblingOrder(db: LinkReader, treeId: string, parentPersonId: string): Promise<number> {
const last = await db.parentChildLink.findFirst({
where: { treeId, parentPersonId, deletedAt: null },
orderBy: { displayOrder: 'desc' },
select: { displayOrder: true },
})
return Number.isInteger(last?.displayOrder) ? (last!.displayOrder as number) + 1 : 1
}
+2 -1
View File
@@ -11,6 +11,7 @@ import { syncPersonMediaOrder } from '../lib/media-order.js'
import type { Actor, MembershipRole } from '../types/auth.js'
import { findParentChildValidationError, findUnionValidationError } from '../utils/relationship-guards.js'
import { nextSiblingOrder } from '../lib/sibling-order.js'
const isoDateRegex = /^\d{4}-\d{2}-\d{2}$/
@@ -632,7 +633,7 @@ async function applyContributionChange(
childPersonId: parsed.data.childPersonId,
viaUnionId: parsed.data.viaUnionId ?? null,
parentageType: parsed.data.parentageType ?? 'biologique',
displayOrder: parsed.data.displayOrder ?? 1,
displayOrder: parsed.data.displayOrder ?? await nextSiblingOrder(tx, treeId, parsed.data.parentPersonId),
},
select: {
id: true,
+2
View File
@@ -4,6 +4,7 @@ import { z } from 'zod'
import { UPLOAD_BODY_LIMIT } from '../config/env.js'
import { indexObjeRecords, photoPathForPerson } from '../lib/gedcom-photos.js'
import type { MembershipRole } from '../types/auth.js'
import { nextSiblingOrder } from '../lib/sibling-order.js'
const treeIdParamSchema = z.object({
id: z.string().min(1),
@@ -621,6 +622,7 @@ export const gedcomRoutes: FastifyPluginAsync = async (app) => {
parentPersonId: parentId,
childPersonId: childId,
viaUnionId: unionId,
displayOrder: await nextSiblingOrder(tx, treeId, parentId),
},
})
linkKeys.add(linkKey)
+2 -1
View File
@@ -11,6 +11,7 @@ import { removeStoredFile } from '../lib/media-files.js'
import { matchSharePassword } from '../lib/password-variants.js'
import { decryptSharePassword, encryptSharePassword } from '../lib/share-password.js'
import { purgeTrees } from '../lib/tree-purge.js'
import { nextSiblingOrder } from '../lib/sibling-order.js'
import { reserveTreeUnlockAttempt, serializePasswordCheck } from '../lib/unlock-guard.js'
import type { MembershipRole } from '../types/auth.js'
import { findParentChildValidationError, findUnionValidationError } from '../utils/relationship-guards.js'
@@ -1817,7 +1818,7 @@ export const treeRoutes: FastifyPluginAsync = async (app) => {
childPersonId: payload.data.childPersonId,
viaUnionId: payload.data.viaUnionId ?? null,
parentageType: payload.data.parentageType ?? 'biologique',
displayOrder: payload.data.displayOrder ?? 1,
displayOrder: payload.data.displayOrder ?? await nextSiblingOrder(app.prisma, params.data.id, payload.data.parentPersonId),
},
})
+25
View File
@@ -0,0 +1,25 @@
import { describe, expect, it, vi } from 'vitest'
import { nextSiblingOrder } from '../src/lib/sibling-order.js'
function dbReturning(last: { displayOrder: number } | null) {
const findFirst = vi.fn().mockResolvedValue(last)
return { db: { parentChildLink: { findFirst } } as any, findFirst }
}
describe('nextSiblingOrder', () => {
it('places a new child after the last sibling of the parent', async () => {
const { db, findFirst } = dbReturning({ displayOrder: 3 })
await expect(nextSiblingOrder(db, 'tree-1', 'parent-1')).resolves.toBe(4)
expect(findFirst).toHaveBeenCalledWith({
where: { treeId: 'tree-1', parentPersonId: 'parent-1', deletedAt: null },
orderBy: { displayOrder: 'desc' },
select: { displayOrder: true },
})
})
it('starts at 1 for a first child', async () => {
const { db } = dbReturning(null)
await expect(nextSiblingOrder(db, 'tree-1', 'parent-1')).resolves.toBe(1)
})
})