import { askUser } from '../../core/ui'; import { getAppConfig } from '../../core/config'; import { getUserEmailMap } from '../../core/mail/emailConfig'; import { pluralizeTasksWord } from './builder'; export function notifyPlanOverdueExecutors(): void { const confirmed = askUser('Подтвеждаете отправку?'); if (!confirmed) return; const reportSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const data = reportSheet.getDataRange().getValues(); if (data.length < 2) { SpreadsheetApp.getUi().alert('В отчете нет данных'); return; } const headers = data[0]; const col: Record = {}; headers.forEach((header, index) => { col[String(header).trim()] = index; }); if (!('Исполнитель' in col)) { throw new Error('Не найдена колонка "Исполнитель"'); } const executorCounts: Record = {}; for (let i = 1; i < data.length; i++) { const row = data[i]; const firstCell = String(row[0] || '').trim(); if (firstCell === 'Итого' || firstCell === '') { continue; } const executor = String(row[col['Исполнитель']] || '').trim(); if (!executor) { continue; } executorCounts[executor] = (executorCounts[executor] || 0) + 1; } const emailMap = getUserEmailMap(); const notSent: string[] = []; const plannerUrl = getAppConfig().bitrixPortal + '/marketplace/app/124/'; Object.entries(executorCounts).forEach(([executor, count]) => { const email = emailMap[executor]; if (!email) { Logger.log('Не найден email для: ' + executor); notSent.push(executor); return; } MailApp.sendEmail({ to: email, subject: 'Задачи, не завершенные по плану', body: `Добрый день У вас есть ${count} ${pluralizeTasksWord(count)}, которые должны были быть завершены вчера согласно их планируемым срокам. Поменяйте планируемое время выполнения этих задач или завершите их. Ссылка на инструмент планирования в Битриксе: ${plannerUrl} Это автоматическое уведомление.`, }); }); if (notSent.length) { SpreadsheetApp.getUi().alert('Не найден email для: \n\n' + notSent.join('\n')); } }