555 lines
13 KiB
JavaScript
555 lines
13 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 CompaniesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const companies = await db.companies.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
legal_name: data.legal_name
|
|
||
|
|
null
|
|
,
|
|
|
|
brand_name: data.brand_name
|
|
||
|
|
null
|
|
,
|
|
|
|
logo_text: data.logo_text
|
|
||
|
|
null
|
|
,
|
|
|
|
phone_number: data.phone_number
|
|
||
|
|
null
|
|
,
|
|
|
|
address: data.address
|
|
||
|
|
null
|
|
,
|
|
|
|
country: data.country
|
|
||
|
|
null
|
|
,
|
|
|
|
city: data.city
|
|
||
|
|
null
|
|
,
|
|
|
|
is_active: data.is_active
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return companies;
|
|
}
|
|
|
|
|
|
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 companiesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
legal_name: item.legal_name
|
|
||
|
|
null
|
|
,
|
|
|
|
brand_name: item.brand_name
|
|
||
|
|
null
|
|
,
|
|
|
|
logo_text: item.logo_text
|
|
||
|
|
null
|
|
,
|
|
|
|
phone_number: item.phone_number
|
|
||
|
|
null
|
|
,
|
|
|
|
address: item.address
|
|
||
|
|
null
|
|
,
|
|
|
|
country: item.country
|
|
||
|
|
null
|
|
,
|
|
|
|
city: item.city
|
|
||
|
|
null
|
|
,
|
|
|
|
is_active: item.is_active
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const companies = await db.companies.bulkCreate(companiesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return companies;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const companies = await db.companies.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.legal_name !== undefined) updatePayload.legal_name = data.legal_name;
|
|
|
|
|
|
if (data.brand_name !== undefined) updatePayload.brand_name = data.brand_name;
|
|
|
|
|
|
if (data.logo_text !== undefined) updatePayload.logo_text = data.logo_text;
|
|
|
|
|
|
if (data.phone_number !== undefined) updatePayload.phone_number = data.phone_number;
|
|
|
|
|
|
if (data.address !== undefined) updatePayload.address = data.address;
|
|
|
|
|
|
if (data.country !== undefined) updatePayload.country = data.country;
|
|
|
|
|
|
if (data.city !== undefined) updatePayload.city = data.city;
|
|
|
|
|
|
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await companies.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return companies;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const companies = await db.companies.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of companies) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of companies) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return companies;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const companies = await db.companies.findByPk(id, options);
|
|
|
|
await companies.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await companies.destroy({
|
|
transaction
|
|
});
|
|
|
|
return companies;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const companies = await db.companies.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!companies) {
|
|
return companies;
|
|
}
|
|
|
|
const output = companies.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.drivers_company = await companies.getDrivers_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.customers_company = await companies.getCustomers_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.trucks_company = await companies.getTrucks_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.trips_company = await companies.getTrips_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.tracker_devices_company = await companies.getTracker_devices_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.violation_types_company = await companies.getViolation_types_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.violations_company = await companies.getViolations_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.accident_damage_reports_company = await companies.getAccident_damage_reports_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.salary_slips_company = await companies.getSalary_slips_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.notifications_company = await companies.getNotifications_company({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.settings_company = await companies.getSettings_company({
|
|
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 = [
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.legal_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'legal_name',
|
|
filter.legal_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.brand_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'brand_name',
|
|
filter.brand_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.logo_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'logo_text',
|
|
filter.logo_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.phone_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'phone_number',
|
|
filter.phone_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'address',
|
|
filter.address,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.country) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'country',
|
|
filter.country,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.city) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'companies',
|
|
'city',
|
|
filter.city,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_active) {
|
|
where = {
|
|
...where,
|
|
is_active: filter.is_active,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.companies.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(
|
|
'companies',
|
|
'legal_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.companies.findAll({
|
|
attributes: [ 'id', 'legal_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['legal_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.legal_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|