190 lines
7.1 KiB
JavaScript
190 lines
7.1 KiB
JavaScript
function beforeTaskSave(colleagueId, nextSequenceId, userList) {
|
|
try {
|
|
var currentState = parseInt(String(getValue("WKNumState") || "0"), 10);
|
|
var nextState = parseInt(String(nextSequenceId || "0"), 10);
|
|
var completeTask = String(getValue("WKCompletTask") || "false");
|
|
log.info("[Recrutamento.beforeTaskSave] currentState=" + currentState + ", nextState=" + nextState + ", completeTask=" + completeTask);
|
|
|
|
if (completeTask !== "true") return;
|
|
|
|
// Disparo do e-mail inicial da solicitação (atividade inicial = 1).
|
|
if (currentState === 1) {
|
|
enviarNotificacaoSolicitacao();
|
|
return;
|
|
}
|
|
|
|
// Disparo quando a seleção define o início do colaborador.
|
|
if (currentState === 6 && nextState === 46) {
|
|
enviarNotificacaoInicioColaborador();
|
|
}
|
|
} catch (e) {
|
|
log.error("[Recrutamento.beforeTaskSave] Erro ao disparar notificação: " + e);
|
|
}
|
|
}
|
|
|
|
function enviarNotificacaoSolicitacao() {
|
|
var envio = montarContextoEnvio();
|
|
if (!envio.ok) return;
|
|
|
|
var params = new java.util.HashMap();
|
|
params.put("WKNumProces", envio.processNumber);
|
|
params.put("linkSolicitacao", envio.processLink);
|
|
params.put("link", envio.processLink);
|
|
params.put("requesterName", valueOrDefault(hAPI.getCardValue("requesterName")));
|
|
params.put("gestorNome", valueOrDefault(hAPI.getCardValue("gestorNome")));
|
|
params.put("dataAbertura", valueOrDefault(hAPI.getCardValue("dataAbertura")));
|
|
params.put("cargo", valueOrDefault(hAPI.getCardValue("funcao")));
|
|
params.put("kitUtilizado", getKitDescricao(hAPI.getCardValue("itensinicio")));
|
|
params.put("motivoSolicitacao", getMotivoDescricao(hAPI.getCardValue("validarMotivo")));
|
|
params.put("justificativa", valueOrDefault(hAPI.getCardValue("justificativa")));
|
|
|
|
notifier.notify("admin", "tpl_recrutamento_realizada_dia", params, envio.destinatarios, "text/html");
|
|
log.info("[Recrutamento.beforeTaskSave] tpl_recrutamento_realizada_dia enviado. processo=" + envio.processNumber + ", destinos=" + envio.destinosLog);
|
|
}
|
|
|
|
function enviarNotificacaoInicioColaborador() {
|
|
var envio = montarContextoEnvio();
|
|
if (!envio.ok) return;
|
|
|
|
var dataInicio = safeTrim(hAPI.getCardValue("datainiciotrabalho"));
|
|
if (dataInicio === "") {
|
|
log.warn("[Recrutamento.beforeTaskSave] Data de início vazia. Notificação de início não enviada.");
|
|
return;
|
|
}
|
|
|
|
var params = new java.util.HashMap();
|
|
params.put("WKNumProces", envio.processNumber);
|
|
params.put("linkSolicitacao", envio.processLink);
|
|
params.put("link", envio.processLink);
|
|
params.put("colaboradorNome", valueOrDefault(hAPI.getCardValue("nomedocandidato")));
|
|
params.put("cargo", valueOrDefault(hAPI.getCardValue("funcao")));
|
|
params.put("dataInicio", formatDateBr(dataInicio));
|
|
params.put("kitUtilizado", getKitDescricao(hAPI.getCardValue("itensinicio")));
|
|
|
|
notifier.notify("admin", "tpl_recrutamento_realizada", params, envio.destinatarios, "text/html");
|
|
log.info("[Recrutamento.beforeTaskSave] tpl_recrutamento_realizada enviado. processo=" + envio.processNumber + ", destinos=" + envio.destinosLog);
|
|
}
|
|
|
|
function montarContextoEnvio() {
|
|
var destinatarios = new java.util.ArrayList();
|
|
var jaAdicionados = {};
|
|
|
|
addEmail(destinatarios, jaAdicionados, "tic@grupoginseng.com.br");
|
|
|
|
var requesterId = safeTrim(hAPI.getCardValue("requesterId"));
|
|
if (requesterId !== "") {
|
|
addEmail(destinatarios, jaAdicionados, resolveEmailByColleagueId(requesterId));
|
|
}
|
|
|
|
if (destinatarios.isEmpty()) {
|
|
log.warn("[Recrutamento.beforeTaskSave] Nenhum e-mail válido encontrado para notificação.");
|
|
return { ok: false };
|
|
}
|
|
|
|
var processNumber = safeTrim(getValue("WKNumProces"));
|
|
var processLink = buildProcessLink(processNumber);
|
|
|
|
return {
|
|
ok: true,
|
|
processNumber: processNumber,
|
|
processLink: processLink,
|
|
destinatarios: destinatarios,
|
|
destinosLog: String(destinatarios.toString())
|
|
};
|
|
}
|
|
|
|
function addEmail(destinatarios, jaAdicionados, email) {
|
|
var v = safeTrim(email).toLowerCase();
|
|
if (!isValidEmail(v)) return;
|
|
if (jaAdicionados[v]) return;
|
|
|
|
destinatarios.add(v);
|
|
jaAdicionados[v] = true;
|
|
}
|
|
|
|
function resolveEmailByColleagueId(colleagueId) {
|
|
var id = safeTrim(colleagueId);
|
|
if (id === "") return "";
|
|
|
|
try {
|
|
var cActive = DatasetFactory.createConstraint("active", "true", "true", ConstraintType.MUST);
|
|
|
|
var cId = DatasetFactory.createConstraint("colleaguePK.colleagueId", id, id, ConstraintType.MUST);
|
|
var byId = DatasetFactory.getDataset("colleague", null, [cId, cActive], null);
|
|
if (byId && byId.rowsCount > 0) {
|
|
return safeTrim(byId.getValue(0, "mail"));
|
|
}
|
|
|
|
var cLogin = DatasetFactory.createConstraint("login", id, id, ConstraintType.MUST);
|
|
var byLogin = DatasetFactory.getDataset("colleague", null, [cLogin, cActive], null);
|
|
if (byLogin && byLogin.rowsCount > 0) {
|
|
return safeTrim(byLogin.getValue(0, "mail"));
|
|
}
|
|
} catch (e) {
|
|
log.warn("[Recrutamento.beforeTaskSave] Falha ao buscar e-mail por colleagueId: " + e);
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
function buildProcessLink(processNumber) {
|
|
var baseUrl = safeTrim(getValue("WKServerURL"));
|
|
var companyId = safeTrim(getValue("WKCompany"));
|
|
|
|
if (baseUrl === "" || processNumber === "") return "";
|
|
if (baseUrl.indexOf("http://") !== 0 && baseUrl.indexOf("https://") !== 0) {
|
|
baseUrl = "https://" + baseUrl;
|
|
}
|
|
if (baseUrl.charAt(baseUrl.length - 1) === "/") {
|
|
baseUrl = baseUrl.substring(0, baseUrl.length - 1);
|
|
}
|
|
if (companyId === "") companyId = "1";
|
|
|
|
return baseUrl + "/portal/p/" + companyId + "/pageworkflowview?app_ecm_workflowview_detailsProcessInstanceID=" + processNumber;
|
|
}
|
|
|
|
function getMotivoDescricao(motivo) {
|
|
var v = safeTrim(motivo).toLowerCase();
|
|
if (v === "aumento") return "Aumento de quadro";
|
|
if (v === "substituicao") return "Substituição";
|
|
if (v === "temporario") return "Temporário";
|
|
return v === "" ? "Não informado" : v;
|
|
}
|
|
|
|
function getKitDescricao(kit) {
|
|
var v = safeTrim(kit).toLowerCase();
|
|
if (v === "backoffice") return "Kit Backoffice (Notebook, mouse, teclado e headset)";
|
|
if (v === "amg") return "Kit AMG (Notebook, kit mouse e teclado, headset e telefone corporativo)";
|
|
if (v === "comercial") return "Kit Comercial (Mobshop e Mobpin)";
|
|
return v === "" ? "Não informado" : v;
|
|
}
|
|
|
|
function formatDateBr(value) {
|
|
var v = safeTrim(value);
|
|
if (/^\d{4}-\d{2}-\d{2}$/.test(v)) {
|
|
return v.substring(8, 10) + "/" + v.substring(5, 7) + "/" + v.substring(0, 4);
|
|
}
|
|
return v;
|
|
}
|
|
|
|
function valueOrDefault(value) {
|
|
var v = safeTrim(value);
|
|
return v === "" ? "Não informado" : v;
|
|
}
|
|
|
|
function isValidEmail(email) {
|
|
var v = safeTrim(email);
|
|
if (v === "") return false;
|
|
if (/\s/.test(v)) return false;
|
|
|
|
var at = v.indexOf("@");
|
|
if (at <= 0 || at !== v.lastIndexOf("@")) return false;
|
|
|
|
var dot = v.lastIndexOf(".");
|
|
return dot > at + 1 && dot < (v.length - 1);
|
|
}
|
|
|
|
function safeTrim(value) {
|
|
return String(value == null ? "" : value).trim();
|
|
}
|