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]); },