561 lines
15 KiB
JavaScript
561 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 AccidentsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const accidents = await db.accidents.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
accident_date: data.accident_date || null,
|
|
birth_date: data.birth_date || null,
|
|
is_driver: data.is_driver || false,
|
|
|
|
driver_responsibility: data.driver_responsibility || null,
|
|
salary_type: data.salary_type || null,
|
|
declared_salary: data.declared_salary || null,
|
|
disability_percentage: data.disability_percentage || null,
|
|
temporary_disability_days: data.temporary_disability_days || null,
|
|
moral_damage: data.moral_damage || null,
|
|
professional_damage: data.professional_damage || null,
|
|
treatment_expenses: data.treatment_expenses || null,
|
|
legal_expenses: data.legal_expenses || null,
|
|
adjustment_percentage: data.adjustment_percentage || null,
|
|
attorney_fees_percentage: data.attorney_fees_percentage || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
return accidents;
|
|
}
|
|
|
|
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 accidentsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
accident_date: item.accident_date || null,
|
|
birth_date: item.birth_date || null,
|
|
is_driver: item.is_driver || false,
|
|
|
|
driver_responsibility: item.driver_responsibility || null,
|
|
salary_type: item.salary_type || null,
|
|
declared_salary: item.declared_salary || null,
|
|
disability_percentage: item.disability_percentage || null,
|
|
temporary_disability_days: item.temporary_disability_days || null,
|
|
moral_damage: item.moral_damage || null,
|
|
professional_damage: item.professional_damage || null,
|
|
treatment_expenses: item.treatment_expenses || null,
|
|
legal_expenses: item.legal_expenses || null,
|
|
adjustment_percentage: item.adjustment_percentage || null,
|
|
attorney_fees_percentage: item.attorney_fees_percentage || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const accidents = await db.accidents.bulkCreate(accidentsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return accidents;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const accidents = await db.accidents.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.accident_date !== undefined)
|
|
updatePayload.accident_date = data.accident_date;
|
|
|
|
if (data.birth_date !== undefined)
|
|
updatePayload.birth_date = data.birth_date;
|
|
|
|
if (data.is_driver !== undefined) updatePayload.is_driver = data.is_driver;
|
|
|
|
if (data.driver_responsibility !== undefined)
|
|
updatePayload.driver_responsibility = data.driver_responsibility;
|
|
|
|
if (data.salary_type !== undefined)
|
|
updatePayload.salary_type = data.salary_type;
|
|
|
|
if (data.declared_salary !== undefined)
|
|
updatePayload.declared_salary = data.declared_salary;
|
|
|
|
if (data.disability_percentage !== undefined)
|
|
updatePayload.disability_percentage = data.disability_percentage;
|
|
|
|
if (data.temporary_disability_days !== undefined)
|
|
updatePayload.temporary_disability_days = data.temporary_disability_days;
|
|
|
|
if (data.moral_damage !== undefined)
|
|
updatePayload.moral_damage = data.moral_damage;
|
|
|
|
if (data.professional_damage !== undefined)
|
|
updatePayload.professional_damage = data.professional_damage;
|
|
|
|
if (data.treatment_expenses !== undefined)
|
|
updatePayload.treatment_expenses = data.treatment_expenses;
|
|
|
|
if (data.legal_expenses !== undefined)
|
|
updatePayload.legal_expenses = data.legal_expenses;
|
|
|
|
if (data.adjustment_percentage !== undefined)
|
|
updatePayload.adjustment_percentage = data.adjustment_percentage;
|
|
|
|
if (data.attorney_fees_percentage !== undefined)
|
|
updatePayload.attorney_fees_percentage = data.attorney_fees_percentage;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await accidents.update(updatePayload, { transaction });
|
|
|
|
return accidents;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const accidents = await db.accidents.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of accidents) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of accidents) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return accidents;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const accidents = await db.accidents.findByPk(id, options);
|
|
|
|
await accidents.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await accidents.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return accidents;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const accidents = await db.accidents.findOne({ where }, { transaction });
|
|
|
|
if (!accidents) {
|
|
return accidents;
|
|
}
|
|
|
|
const output = accidents.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.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
accident_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
birth_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (filter.accident_dateRange) {
|
|
const [start, end] = filter.accident_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
accident_date: {
|
|
...where.accident_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
accident_date: {
|
|
...where.accident_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.birth_dateRange) {
|
|
const [start, end] = filter.birth_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
birth_date: {
|
|
...where.birth_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
birth_date: {
|
|
...where.birth_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.declared_salaryRange) {
|
|
const [start, end] = filter.declared_salaryRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
declared_salary: {
|
|
...where.declared_salary,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
declared_salary: {
|
|
...where.declared_salary,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.disability_percentageRange) {
|
|
const [start, end] = filter.disability_percentageRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
disability_percentage: {
|
|
...where.disability_percentage,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
disability_percentage: {
|
|
...where.disability_percentage,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.temporary_disability_daysRange) {
|
|
const [start, end] = filter.temporary_disability_daysRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
temporary_disability_days: {
|
|
...where.temporary_disability_days,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
temporary_disability_days: {
|
|
...where.temporary_disability_days,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.treatment_expensesRange) {
|
|
const [start, end] = filter.treatment_expensesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
treatment_expenses: {
|
|
...where.treatment_expenses,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
treatment_expenses: {
|
|
...where.treatment_expenses,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.legal_expensesRange) {
|
|
const [start, end] = filter.legal_expensesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
legal_expenses: {
|
|
...where.legal_expenses,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
legal_expenses: {
|
|
...where.legal_expenses,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.attorney_fees_percentageRange) {
|
|
const [start, end] = filter.attorney_fees_percentageRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
attorney_fees_percentage: {
|
|
...where.attorney_fees_percentage,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
attorney_fees_percentage: {
|
|
...where.attorney_fees_percentage,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.is_driver) {
|
|
where = {
|
|
...where,
|
|
is_driver: filter.is_driver,
|
|
};
|
|
}
|
|
|
|
if (filter.driver_responsibility) {
|
|
where = {
|
|
...where,
|
|
driver_responsibility: filter.driver_responsibility,
|
|
};
|
|
}
|
|
|
|
if (filter.salary_type) {
|
|
where = {
|
|
...where,
|
|
salary_type: filter.salary_type,
|
|
};
|
|
}
|
|
|
|
if (filter.moral_damage) {
|
|
where = {
|
|
...where,
|
|
moral_damage: filter.moral_damage,
|
|
};
|
|
}
|
|
|
|
if (filter.professional_damage) {
|
|
where = {
|
|
...where,
|
|
professional_damage: filter.professional_damage,
|
|
};
|
|
}
|
|
|
|
if (filter.adjustment_percentage) {
|
|
where = {
|
|
...where,
|
|
adjustment_percentage: filter.adjustment_percentage,
|
|
};
|
|
}
|
|
|
|
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.accidents.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('accidents', 'accident_date', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.accidents.findAll({
|
|
attributes: ['id', 'accident_date'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['accident_date', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.accident_date,
|
|
}));
|
|
}
|
|
};
|