mirror of
https://github.com/IliesAllali/parenthese.git
synced 2026-11-16 16:33:28 +00:00
API : réordonner une fratrie en une transaction
PUT /trees/:id/links/order réécrit le displayOrder des liens parent-enfant d'une fratrie, dans l'ordre reçu. Administrateurs seulement ; chaque lien doit appartenir à l'arbre et à son enfant, et les enfants partager au moins un parent. Journalisé (children_reordered). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8574dfed9e
commit
1caa090efd
3 files changed
+276
No files matched your search
@@ -169,6 +169,19 @@ const linkParamsSchema = z.object({
|
||||
linkId: z.string().min(1),
|
||||
})
|
||||
|
||||
// Ordre d'une fratrie : pour chaque enfant, dans l'ordre voulu, les liens qui le rattachent à ses parents
|
||||
const childrenOrderSchema = z.object({
|
||||
order: z
|
||||
.array(
|
||||
z.object({
|
||||
childId: z.string().min(1),
|
||||
linkIds: z.array(z.string().min(1)).min(1).max(4),
|
||||
}),
|
||||
)
|
||||
.min(2)
|
||||
.max(200),
|
||||
})
|
||||
|
||||
function requireUser(request: FastifyRequest, reply: FastifyReply): { userId: string; email: string } | null {
|
||||
if (!request.actor || request.actor.kind !== 'user') {
|
||||
reply.code(401).send({ error: 'authentication_required' })
|
||||
@@ -1979,6 +1992,85 @@ export const treeRoutes: FastifyPluginAsync = async (app) => {
|
||||
return reply.send({ link })
|
||||
})
|
||||
|
||||
// Réordonner une fratrie : displayOrder des liens parent-enfant réécrit en une transaction
|
||||
app.put('/trees/:id/links/order', async (request, reply) => {
|
||||
const user = requireUser(request, reply)
|
||||
if (!user) {
|
||||
return
|
||||
}
|
||||
|
||||
const params = treeIdParamSchema.safeParse(request.params)
|
||||
if (!params.success) {
|
||||
return reply.code(400).send({ error: 'invalid_params' })
|
||||
}
|
||||
|
||||
const payload = childrenOrderSchema.safeParse(request.body)
|
||||
if (!payload.success) {
|
||||
return reply.code(400).send({ error: 'invalid_payload', details: payload.error.flatten() })
|
||||
}
|
||||
|
||||
const membership = await getAdminMembership(app, user.userId, params.data.id)
|
||||
if (!hasActiveAdminMembership(membership)) {
|
||||
return reply.code(403).send({ error: 'forbidden' })
|
||||
}
|
||||
|
||||
const childIds = payload.data.order.map((entry) => entry.childId)
|
||||
const linkIds = payload.data.order.flatMap((entry) => entry.linkIds)
|
||||
if (new Set(childIds).size !== childIds.length || new Set(linkIds).size !== linkIds.length) {
|
||||
return reply.code(400).send({ error: 'invalid_payload' })
|
||||
}
|
||||
|
||||
const links = await app.prisma.parentChildLink.findMany({
|
||||
where: { id: { in: linkIds }, treeId: params.data.id, deletedAt: null },
|
||||
select: { id: true, parentPersonId: true, childPersonId: true },
|
||||
})
|
||||
const linkById = new Map(links.map((link) => [link.id, link]))
|
||||
if (links.length !== linkIds.length) {
|
||||
return reply.code(404).send({ error: 'link_not_found' })
|
||||
}
|
||||
|
||||
// Chaque lien appartient bien à son enfant, et les enfants partagent au moins un parent
|
||||
const parentSets: Set<string>[] = []
|
||||
for (const entry of payload.data.order) {
|
||||
const parents = new Set<string>()
|
||||
for (const linkId of entry.linkIds) {
|
||||
const link = linkById.get(linkId)
|
||||
if (!link || link.childPersonId !== entry.childId) {
|
||||
return reply.code(400).send({ error: 'invalid_link_reference' })
|
||||
}
|
||||
parents.add(link.parentPersonId)
|
||||
}
|
||||
parentSets.push(parents)
|
||||
}
|
||||
const hasSharedParent = [...parentSets[0]].some((parentId) => parentSets.every((parents) => parents.has(parentId)))
|
||||
if (!hasSharedParent) {
|
||||
return reply.code(400).send({ error: 'not_siblings' })
|
||||
}
|
||||
|
||||
await app.prisma.$transaction(
|
||||
payload.data.order.map((entry, index) =>
|
||||
app.prisma.parentChildLink.updateMany({
|
||||
where: { id: { in: entry.linkIds }, treeId: params.data.id, deletedAt: null },
|
||||
data: { displayOrder: index + 1 },
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
await app.prisma.auditLog.create({
|
||||
data: {
|
||||
treeId: params.data.id,
|
||||
actorType: 'user',
|
||||
actorId: user.userId,
|
||||
action: 'children_reordered',
|
||||
entityType: 'parent_child_link',
|
||||
entityId: payload.data.order[0].linkIds[0],
|
||||
payloadJson: { childIds },
|
||||
},
|
||||
})
|
||||
|
||||
return reply.send({ ok: true })
|
||||
})
|
||||
|
||||
app.delete('/trees/:id/links/:linkId', async (request, reply) => {
|
||||
const user = requireUser(request, reply)
|
||||
if (!user) {
|
||||
|
||||
Reference in new issue
Block a user