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) + }) +})