207 lines
5.6 KiB
TypeScript
207 lines
5.6 KiB
TypeScript
import { EXECUTOR_SHEET_NAMES } from '../../core/constants';
|
||
import { askUser } from '../../core/ui';
|
||
import { getUserEmailMap } from '../../core/mail/emailConfig';
|
||
import { loadConfig, saveConfig } from '../../core/mail/executorTables';
|
||
import { sendExecutorNotification } from './executorEmail';
|
||
|
||
const EXECUTOR_SHEET_HEADERS = [
|
||
'Название задачи',
|
||
'Название родительской задачи',
|
||
'ID Р. задачи',
|
||
'ID задачи',
|
||
'Исполнитель',
|
||
'Постановщик',
|
||
'Элемент CRM',
|
||
'Большая или мелкая',
|
||
'Крайний срок Р. задачи',
|
||
'Крайний срок задачи',
|
||
'Дата создания подзадачи',
|
||
'Время выполнения',
|
||
'Ведет к потере клиента',
|
||
'Цена задачи',
|
||
'Требует внимания',
|
||
'Комментарий исполнителя',
|
||
'Комментарий руководителя',
|
||
'Расшифровка кода',
|
||
'Расшифровка кода для столбца Название родительской задачи'
|
||
];
|
||
|
||
export function distributeTasksToExecutors(): void {
|
||
const confirmed = askUser('Подтвеждаете отправку?');
|
||
if (!confirmed) return;
|
||
|
||
const reportSheet = SpreadsheetApp.getActiveSheet();
|
||
|
||
if (!reportSheet) {
|
||
throw new Error('Лист Итог не найден');
|
||
}
|
||
|
||
const data = reportSheet.getDataRange().getValues();
|
||
|
||
if (data.length < 2) {
|
||
return;
|
||
}
|
||
|
||
const headers = data[0];
|
||
const col: Record<string, number> = {};
|
||
|
||
headers.forEach((h, i) => {
|
||
col[String(h)] = i;
|
||
});
|
||
|
||
const rows = data.slice(1);
|
||
const config = loadConfig();
|
||
const usersMap = getUserEmailMap();
|
||
const grouped: Record<string, any[][]> = {};
|
||
|
||
rows.forEach(row => {
|
||
const attention = String(row[col['Требует внимания']] || '');
|
||
|
||
if (!attention.startsWith('Да')) {
|
||
return;
|
||
}
|
||
|
||
const executor = String(row[col['Исполнитель']] || '').trim();
|
||
const taskSetter = String(row[col['Постановщик']] || '').trim();
|
||
|
||
if (!executor) {
|
||
return;
|
||
}
|
||
|
||
if (
|
||
executor != taskSetter &&
|
||
!(attention.toLowerCase().includes('время завершения') ||
|
||
attention.toLowerCase().includes('длительность задачи'))
|
||
) {
|
||
return;
|
||
}
|
||
|
||
if (!grouped[executor]) {
|
||
grouped[executor] = [];
|
||
}
|
||
|
||
grouped[executor].push(row);
|
||
});
|
||
|
||
Object.keys(grouped).forEach(executor => {
|
||
let spreadsheetId = config[executor];
|
||
let spreadsheet: GoogleAppsScript.Spreadsheet.Spreadsheet | null = null;
|
||
|
||
if (spreadsheetId) {
|
||
try {
|
||
spreadsheet = SpreadsheetApp.openById(spreadsheetId);
|
||
} catch (_e) {
|
||
spreadsheet = null;
|
||
}
|
||
}
|
||
|
||
if (!spreadsheet) {
|
||
spreadsheet = SpreadsheetApp.create('Задачи - ' + executor);
|
||
spreadsheetId = spreadsheet.getId();
|
||
config[executor] = spreadsheetId;
|
||
}
|
||
|
||
const sheet = prepareExecutorSpreadsheet(spreadsheet, grouped[executor]);
|
||
sendExecutorNotification({ executor, spreadsheet, usersMap, sheet });
|
||
});
|
||
|
||
saveConfig(config);
|
||
}
|
||
|
||
function prepareExecutorSpreadsheet(
|
||
spreadsheet: GoogleAppsScript.Spreadsheet.Spreadsheet,
|
||
rows: any[][]
|
||
) {
|
||
const SHEET_NAME = EXECUTOR_SHEET_NAMES.bigSmallQuestions;
|
||
let sheet = spreadsheet.getSheetByName(SHEET_NAME);
|
||
|
||
if (!sheet) {
|
||
sheet = spreadsheet.insertSheet(SHEET_NAME);
|
||
}
|
||
|
||
sheet.clearContents();
|
||
|
||
const result = [
|
||
EXECUTOR_SHEET_HEADERS,
|
||
...rows
|
||
];
|
||
|
||
sheet
|
||
.getRange(1, 1, result.length, result[0].length)
|
||
.setValues(result);
|
||
|
||
sheet.autoResizeColumns(1, result[0].length);
|
||
|
||
return sheet;
|
||
}
|
||
|
||
export function loadExecutorComments(): void {
|
||
const SHEET_NAME = 'Вопросы по большим и малым задачам';
|
||
const config = loadConfig();
|
||
const commentsMap: Record<string, any> = {};
|
||
|
||
Object.keys(config).forEach(executor => {
|
||
const spreadsheetId = config[executor];
|
||
|
||
try {
|
||
const ss = SpreadsheetApp.openById(spreadsheetId);
|
||
const sheet = ss.getSheetByName(SHEET_NAME);
|
||
const data = sheet.getDataRange().getValues();
|
||
|
||
if (data.length < 2) {
|
||
return;
|
||
}
|
||
|
||
const headers = data[0];
|
||
const col: Record<string, number> = {};
|
||
|
||
headers.forEach((h, i) => {
|
||
col[String(h)] = i;
|
||
});
|
||
|
||
data.slice(1).forEach(row => {
|
||
const taskId = row[col['ID задачи']];
|
||
const comment = row[col['Комментарий исполнителя']];
|
||
|
||
if (taskId && comment) {
|
||
commentsMap[String(taskId)] = comment;
|
||
}
|
||
});
|
||
} catch (_e) {
|
||
Logger.log('Ошибка чтения таблицы: ' + executor);
|
||
}
|
||
});
|
||
|
||
updateMainReportComments(commentsMap);
|
||
}
|
||
|
||
function updateMainReportComments(commentsMap: Record<string, any>): void {
|
||
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
|
||
const data = sheet.getDataRange().getValues();
|
||
const headers = data[0];
|
||
const col: Record<string, number> = {};
|
||
|
||
headers.forEach((h, i) => {
|
||
col[String(h)] = i;
|
||
});
|
||
|
||
for (let i = 1; i < data.length; i++) {
|
||
const row = data[i];
|
||
const attention = String(row[col['Требует внимания']] || '');
|
||
|
||
if (!attention.startsWith('Да')) {
|
||
continue;
|
||
}
|
||
|
||
const taskId = String(row[col['ID задачи']] || '');
|
||
if (!taskId) {
|
||
continue;
|
||
}
|
||
|
||
const comment = commentsMap[taskId];
|
||
if (comment !== undefined) {
|
||
sheet.getRange(i + 1, col['Комментарий исполнителя'] + 1).setValue(comment);
|
||
}
|
||
}
|
||
}
|