Este repositorio centraliza las operaciones relacionadas con el modelo OfertaEmpleo y sus interacciones en la base de datos en Matchify. Proporciona funciones para crear, buscar, actualizar, eliminar y gestionar ofertas de empleo y sus relaciones con empresas, usuarios y candidatos.

import OfertaEmpleo from '../models/OfertaEmpleo.js';
import Empresa from '../models/Empresa.js';
import User from '../models/User.js';
import CandidatoOferta from '../models/CandidatoOferta.js';
import { Op } from 'sequelize';

export const createJobOffer = async (jobData) => {
    try {
        return await OfertaEmpleo.create(jobData);
    } catch (error) {
        throw new Error(`Error al crear la oferta de empleo: ${error.message}`);
    }
};

export const searchJobOffers = async (filters) => {
    return await OfertaEmpleo.findAll({
        where: filters,
        order: [['fechaPublicacion', 'DESC']],
    });
};

export const getEmpresasByIds = async (empresaIds) => {
    return await Empresa.findAll({
        where: { id: empresaIds },
        attributes: ['id', 'nombre', 'telefono', 'sitioWeb', 'email', 'descripcion', 'direccion'],
    });
};

export const getUserById = async (userId) => {
    return await User.findOne({
        where: { id: userId },
        attributes: ['username'],
    });
};

export const getJobOfferById = async (id) => {
    return await OfertaEmpleo.findByPk(id);
};

export const updateJobOffer = async (id, updatedData) => {
    const oferta = await OfertaEmpleo.findByPk(id);
    if (!oferta) throw new Error('Oferta no encontrada');
    return await oferta.update(updatedData);
};

export const deleteJobOffer = async (id) => {
    const oferta = await OfertaEmpleo.findByPk(id);
    if (!oferta) throw new Error('Oferta no encontrada');
    await oferta.destroy();
};

export const getJobOffersByCompany = async (empresaId, userId) => {
    const whereClause = { empresaId };
    if (userId) whereClause.userId = userId;

    return await OfertaEmpleo.findAll({
        where: whereClause,
        order: [['fechaPublicacion', 'DESC']],
    });
};

export const getActiveJobOffers = async () => {
    return await OfertaEmpleo.findAll({
        where: { estatus: 'Activo' },
        order: [['fechaPublicacion', 'DESC']],
    });
};

export const getJobCountByStatus = async (empresaId, status) => {
    return await OfertaEmpleo.count({
        where: {
            empresaId,
            estatus: status
        }
    });
};

export const getJobOffersByStatus = async (empresaId, status) => {
    return await OfertaEmpleo.findAll({
        where: { empresaId, estatus: status },
        attributes: ['id', 'titulo', 'fechaPublicacion'],
        order: [['fechaPublicacion', 'DESC']],
    });
};

export const countCandidatesForJobOffer = async (jobOfferId) => {
    try {
        const count = await CandidatoOferta.count({
            where: { ofertaEmpleoId: jobOfferId },
        });
        return count;
    } catch (error) {
        throw new Error(`Error al contar los candidatos aplicados a la oferta: ${error.message}`);
    }
};

Explicación de cada función

  1. createJobOffer(jobData):
  2. searchJobOffers(filters):
  3. getEmpresasByIds(empresaIds):
  4. getUserById(userId):
  5. getJobOfferById(id):
  6. updateJobOffer(id, updatedData):
  7. deleteJobOffer(id):
  8. getJobOffersByCompany(empresaId, userId):
  9. getActiveJobOffers():
  10. getJobCountByStatus(empresaId, status):
  11. getJobOffersByStatus(empresaId, status):
  12. countCandidatesForJobOffer(jobOfferId):