From ba723aa0b77302a86e0a677d697ccfee82d180dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=D1=81=20=D0=A1=D1=83=D0=BB=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Fri, 4 Sep 2026 20:14:51 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20ctx.users.recalcFieldAverage=20+=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BA=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B0=D0=B2=D1=82=D0=BE=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D1=81=D1=87=D1=91=D1=82=D0=B0=20=D1=81=D1=80=D0=B5=D0=B4=D0=BD?= =?UTF-8?q?=D0=B5=D0=B3=D0=BE=20history-number?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/pages/Automations.tsx | 13 ++++++++++--- server/mcp.ts | 3 ++- server/workers/automation-worker-code.ts | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/client/src/pages/Automations.tsx b/client/src/pages/Automations.tsx index 0674635..76aa095 100644 --- a/client/src/pages/Automations.tsx +++ b/client/src/pages/Automations.tsx @@ -83,8 +83,12 @@ const CODE_TEMPLATE = `// ====================================================== // ctx.users.updateStatus(id, statusId) — сменить статус пользователя // ctx.users.setFieldValue(id, fieldCodeOrId, value) — записать значение кастомного поля профиля // пользователя (по id или code поля, с аудитом от имени «Автоматизация»). Для полей типа -// «history-number» значение дополнительно добавляется в историю user_field_history -// (с привязкой к задаче триггера); нечисловое значение вызовет ошибку автоматизации. +// «history-number» переданное число добавляется в историю user_field_history +// (с привязкой к задаче триггера), а значением поля становится среднее за текущий месяц +// (автопересчёт); нечисловое значение вызовет ошибку автоматизации. +// ctx.users.recalcFieldAverage(id, fieldCodeOrId) — принудительный пересчёт среднего +// за текущий месяц для history-number поля (например, ежедневно по schedule; +// нет записей в месяце — поле очищается). // // Триггеры: // manual — запуск вручную @@ -319,8 +323,11 @@ export function AutomationsContent() { ctx.users.list() / ctx.users.get(userId) ctx.users.update(userId, updates) / ctx.users.updateStatus(userId, statusId) ctx.users.setFieldValue(userId, fieldCodeOrId, value) — записать кастомное поле профиля - (с аудитом; для полей «history-number» значение также пишется в историю user_field_history, + (с аудитом; для полей «history-number» число пишется в историю user_field_history, + а значением поля становится среднее за текущий месяц — автопересчёт; нечисловое значение — ошибка автоматизации) + ctx.users.recalcFieldAverage(userId, fieldCodeOrId) — принудительный пересчёт среднего + за текущий месяц для history-number поля (нет записей в месяце — поле очищается) Логирование: ctx.log(msg) diff --git a/server/mcp.ts b/server/mcp.ts index b5ca252..6eae6ea 100644 --- a/server/mcp.ts +++ b/server/mcp.ts @@ -2423,7 +2423,8 @@ Users: - ctx.users.get(userId) — get one user - ctx.users.update(userId, updates) — update user fields (firstName, lastName, position, statusId, etc.) - ctx.users.updateStatus(userId, statusId) — change user status -- ctx.users.setFieldValue(userId, fieldCodeOrId, value) — set a user profile custom field value by field id or code (with audit as 'Автоматизация'). For fields of type 'history-number' the numeric value is also appended to user_field_history (linked to the trigger task); a non-numeric value throws an error. +- ctx.users.setFieldValue(userId, fieldCodeOrId, value) — set a user profile custom field value by field id or code (with audit as 'Автоматизация'). For fields of type 'history-number' the numeric value is appended to user_field_history (linked to the trigger task) and the field value is set to the current-month average (auto-recalculated); a non-numeric value throws an error. +- ctx.users.recalcFieldAverage(userId, fieldCodeOrId) — recalculate the current-month average of a history-number field from user_field_history (use from schedule automations for monthly reset; clears the field if no entries this month). Trigger types: 'manual', 'task.before_create', 'task.created', 'task.status_changed', 'qr.scan', 'schedule' TriggerConfig for task.before_create / task.created / task.status_changed: { formId: number, statusId?: number } — statusId applies only to task.status_changed and limits firing to transitions INTO that status diff --git a/server/workers/automation-worker-code.ts b/server/workers/automation-worker-code.ts index e04d314..30c9b0d 100644 --- a/server/workers/automation-worker-code.ts +++ b/server/workers/automation-worker-code.ts @@ -68,6 +68,9 @@ const ctx = { : null; return callStorage('setUserProfileFieldValueFromAutomation', [userId, fieldCodeOrId, value, organizationId, taskId]); }, + recalcFieldAverage: function(userId, fieldCodeOrId) { + return callStorage('recalcUserFieldAverageFromAutomation', [userId, fieldCodeOrId, organizationId]); + }, }, tasks: { list: function(formId) { return callStorage('getTasksByForm', [formId, organizationId]); },