123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- const ExecutivesHistoryRepository = require('../../repository/sales/ExecutivesHistoryRepository.js');
- const HttpException = require('../../utils/HttpException.js');
- const prisma = require('../../prisma/PrismaClient.js');
- const { SearchFilter } = require('../../utils/SearchFilter.js');
- const timeLocal = require('../../utils/TimeLocal.js');
- const { createLog, updateLog, deleteLog } = require('../../utils/LogActivity.js');
- exports.getAllExecutivesHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
- const skip = (page - 1) * limit;
- const hospitalId = req.params.id;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: hospitalId
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const where = {
- // ...SearchFilter(search, ['name', 'name_pt']),
- hospital_id: req.params.id,
- deletedAt: null
- };
- const [executives_histories, total] = await Promise.all([
- ExecutivesHistoryRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
- ExecutivesHistoryRepository.countAll(where)
- ]);
- return { executives_histories, total };
- };
- exports.showExecutivesHistoryService = async (req) => {
- const id_hospital = req.params.id;
- const id_executives_history = req.params.id_executives_history;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: id_hospital
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const executivesHistory = await ExecutivesHistoryRepository.findById(id_executives_history);
- if (!executivesHistory) {
- throw new HttpException("Executives history not found", 404);
- }
- return executivesHistory;
- };
- exports.storeExecutivesHistoryService = async (validateData, req) => {
- const hospitalId = req.params.id;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: hospitalId
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- if (validateData.status === "active") {
- await prisma.executivesHistory.updateMany({
- where: {
- hospital_id: hospitalId,
- status: "active"
- },
- data: {
- status: "inactive"
- }
- });
- }
- const payload = {
- ...validateData,
- hospital_id: hospitalId
- };
- const data = await ExecutivesHistoryRepository.create(payload);
- await createLog(req, data);
- };
- exports.updateExecutivesHistoryService = async (validateData, req) => {
- const id_hospital = req.params.id;
- const id_executives_history = req.params.id_executives_history;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: id_hospital
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const executivesHistory = await ExecutivesHistoryRepository.findById(id_executives_history);
- if (!executivesHistory) {
- throw new HttpException("Executives history not found", 404);
- }
- if (validateData.start_term && validateData.end_term) {
- if (validateData.start_term >= validateData.end_term) {
- throw new HttpException("End term must be after start_term", 400)
- }
- }
- if (validateData.status === "active") {
- await prisma.executivesHistory.updateMany({
- where: {
- hospital_id: id_hospital,
- status: "active"
- },
- data: {
- status: "inactive"
- }
- });
- }
- const payload = {
- ...validateData,
- start_term: validateData.start_term ? new Date(validateData.start_term) : executivesHistory.start_term,
- end_term: validateData.end_term ? new Date(validateData.end_term) : executivesHistory.end_term,
- };
- const data = await ExecutivesHistoryRepository.update(id_executives_history, payload);
- await updateLog(req, data);
- };
- exports.deleteExecutivesHistoryService = async (req) => {
- const id_hospital = req.params.id;
- const id_executives_history = req.params.id_executives_history;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: id_hospital
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const executivesHistory = await ExecutivesHistoryRepository.findById(id_executives_history);
- if (!executivesHistory) {
- throw new HttpException("Executives history not found", 404);
- }
- const data = await ExecutivesHistoryRepository.update(id_executives_history, {
- deletedAt: timeLocal.now().toDate()
- });
- await deleteLog(req, data);
- };
|