From 588ac0c4e98637317d86c56963999cc9b8f8ef52 Mon Sep 17 00:00:00 2001 From: IliesAllali Date: Fri, 16 Oct 2026 18:53:23 +0200 Subject: [PATCH] =?UTF-8?q?API=20:=20un=20nouvel=20enfant=20se=20place=20a?= =?UTF-8?q?pr=C3=A8s=20ses=20fr=C3=A8res=20et=20soeurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- backend/src/lib/sibling-order.ts | 14 ++++++++++++++ backend/src/routes/contributions.ts | 3 ++- backend/src/routes/gedcom.ts | 2 ++ backend/src/routes/trees.ts | 3 ++- backend/test/sibling-order.test.ts | 25 +++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 backend/src/lib/sibling-order.ts create mode 100644 backend/test/sibling-order.test.ts diff --git a/backend/src/lib/sibling-order.ts b/backend/src/lib/sibling-order.ts new file mode 100644 index 0000000..4b5b0fc --- /dev/null +++ b/backend/src/lib/sibling-order.ts @@ -0,0 +1,14 @@ +import type { Prisma, PrismaClient } from '@prisma/client' + +type LinkReader = Pick | Pick + +// 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 { + 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 +} diff --git a/backend/src/routes/contributions.ts b/backend/src/routes/contributions.ts index 4c0f3fa..b5b8fdc 100644 --- a/backend/src/routes/contributions.ts +++ b/backend/src/routes/contributions.ts @@ -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, diff --git a/backend/src/routes/gedcom.ts b/backend/src/routes/gedcom.ts index d5789a4..f655653 100644 --- a/backend/src/routes/gedcom.ts +++ b/backend/src/routes/gedcom.ts @@ -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) diff --git a/backend/src/routes/trees.ts b/backend/src/routes/trees.ts index b80fd47..2be93a0 100644 --- a/backend/src/routes/trees.ts +++ b/backend/src/routes/trees.ts @@ -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), }, }) diff --git a/backend/test/sibling-order.test.ts b/backend/test/sibling-order.test.ts new file mode 100644 index 0000000..d2fae24 --- /dev/null +++ b/backend/test/sibling-order.test.ts @@ -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) + }) +})