307 lines
7.5 KiB
JavaScript
307 lines
7.5 KiB
JavaScript
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_listingsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_listings = await db.job_listings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
job_title: data.job_title || null,
|
|
company: data.company || null,
|
|
source: data.source || null,
|
|
apply_link: data.apply_link || null,
|
|
location: data.location || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
return job_listings;
|
|
}
|
|
|
|
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_listingsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
job_title: item.job_title || null,
|
|
company: item.company || null,
|
|
source: item.source || null,
|
|
apply_link: item.apply_link || null,
|
|
location: item.location || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const job_listings = await db.job_listings.bulkCreate(job_listingsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return job_listings;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_listings = await db.job_listings.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.job_title !== undefined) updatePayload.job_title = data.job_title;
|
|
|
|
if (data.company !== undefined) updatePayload.company = data.company;
|
|
|
|
if (data.source !== undefined) updatePayload.source = data.source;
|
|
|
|
if (data.apply_link !== undefined)
|
|
updatePayload.apply_link = data.apply_link;
|
|
|
|
if (data.location !== undefined) updatePayload.location = data.location;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await job_listings.update(updatePayload, { transaction });
|
|
|
|
return job_listings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_listings = await db.job_listings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of job_listings) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of job_listings) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return job_listings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_listings = await db.job_listings.findByPk(id, options);
|
|
|
|
await job_listings.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await job_listings.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return job_listings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const job_listings = await db.job_listings.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!job_listings) {
|
|
return job_listings;
|
|
}
|
|
|
|
const output = job_listings.get({ plain: true });
|
|
|
|
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 = [];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.job_title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('job_listings', 'job_title', filter.job_title),
|
|
};
|
|
}
|
|
|
|
if (filter.company) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('job_listings', 'company', filter.company),
|
|
};
|
|
}
|
|
|
|
if (filter.source) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('job_listings', 'source', filter.source),
|
|
};
|
|
}
|
|
|
|
if (filter.apply_link) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'job_listings',
|
|
'apply_link',
|
|
filter.apply_link,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('job_listings', 'location', filter.location),
|
|
};
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
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_listings.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_listings', 'job_title', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.job_listings.findAll({
|
|
attributes: ['id', 'job_title'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['job_title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.job_title,
|
|
}));
|
|
}
|
|
};
|