diff --git a/controller/matrix/MatrixController.js b/controller/matrix/MatrixController.js index 1971b22..e5b9f39 100644 --- a/controller/matrix/MatrixController.js +++ b/controller/matrix/MatrixController.js @@ -2,11 +2,13 @@ const MatrixService = require('../../services/matrix/MatrixService'); async function create(req, res) { try { - const { name, email, force } = req.query; // email of the top node account + const { name, email, force, depth } = req.query; // email of the top node account + const parsedDepth = depth !== undefined ? Number(depth) : undefined; const result = await MatrixService.create({ name, topNodeEmail: email, force: String(force || '').toLowerCase() === 'true', + depth: parsedDepth, actorUser: req.user }); return res.json({ success: true, data: result }); diff --git a/repositories/matrix/MatrixRepository.js b/repositories/matrix/MatrixRepository.js index dbfe997..655917c 100644 --- a/repositories/matrix/MatrixRepository.js +++ b/repositories/matrix/MatrixRepository.js @@ -57,7 +57,7 @@ async function ensureUserExistsByEmail(conn, email) { return rows[0]; } -async function createMatrix({ name, topNodeEmail }) { +async function createMatrix({ name, topNodeEmail, maxDepth }) { const conn = await pool.getConnection(); try { await conn.beginTransaction(); @@ -74,8 +74,8 @@ async function createMatrix({ name, topNodeEmail }) { const [instRes] = await conn.query( `INSERT INTO matrix_instances (root_user_id, name, is_active, max_depth, ego_activated_at, immediate_children_count, first_free_position) - VALUES (?, ?, TRUE, NULL, NOW(), 0, NULL)`, // CHANGED: first_free_position NULL at root - [topUser.id, name] + VALUES (?, ?, TRUE, ?, NOW(), 0, NULL)`, + [topUser.id, name, (typeof maxDepth === 'number' && Number.isFinite(maxDepth)) ? maxDepth : null] ); const matrixInstanceId = instRes.insertId; @@ -84,8 +84,8 @@ async function createMatrix({ name, topNodeEmail }) { `INSERT INTO user_matrix_metadata (matrix_instance_id, root_user_id, ego_activated_at, last_bfs_fill_at, immediate_children_count, first_free_position, name, is_active, max_depth) - VALUES (?, ?, NOW(), NULL, 0, 1, ?, TRUE, NULL)`, - [matrixInstanceId, topUser.id, name] + VALUES (?, ?, NOW(), NULL, 0, 1, ?, TRUE, ?)`, + [matrixInstanceId, topUser.id, name, (typeof maxDepth === 'number' && Number.isFinite(maxDepth)) ? maxDepth : null] ); // Self closure scoped to instance diff --git a/services/matrix/MatrixService.js b/services/matrix/MatrixService.js index 106c1e8..295f5c5 100644 --- a/services/matrix/MatrixService.js +++ b/services/matrix/MatrixService.js @@ -19,7 +19,7 @@ function toBool(value, defaultVal = false) { return defaultVal; } -async function create({ name, topNodeEmail, actorUser }) { // force removed (new instance each time) +async function create({ name, topNodeEmail, actorUser, depth }) { if (!actorUser) { const err = new Error('Unauthorized'); err.status = 401; @@ -47,8 +47,18 @@ async function create({ name, topNodeEmail, actorUser }) { // force removed (new err.status = 400; throw err; } + let maxDepth = undefined; + if (depth !== undefined) { + const d = Number(depth); + if (!Number.isFinite(d) || d < 1 || d > 20) { + const err = new Error('Matrix depth must be between 1 and 20'); + err.status = 400; + throw err; + } + maxDepth = d; + } - const res = await MatrixRepository.createMatrix({ name: trimmedName, topNodeEmail: email }); + const res = await MatrixRepository.createMatrix({ name: trimmedName, topNodeEmail: email, maxDepth }); return new Matrix({ name: res.name, masterTopUserId: res.masterTopUserId,