935 lines
23 KiB
JavaScript
935 lines
23 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 TripsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trips = await db.trips.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
load_from_location: data.load_from_location
|
|
||
|
|
null
|
|
,
|
|
|
|
deliver_to_location: data.deliver_to_location
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_start_at: data.planned_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_end_at: data.planned_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_start_at: data.actual_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_end_at: data.actual_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
trip_status: data.trip_status
|
|
||
|
|
null
|
|
,
|
|
|
|
has_backload: data.has_backload
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
car_count: data.car_count
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_distance_km: data.planned_distance_km
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_distance_km: data.actual_distance_km
|
|
||
|
|
null
|
|
,
|
|
|
|
local_trip_slot: data.local_trip_slot
|
|
||
|
|
null
|
|
,
|
|
|
|
trip_reference: data.trip_reference
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await trips.setCompany( data.company || null, {
|
|
transaction,
|
|
});
|
|
|
|
await trips.setTruck( data.truck || null, {
|
|
transaction,
|
|
});
|
|
|
|
await trips.setDriver( data.driver || null, {
|
|
transaction,
|
|
});
|
|
|
|
await trips.setRoute( data.route || null, {
|
|
transaction,
|
|
});
|
|
|
|
await trips.setLoad_customer( data.load_customer || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return trips;
|
|
}
|
|
|
|
|
|
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 tripsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
load_from_location: item.load_from_location
|
|
||
|
|
null
|
|
,
|
|
|
|
deliver_to_location: item.deliver_to_location
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_start_at: item.planned_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_end_at: item.planned_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_start_at: item.actual_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_end_at: item.actual_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
trip_status: item.trip_status
|
|
||
|
|
null
|
|
,
|
|
|
|
has_backload: item.has_backload
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
car_count: item.car_count
|
|
||
|
|
null
|
|
,
|
|
|
|
planned_distance_km: item.planned_distance_km
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_distance_km: item.actual_distance_km
|
|
||
|
|
null
|
|
,
|
|
|
|
local_trip_slot: item.local_trip_slot
|
|
||
|
|
null
|
|
,
|
|
|
|
trip_reference: item.trip_reference
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const trips = await db.trips.bulkCreate(tripsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return trips;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const trips = await db.trips.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.load_from_location !== undefined) updatePayload.load_from_location = data.load_from_location;
|
|
|
|
|
|
if (data.deliver_to_location !== undefined) updatePayload.deliver_to_location = data.deliver_to_location;
|
|
|
|
|
|
if (data.planned_start_at !== undefined) updatePayload.planned_start_at = data.planned_start_at;
|
|
|
|
|
|
if (data.planned_end_at !== undefined) updatePayload.planned_end_at = data.planned_end_at;
|
|
|
|
|
|
if (data.actual_start_at !== undefined) updatePayload.actual_start_at = data.actual_start_at;
|
|
|
|
|
|
if (data.actual_end_at !== undefined) updatePayload.actual_end_at = data.actual_end_at;
|
|
|
|
|
|
if (data.trip_status !== undefined) updatePayload.trip_status = data.trip_status;
|
|
|
|
|
|
if (data.has_backload !== undefined) updatePayload.has_backload = data.has_backload;
|
|
|
|
|
|
if (data.car_count !== undefined) updatePayload.car_count = data.car_count;
|
|
|
|
|
|
if (data.planned_distance_km !== undefined) updatePayload.planned_distance_km = data.planned_distance_km;
|
|
|
|
|
|
if (data.actual_distance_km !== undefined) updatePayload.actual_distance_km = data.actual_distance_km;
|
|
|
|
|
|
if (data.local_trip_slot !== undefined) updatePayload.local_trip_slot = data.local_trip_slot;
|
|
|
|
|
|
if (data.trip_reference !== undefined) updatePayload.trip_reference = data.trip_reference;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await trips.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.company !== undefined) {
|
|
await trips.setCompany(
|
|
|
|
data.company,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.truck !== undefined) {
|
|
await trips.setTruck(
|
|
|
|
data.truck,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.driver !== undefined) {
|
|
await trips.setDriver(
|
|
|
|
data.driver,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.route !== undefined) {
|
|
await trips.setRoute(
|
|
|
|
data.route,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.load_customer !== undefined) {
|
|
await trips.setLoad_customer(
|
|
|
|
data.load_customer,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return trips;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trips = await db.trips.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of trips) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of trips) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return trips;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trips = await db.trips.findByPk(id, options);
|
|
|
|
await trips.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await trips.destroy({
|
|
transaction
|
|
});
|
|
|
|
return trips;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const trips = await db.trips.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!trips) {
|
|
return trips;
|
|
}
|
|
|
|
const output = trips.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.violations_trip = await trips.getViolations_trip({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.accident_damage_reports_trip = await trips.getAccident_damage_reports_trip({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.notifications_trip = await trips.getNotifications_trip({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.company = await trips.getCompany({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.truck = await trips.getTruck({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.driver = await trips.getDriver({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.route = await trips.getRoute({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.load_customer = await trips.getLoad_customer({
|
|
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.companies,
|
|
as: 'company',
|
|
|
|
where: filter.company ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.company.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
legal_name: {
|
|
[Op.or]: filter.company.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.trucks,
|
|
as: 'truck',
|
|
|
|
where: filter.truck ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.truck.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
truck_number: {
|
|
[Op.or]: filter.truck.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.drivers,
|
|
as: 'driver',
|
|
|
|
where: filter.driver ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.driver.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
driver_name: {
|
|
[Op.or]: filter.driver.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.routes,
|
|
as: 'route',
|
|
|
|
where: filter.route ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.route.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
route_name: {
|
|
[Op.or]: filter.route.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.customers,
|
|
as: 'load_customer',
|
|
|
|
where: filter.load_customer ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.load_customer.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
customer_name: {
|
|
[Op.or]: filter.load_customer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.load_from_location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trips',
|
|
'load_from_location',
|
|
filter.load_from_location,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.deliver_to_location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trips',
|
|
'deliver_to_location',
|
|
filter.deliver_to_location,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.trip_reference) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trips',
|
|
'trip_reference',
|
|
filter.trip_reference,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'trips',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.planned_start_atRange) {
|
|
const [start, end] = filter.planned_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
planned_start_at: {
|
|
...where.planned_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
planned_start_at: {
|
|
...where.planned_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.planned_end_atRange) {
|
|
const [start, end] = filter.planned_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
planned_end_at: {
|
|
...where.planned_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
planned_end_at: {
|
|
...where.planned_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_start_atRange) {
|
|
const [start, end] = filter.actual_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_start_at: {
|
|
...where.actual_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_start_at: {
|
|
...where.actual_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_end_atRange) {
|
|
const [start, end] = filter.actual_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_end_at: {
|
|
...where.actual_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_end_at: {
|
|
...where.actual_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.car_countRange) {
|
|
const [start, end] = filter.car_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
car_count: {
|
|
...where.car_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
car_count: {
|
|
...where.car_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.planned_distance_kmRange) {
|
|
const [start, end] = filter.planned_distance_kmRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
planned_distance_km: {
|
|
...where.planned_distance_km,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
planned_distance_km: {
|
|
...where.planned_distance_km,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_distance_kmRange) {
|
|
const [start, end] = filter.actual_distance_kmRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_distance_km: {
|
|
...where.actual_distance_km,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_distance_km: {
|
|
...where.actual_distance_km,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.trip_status) {
|
|
where = {
|
|
...where,
|
|
trip_status: filter.trip_status,
|
|
};
|
|
}
|
|
|
|
if (filter.has_backload) {
|
|
where = {
|
|
...where,
|
|
has_backload: filter.has_backload,
|
|
};
|
|
}
|
|
|
|
if (filter.local_trip_slot) {
|
|
where = {
|
|
...where,
|
|
local_trip_slot: filter.local_trip_slot,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.trips.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(
|
|
'trips',
|
|
'trip_reference',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.trips.findAll({
|
|
attributes: [ 'id', 'trip_reference' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['trip_reference', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.trip_reference,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|