feat(arbre): rétablir la disposition automatique

Route admin POST /trees/:id/layout/reset : remet layoutX/layoutY à null
pour toutes les personnes de l'arbre, dans une transaction, avec une
entrée de journal (tree_layout_reset). Documentée dans l'OpenAPI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
IliesAllaliandClaude Opus 5.5 committed 2026-10-16 18:09:11 +02:00
1 parent 420bda5e38
commit d17cee20df
3 files changed
+128

No files matched your search

+35
View File
@@ -1373,6 +1373,41 @@ paths:
schema:
$ref: '#/components/schemas/ErrorResponse'
/trees/{id}/layout/reset:
post:
tags: [Trees]
summary: Reset manual person positions so the tree is laid out automatically again (admin)
security:
- BearerAuth: []
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
'200':
description: Positions cleared
content:
application/json:
schema:
type: object
properties:
personsReset:
type: integer
'401':
description: Authentication required
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'403':
description: Forbidden (admin only)
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/trees/{id}/unions:
post:
tags: [Trees]
+49
View File
@@ -1410,6 +1410,55 @@ export const treeRoutes: FastifyPluginAsync = async (app) => {
return reply.send({ person })
})
// Rétablit la disposition automatique : efface la place manuelle de toutes les personnes de l'arbre
app.post('/trees/:id/layout/reset', 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_tree_id' })
}
const membership = await getAdminMembership(app, user.userId, params.data.id)
if (!hasActiveAdminMembership(membership)) {
return reply.code(403).send({ error: 'forbidden' })
}
const count = await app.prisma.$transaction(async (tx) => {
const result = await tx.person.updateMany({
where: {
treeId: params.data.id,
deletedAt: null,
OR: [{ layoutX: { not: null } }, { layoutY: { not: null } }],
},
data: {
layoutX: null,
layoutY: null,
updatedBy: user.userId,
},
})
await tx.auditLog.create({
data: {
treeId: params.data.id,
actorType: 'user',
actorId: user.userId,
action: 'tree_layout_reset',
entityType: 'tree',
entityId: params.data.id,
payloadJson: { personsReset: result.count },
},
})
return result.count
})
return reply.send({ personsReset: count })
})
app.delete('/trees/:id/persons/:personId', async (request, reply) => {
const user = requireUser(request, reply)
if (!user) {
@@ -579,6 +579,50 @@ describe('API security and authz', () => {
await app.close()
})
it('lets the owner put every person back to the automatic layout, with an audit entry', async () => {
const app = createApp()
const token = await createUserToken(app)
prismaMock.person.updateMany.mockResolvedValueOnce({ count: 4 })
const response = await app.inject({
method: 'POST',
url: '/trees/tree-1/layout/reset',
headers: { authorization: `Bearer ${token}` },
})
expect(response.statusCode).toBe(200)
expect(response.json()).toEqual({ personsReset: 4 })
expect(prismaMock.person.updateMany).toHaveBeenCalledWith(expect.objectContaining({
where: expect.objectContaining({ treeId: 'tree-1', deletedAt: null }),
data: expect.objectContaining({ layoutX: null, layoutY: null }),
}))
expect(prismaMock.auditLog.create).toHaveBeenCalledWith({
data: expect.objectContaining({ treeId: 'tree-1', action: 'tree_layout_reset', payloadJson: { personsReset: 4 } }),
})
await app.close()
})
it('forbids a member from resetting the automatic layout', async () => {
const app = createApp()
const token = await createUserToken(app)
prismaMock.treeMembership.findUnique.mockResolvedValue({
treeId: 'tree-1',
userId: 'user-1',
role: 'member',
tree: { deletedAt: null, rootPersonId: null },
})
const response = await app.inject({
method: 'POST',
url: '/trees/tree-1/layout/reset',
headers: { authorization: `Bearer ${token}` },
})
expect(response.statusCode).toBe(403)
expect(prismaMock.person.updateMany).not.toHaveBeenCalled()
await app.close()
})
it('allows contributor access token to submit a pending contribution session', async () => {
const app = createApp()
await app.ready()