const db = require('../models'); const FileDBApi = require('./file'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class Job_vacancy_skillsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const job_vacancy_skills = await db.job_vacancy_skills.create( { id: data.id || undefined, importance: data.importance || null , min_proficiency: data.min_proficiency || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await job_vacancy_skills.setJob_vacancy( data.job_vacancy || null, { transaction, }); await job_vacancy_skills.setSkill( data.skill || null, { transaction, }); return job_vacancy_skills; } static async bulkImport(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; // Prepare data - wrapping individual data transformations in a map() method const job_vacancy_skillsData = data.map((item, index) => ({ id: item.id || undefined, importance: item.importance || null , min_proficiency: item.min_proficiency || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const job_vacancy_skills = await db.job_vacancy_skills.bulkCreate(job_vacancy_skillsData, { transaction }); // For each item created, replace relation files return job_vacancy_skills; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const job_vacancy_skills = await db.job_vacancy_skills.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.importance !== undefined) updatePayload.importance = data.importance; if (data.min_proficiency !== undefined) updatePayload.min_proficiency = data.min_proficiency; updatePayload.updatedById = currentUser.id; await job_vacancy_skills.update(updatePayload, {transaction}); if (data.job_vacancy !== undefined) { await job_vacancy_skills.setJob_vacancy( data.job_vacancy, { transaction } ); } if (data.skill !== undefined) { await job_vacancy_skills.setSkill( data.skill, { transaction } ); } return job_vacancy_skills; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const job_vacancy_skills = await db.job_vacancy_skills.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of job_vacancy_skills) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of job_vacancy_skills) { await record.destroy({transaction}); } }); return job_vacancy_skills; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const job_vacancy_skills = await db.job_vacancy_skills.findByPk(id, options); await job_vacancy_skills.update({ deletedBy: currentUser.id }, { transaction, }); await job_vacancy_skills.destroy({ transaction }); return job_vacancy_skills; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const job_vacancy_skills = await db.job_vacancy_skills.findOne( { where }, { transaction }, ); if (!job_vacancy_skills) { return job_vacancy_skills; } const output = job_vacancy_skills.get({plain: true}); output.job_vacancy = await job_vacancy_skills.getJob_vacancy({ transaction }); output.skill = await job_vacancy_skills.getSkill({ transaction }); return output; } static async findAll( filter, options ) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.job_vacancies, as: 'job_vacancy', where: filter.job_vacancy ? { [Op.or]: [ { id: { [Op.in]: filter.job_vacancy.split('|').map(term => Utils.uuid(term)) } }, { job_title: { [Op.or]: filter.job_vacancy.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.skills, as: 'skill', where: filter.skill ? { [Op.or]: [ { id: { [Op.in]: filter.skill.split('|').map(term => Utils.uuid(term)) } }, { skill_name: { [Op.or]: filter.skill.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.importance) { where = { ...where, importance: filter.importance, }; } if (filter.min_proficiency) { where = { ...where, min_proficiency: filter.min_proficiency, }; } if (filter.createdAtRange) { const [start, end] = filter.createdAtRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, ['createdAt']: { ...where.createdAt, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, ['createdAt']: { ...where.createdAt, [Op.lte]: end, }, }; } } } const queryOptions = { where, include, distinct: true, order: filter.field && filter.sort ? [[filter.field, filter.sort]] : [['createdAt', 'desc']], transaction: options?.transaction, logging: console.log }; if (!options?.countOnly) { queryOptions.limit = limit ? Number(limit) : undefined; queryOptions.offset = offset ? Number(offset) : undefined; } try { const { rows, count } = await db.job_vacancy_skills.findAndCountAll(queryOptions); return { rows: options?.countOnly ? [] : rows, count: count }; } catch (error) { console.error('Error executing query:', error); throw error; } } static async findAllAutocomplete(query, limit, offset, ) { let where = {}; if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike( 'job_vacancy_skills', 'importance', query, ), ], }; } const records = await db.job_vacancy_skills.findAll({ attributes: [ 'id', 'importance' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['importance', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.importance, })); } };