575 lines
15 KiB
JavaScript
575 lines
15 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 AssessmentsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const assessments = await db.assessments.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
departure_country: data.departure_country || null,
|
|
destination_countries: data.destination_countries || null,
|
|
reason_for_travel: data.reason_for_travel || null,
|
|
number_of_people: data.number_of_people || null,
|
|
budget_range: data.budget_range || null,
|
|
desired_visa_type: data.desired_visa_type || null,
|
|
language_proficiencies: data.language_proficiencies || null,
|
|
academic_background: data.academic_background || null,
|
|
professional_background: data.professional_background || null,
|
|
family_abroad_info: data.family_abroad_info || null,
|
|
visa_success_rate: data.visa_success_rate || null,
|
|
suggested_countries: data.suggested_countries || null,
|
|
required_documents: data.required_documents || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await assessments.setUser(data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await assessments.setTravel_agencies(data.travel_agencies || null, {
|
|
transaction,
|
|
});
|
|
|
|
return assessments;
|
|
}
|
|
|
|
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 assessmentsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
departure_country: item.departure_country || null,
|
|
destination_countries: item.destination_countries || null,
|
|
reason_for_travel: item.reason_for_travel || null,
|
|
number_of_people: item.number_of_people || null,
|
|
budget_range: item.budget_range || null,
|
|
desired_visa_type: item.desired_visa_type || null,
|
|
language_proficiencies: item.language_proficiencies || null,
|
|
academic_background: item.academic_background || null,
|
|
professional_background: item.professional_background || null,
|
|
family_abroad_info: item.family_abroad_info || null,
|
|
visa_success_rate: item.visa_success_rate || null,
|
|
suggested_countries: item.suggested_countries || null,
|
|
required_documents: item.required_documents || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const assessments = await db.assessments.bulkCreate(assessmentsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return assessments;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const assessments = await db.assessments.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.departure_country !== undefined)
|
|
updatePayload.departure_country = data.departure_country;
|
|
|
|
if (data.destination_countries !== undefined)
|
|
updatePayload.destination_countries = data.destination_countries;
|
|
|
|
if (data.reason_for_travel !== undefined)
|
|
updatePayload.reason_for_travel = data.reason_for_travel;
|
|
|
|
if (data.number_of_people !== undefined)
|
|
updatePayload.number_of_people = data.number_of_people;
|
|
|
|
if (data.budget_range !== undefined)
|
|
updatePayload.budget_range = data.budget_range;
|
|
|
|
if (data.desired_visa_type !== undefined)
|
|
updatePayload.desired_visa_type = data.desired_visa_type;
|
|
|
|
if (data.language_proficiencies !== undefined)
|
|
updatePayload.language_proficiencies = data.language_proficiencies;
|
|
|
|
if (data.academic_background !== undefined)
|
|
updatePayload.academic_background = data.academic_background;
|
|
|
|
if (data.professional_background !== undefined)
|
|
updatePayload.professional_background = data.professional_background;
|
|
|
|
if (data.family_abroad_info !== undefined)
|
|
updatePayload.family_abroad_info = data.family_abroad_info;
|
|
|
|
if (data.visa_success_rate !== undefined)
|
|
updatePayload.visa_success_rate = data.visa_success_rate;
|
|
|
|
if (data.suggested_countries !== undefined)
|
|
updatePayload.suggested_countries = data.suggested_countries;
|
|
|
|
if (data.required_documents !== undefined)
|
|
updatePayload.required_documents = data.required_documents;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await assessments.update(updatePayload, { transaction });
|
|
|
|
if (data.user !== undefined) {
|
|
await assessments.setUser(
|
|
data.user,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.travel_agencies !== undefined) {
|
|
await assessments.setTravel_agencies(
|
|
data.travel_agencies,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return assessments;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const assessments = await db.assessments.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of assessments) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of assessments) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return assessments;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const assessments = await db.assessments.findByPk(id, options);
|
|
|
|
await assessments.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await assessments.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return assessments;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const assessments = await db.assessments.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!assessments) {
|
|
return assessments;
|
|
}
|
|
|
|
const output = assessments.get({ plain: true });
|
|
|
|
output.user = await assessments.getUser({
|
|
transaction,
|
|
});
|
|
|
|
output.travel_agencies = await assessments.getTravel_agencies({
|
|
transaction,
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userTravel_agencies = (user && user.travel_agencies?.id) || null;
|
|
|
|
if (userTravel_agencies) {
|
|
if (options?.currentUser?.travel_agenciesId) {
|
|
where.travel_agenciesId = options.currentUser.travel_agenciesId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.users,
|
|
as: 'user',
|
|
|
|
where: filter.user
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.user
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.user
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.travel_agencies,
|
|
as: 'travel_agencies',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.departure_country) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'departure_country',
|
|
filter.departure_country,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.destination_countries) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'destination_countries',
|
|
filter.destination_countries,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.reason_for_travel) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'reason_for_travel',
|
|
filter.reason_for_travel,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.budget_range) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'budget_range',
|
|
filter.budget_range,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.desired_visa_type) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'desired_visa_type',
|
|
filter.desired_visa_type,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.language_proficiencies) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'language_proficiencies',
|
|
filter.language_proficiencies,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.academic_background) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'academic_background',
|
|
filter.academic_background,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.professional_background) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'professional_background',
|
|
filter.professional_background,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.family_abroad_info) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'family_abroad_info',
|
|
filter.family_abroad_info,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.suggested_countries) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'suggested_countries',
|
|
filter.suggested_countries,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.required_documents) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'assessments',
|
|
'required_documents',
|
|
filter.required_documents,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.number_of_peopleRange) {
|
|
const [start, end] = filter.number_of_peopleRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
number_of_people: {
|
|
...where.number_of_people,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
number_of_people: {
|
|
...where.number_of_people,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.visa_success_rateRange) {
|
|
const [start, end] = filter.visa_success_rateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
visa_success_rate: {
|
|
...where.visa_success_rate,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
visa_success_rate: {
|
|
...where.visa_success_rate,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.travel_agencies) {
|
|
const listItems = filter.travel_agencies.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
travel_agenciesId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.travel_agenciesId;
|
|
}
|
|
|
|
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.assessments.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,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('assessments', 'departure_country', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.assessments.findAll({
|
|
attributes: ['id', 'departure_country'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['departure_country', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.departure_country,
|
|
}));
|
|
}
|
|
};
|