38803-vm/backend/src/db/api/tracker_devices.js
2026-02-27 03:22:43 +00:00

578 lines
14 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 Tracker_devicesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tracker_devices = await db.tracker_devices.create(
{
id: data.id || undefined,
provider_name: data.provider_name
||
null
,
device_name: data.device_name
||
null
,
device_imei: data.device_imei
||
null
,
external_vehicle_key: data.external_vehicle_key
||
null
,
live_map_url: data.live_map_url
||
null
,
is_active: data.is_active
||
false
,
last_ping_at: data.last_ping_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await tracker_devices.setCompany( data.company || null, {
transaction,
});
await tracker_devices.setTruck( data.truck || null, {
transaction,
});
return tracker_devices;
}
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 tracker_devicesData = data.map((item, index) => ({
id: item.id || undefined,
provider_name: item.provider_name
||
null
,
device_name: item.device_name
||
null
,
device_imei: item.device_imei
||
null
,
external_vehicle_key: item.external_vehicle_key
||
null
,
live_map_url: item.live_map_url
||
null
,
is_active: item.is_active
||
false
,
last_ping_at: item.last_ping_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const tracker_devices = await db.tracker_devices.bulkCreate(tracker_devicesData, { transaction });
// For each item created, replace relation files
return tracker_devices;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tracker_devices = await db.tracker_devices.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.provider_name !== undefined) updatePayload.provider_name = data.provider_name;
if (data.device_name !== undefined) updatePayload.device_name = data.device_name;
if (data.device_imei !== undefined) updatePayload.device_imei = data.device_imei;
if (data.external_vehicle_key !== undefined) updatePayload.external_vehicle_key = data.external_vehicle_key;
if (data.live_map_url !== undefined) updatePayload.live_map_url = data.live_map_url;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
if (data.last_ping_at !== undefined) updatePayload.last_ping_at = data.last_ping_at;
updatePayload.updatedById = currentUser.id;
await tracker_devices.update(updatePayload, {transaction});
if (data.company !== undefined) {
await tracker_devices.setCompany(
data.company,
{ transaction }
);
}
if (data.truck !== undefined) {
await tracker_devices.setTruck(
data.truck,
{ transaction }
);
}
return tracker_devices;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tracker_devices = await db.tracker_devices.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of tracker_devices) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of tracker_devices) {
await record.destroy({transaction});
}
});
return tracker_devices;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tracker_devices = await db.tracker_devices.findByPk(id, options);
await tracker_devices.update({
deletedBy: currentUser.id
}, {
transaction,
});
await tracker_devices.destroy({
transaction
});
return tracker_devices;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const tracker_devices = await db.tracker_devices.findOne(
{ where },
{ transaction },
);
if (!tracker_devices) {
return tracker_devices;
}
const output = tracker_devices.get({plain: true});
output.tracker_positions_device = await tracker_devices.getTracker_positions_device({
transaction
});
output.company = await tracker_devices.getCompany({
transaction
});
output.truck = await tracker_devices.getTruck({
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}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.provider_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracker_devices',
'provider_name',
filter.provider_name,
),
};
}
if (filter.device_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracker_devices',
'device_name',
filter.device_name,
),
};
}
if (filter.device_imei) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracker_devices',
'device_imei',
filter.device_imei,
),
};
}
if (filter.external_vehicle_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracker_devices',
'external_vehicle_key',
filter.external_vehicle_key,
),
};
}
if (filter.live_map_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracker_devices',
'live_map_url',
filter.live_map_url,
),
};
}
if (filter.last_ping_atRange) {
const [start, end] = filter.last_ping_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
last_ping_at: {
...where.last_ping_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
last_ping_at: {
...where.last_ping_at,
[Op.lte]: end,
},
};
}
}
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.tracker_devices.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(
'tracker_devices',
'device_name',
query,
),
],
};
}
const records = await db.tracker_devices.findAll({
attributes: [ 'id', 'device_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['device_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.device_name,
}));
}
};