From d17cee20df177478269c7c2286ba5a773678128e Mon Sep 17 00:00:00 2001 From: IliesAllali Date: Fri, 16 Oct 2026 18:09:11 +0200 Subject: [PATCH] =?UTF-8?q?feat(arbre):=20r=C3=A9tablir=20la=20disposition?= =?UTF-8?q?=20automatique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- backend/openapi/v1.yaml | 35 +++++++++++++ backend/src/routes/trees.ts | 49 +++++++++++++++++++ backend/test/api-security.integration.test.ts | 44 +++++++++++++++++ 3 files changed, 128 insertions(+) diff --git a/backend/openapi/v1.yaml b/backend/openapi/v1.yaml index 6eb7257..0de1110 100644 --- a/backend/openapi/v1.yaml +++ b/backend/openapi/v1.yaml @@ -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] diff --git a/backend/src/routes/trees.ts b/backend/src/routes/trees.ts index 9baba17..c80c36a 100644 --- a/backend/src/routes/trees.ts +++ b/backend/src/routes/trees.ts @@ -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) { diff --git a/backend/test/api-security.integration.test.ts b/backend/test/api-security.integration.test.ts index 63cdb9b..d13606b 100644 --- a/backend/test/api-security.integration.test.ts +++ b/backend/test/api-security.integration.test.ts @@ -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()