Skip to content

Commit

Permalink
Merge pull request #22 from fga-eps-mds/feat/68-registro-de-data-por-…
Browse files Browse the repository at this point in the history
…etapa

Registro de data por etapa
  • Loading branch information
sergiosacj authored Jul 2, 2023
2 parents f258ba9 + 74ee636 commit 6f4ab6e
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 57 deletions.
4 changes: 0 additions & 4 deletions src/controllers/Emailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export async function getMailContents() {
});
return mailContents;
} catch (error) {
console.log(error);
return {
error,
message: "Erro ao obter conteúdo dos emails",
Expand All @@ -40,11 +39,9 @@ export async function sendEmail() {
json = await getMailContents();

if (json.length == 0) {
console.log("Não há processos atrasados");
return true;
}
if (!senha) {
console.log("Não há senha");
return false;
}

Expand Down Expand Up @@ -199,7 +196,6 @@ export async function sendEmail() {
transport.sendMail(message);
process = [];
} catch (err) {
console.log("Error occurred. " + err.message);
return false;
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/controllers/FlowController.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class FlowController {
message: `Não há fluxos com o processo '${record}'`,
});
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: `Erro ao buscar fluxos do processo ${record}`,
Expand Down Expand Up @@ -181,12 +180,10 @@ class FlowController {

flowsWithSequences.push(flowSequence);
}
console.log("aquii pelo amor");
return res
.status(200)
.json({ flows: flowsWithSequences || [], totalPages });
} catch (error) {
console.log(error);
return res
.status(500)
.json({ error, message: "Impossível obter fluxos" });
Expand Down Expand Up @@ -218,7 +215,6 @@ class FlowController {

return res.status(200).json(flowSequence);
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: `Impossível obter fluxo ${idFlow}`,
Expand Down Expand Up @@ -258,7 +254,6 @@ class FlowController {
sequences: sequences,
});
} catch (error) {
console.log(error);
return res
.status(500)
.json({ error, message: "Impossível ler sequências" });
Expand All @@ -277,7 +272,6 @@ class FlowController {

return res.status(200).json(flowStages);
} catch (error) {
console.log(error);
return res
.status(500)
.json({ error, message: "Erro ao ler fluxos ligados a etapas" });
Expand All @@ -304,7 +298,6 @@ class FlowController {

res.status(200).json({ usersToNotify: result });
} catch (error) {
console.log(error);
res.status(500).json({
error,
message: "Impossível obter usuários que devem ser notificados no fluxo",
Expand All @@ -327,7 +320,6 @@ class FlowController {
? res.status(status).json(json)
: res.status(status).json({ message });
} catch (error) {
console.log(error);
return res.status(500).json({ error: "Impossível criar fluxo" });
}
}
Expand Down Expand Up @@ -368,7 +360,6 @@ class FlowController {
: res.status(status).json({ message });
}
} catch (error) {
console.log(error);
return res.status(500).json({ error, message: "Impossível criar fluxo" });
}
}
Expand All @@ -392,7 +383,6 @@ class FlowController {
return res.status(404).json({ message: "Fluxo não encontrado" });
}
} catch (error) {
console.log(error);
return res
.status(500)
.json({ error, message: "Impossível apagar fluxo" });
Expand All @@ -417,7 +407,6 @@ class FlowController {
message: `Desassociação entre fluxo '${idFlow}' e etapas '${idStageA}' e '${idStageB}' concluída`,
});
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: `Falha ao desassociar fluxo '${idFlow}' e etapas '${idStageA}' e '${idStageB}'`,
Expand Down
104 changes: 93 additions & 11 deletions src/controllers/ProcessController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import FlowStage from "../models/FlowStage.js";
import Priority from "../models/Priority.js";
import Process from "../models/Process.js";
import Flow from "../models/Flow.js";
import Stage from "../models/Stage.js";
import Database from "../database/index.js";
import { QueryTypes } from "sequelize";
import { tokenToUser } from "../middleware/authMiddleware.js";
Expand All @@ -17,6 +18,23 @@ const recordFilter = (record) => {
return record.replace(regex, "");
};

const IsUtilDay = (data) => {
const diaDaSemana = data.getDay();
return diaDaSemana >= 1 && diaDaSemana <= 5;
};

const handleVerifyDate = (startDate, duration) => {
let days = 0;
while (duration > 0) {
startDate.setDate(startDate.getDate() + 1);
if (IsUtilDay(startDate)) {
duration--;
}
days++;
}
return days;
};

const validateRecord = (record) => {
const filtered = recordFilter(record);
return {
Expand Down Expand Up @@ -44,7 +62,7 @@ class ProcessController {
});

if (!processes || processes.length === 0) {
return res.status(404).json({ error: "Não há processos" });
return res.status(204).json({ error: "Não há processos" });
} else {
const processesWithFlows = [];
for (const process of processes) {
Expand All @@ -67,6 +85,7 @@ class ProcessController {
idPriority: process.idPriority,
idFlow: flowProcessesIdFlows,
status: process.status,
progress: process.progress,
});
}

Expand All @@ -78,7 +97,6 @@ class ProcessController {
.json({ processes: processesWithFlows, totalPages });
}
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: "Erro ao buscar processos",
Expand Down Expand Up @@ -137,7 +155,6 @@ class ProcessController {
return res.json(process);
}
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: `Erro ao procurar processo ${idProcess}`,
Expand Down Expand Up @@ -181,13 +198,11 @@ class ProcessController {
.json({ message: "Criado com sucesso!", flowProcess });
}
} catch (error) {
console.log(error);
return res.status(500).json(error);
}
}
return res.status(404).json({ message: "Erro na criação de processo" });
} catch (error) {
console.log(error);
return res.status(500).json(error);
}
}
Expand Down Expand Up @@ -219,7 +234,6 @@ class ProcessController {

return res.status(200).json(processes);
} catch (error) {
console.log(error);
return res
.status(500)
.json({ error, message: "Erro ao buscar processos" });
Expand Down Expand Up @@ -262,12 +276,37 @@ class ProcessController {
effectiveDate: new Date(),
}
: {};
let tempProgress = [];
if (process.status === "notStarted" && status === "inProgress") {
const currentStage = await Stage.findOne({
where: { idStage: flowStages[0].idStageA },
});

const stageStartDate = new Date();
const stageEndDate = new Date(stageStartDate);
stageEndDate.setDate(
stageEndDate.getDate() +
handleVerifyDate(stageStartDate, currentStage.duration)
);

const progressData = {
idStage: flowStages[0].idStageA,
entrada: new Date(),
vencimento: stageEndDate,
};
tempProgress.push(progressData);
} else {
// let aux = [];
// aux.push(process.progress);
tempProgress = process.progress;
}

process.set({
nickname,
idStage: idStage || process.idStage,
idPriority: priority,
status,
progress: tempProgress,
...startingProcess,
});

Expand Down Expand Up @@ -309,13 +348,12 @@ class ProcessController {

return res.status(200).json({ message: "OK" });
} catch (error) {
console.log(error);
return res.status(500).json({ error, message: "Impossível apagar" });
}
}

async updateProcessStage(req, res) {
const { record, from, to, idFlow } = req.body;
const { record, from, to, idFlow, isNextStage } = req.body;

if (
isNaN(parseInt(from)) ||
Expand Down Expand Up @@ -355,10 +393,57 @@ class ProcessController {
message: `Não há a transição da etapa '${to}' para '${from}' no fluxo '${idFlow}'`,
});
}

const currentProcess = await Process.findOne({
where: { record },
});
const currentToStage = await Stage.findOne({
where: { idStage: to },
});

const currentFromStage = await Stage.findOne({
where: { idStage: from },
});

let tempProgress = [];
let maturityDate;
const stageStartDate = new Date();
const stageEndDate = new Date(stageStartDate);
stageEndDate.setDate(
stageEndDate.getDate() +
handleVerifyDate(stageStartDate, currentToStage.duration)
);

maturityDate = stageEndDate;

if (isNextStage) {
const progressData = {
idStage: to,
entrada: new Date(),
vencimento: maturityDate,
};
tempProgress = currentProcess.progress;
const index = tempProgress.findIndex((x) => x.idStage == to);
index === -1 && tempProgress.push(progressData);
} else {
tempProgress = Array.isArray(currentProcess.progress)
? currentProcess.progress
: [currentProcess.progres];
tempProgress.pop();
tempProgress[tempProgress.length - 1] = {
idStage: to,
entrada: new Date(),
vencimento: maturityDate,
};
}

console.log("FROM TO", currentProcess.idStage, from, to);

const process = await Process.update(
{
idStage: to,
effectiveDate: new Date(),
progress: tempProgress,
},
{
where: {
Expand All @@ -367,7 +452,6 @@ class ProcessController {
},
}
);
console.log(process[0]);
if (process[0] > 0) {
return res.status(200).json({
message: "Etapa atualizada com sucesso",
Expand Down Expand Up @@ -424,12 +508,10 @@ class ProcessController {
}
);

console.log("updateResult = ", updateResult);
return res.status(200).json({
message: "Comentário adicionado com sucesso",
});
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: "Falha ao adicionar comentário",
Expand Down
1 change: 0 additions & 1 deletion src/controllers/RoleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class RoleController {
});
return res.json(role);
} catch (error) {
console.log(error);
return res.status(error).json(error);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/controllers/StageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class StageController {

return res.json(stage);
} catch (error) {
console.log(error);
return res.status(error).json(error);
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/controllers/UnitController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class UnitController {
const totalPages = Math.ceil(totalCount / parseInt(req.query.limit, 10));
return res.json({ units: units || [], totalPages });
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: "Erro ao listar unidades",
Expand All @@ -29,7 +28,6 @@ class UnitController {
});
return res.json(unit);
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: "Erro ao criar unidade",
Expand Down Expand Up @@ -127,7 +125,6 @@ class UnitController {
};
return res.status(200).json(userNoPassword);
} catch (error) {
console.log(error);
return res.status(500).json({
error,
message: "Erro ao configurar usuário como administrador",
Expand All @@ -154,7 +151,6 @@ class UnitController {
await user.save();
return res.status(200).json(user);
} catch (error) {
console.log(error);
return res.status(500).json({
error: "Erro ao configurar usuário como administrador",
});
Expand Down
Loading

0 comments on commit 6f4ab6e

Please sign in to comment.