Merge branch 'dev'

This commit is contained in:
seaznCode 2026-01-31 17:36:28 +01:00
commit 2c850b84eb
3 changed files with 20 additions and 8 deletions

View File

@ -2,11 +2,13 @@ const MatrixService = require('../../services/matrix/MatrixService');
async function create(req, res) { async function create(req, res) {
try { 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({ const result = await MatrixService.create({
name, name,
topNodeEmail: email, topNodeEmail: email,
force: String(force || '').toLowerCase() === 'true', force: String(force || '').toLowerCase() === 'true',
depth: parsedDepth,
actorUser: req.user actorUser: req.user
}); });
return res.json({ success: true, data: result }); return res.json({ success: true, data: result });

View File

@ -57,7 +57,7 @@ async function ensureUserExistsByEmail(conn, email) {
return rows[0]; return rows[0];
} }
async function createMatrix({ name, topNodeEmail }) { async function createMatrix({ name, topNodeEmail, maxDepth }) {
const conn = await pool.getConnection(); const conn = await pool.getConnection();
try { try {
await conn.beginTransaction(); await conn.beginTransaction();
@ -74,8 +74,8 @@ async function createMatrix({ name, topNodeEmail }) {
const [instRes] = await conn.query( const [instRes] = await conn.query(
`INSERT INTO matrix_instances `INSERT INTO matrix_instances
(root_user_id, name, is_active, max_depth, ego_activated_at, immediate_children_count, first_free_position) (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 VALUES (?, ?, TRUE, ?, NOW(), 0, NULL)`,
[topUser.id, name] [topUser.id, name, (typeof maxDepth === 'number' && Number.isFinite(maxDepth)) ? maxDepth : null]
); );
const matrixInstanceId = instRes.insertId; const matrixInstanceId = instRes.insertId;
@ -84,8 +84,8 @@ async function createMatrix({ name, topNodeEmail }) {
`INSERT INTO user_matrix_metadata `INSERT INTO user_matrix_metadata
(matrix_instance_id, root_user_id, ego_activated_at, last_bfs_fill_at, immediate_children_count, (matrix_instance_id, root_user_id, ego_activated_at, last_bfs_fill_at, immediate_children_count,
first_free_position, name, is_active, max_depth) first_free_position, name, is_active, max_depth)
VALUES (?, ?, NOW(), NULL, 0, 1, ?, TRUE, NULL)`, VALUES (?, ?, NOW(), NULL, 0, 1, ?, TRUE, ?)`,
[matrixInstanceId, topUser.id, name] [matrixInstanceId, topUser.id, name, (typeof maxDepth === 'number' && Number.isFinite(maxDepth)) ? maxDepth : null]
); );
// Self closure scoped to instance // Self closure scoped to instance

View File

@ -19,7 +19,7 @@ function toBool(value, defaultVal = false) {
return defaultVal; return defaultVal;
} }
async function create({ name, topNodeEmail, actorUser }) { // force removed (new instance each time) async function create({ name, topNodeEmail, actorUser, depth }) {
if (!actorUser) { if (!actorUser) {
const err = new Error('Unauthorized'); const err = new Error('Unauthorized');
err.status = 401; err.status = 401;
@ -47,8 +47,18 @@ async function create({ name, topNodeEmail, actorUser }) { // force removed (new
err.status = 400; err.status = 400;
throw err; 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({ return new Matrix({
name: res.name, name: res.name,
masterTopUserId: res.masterTopUserId, masterTopUserId: res.masterTopUserId,