571 lines
14 KiB
JavaScript
571 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 Arrival_eventsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const arrival_events = await db.arrival_events.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
seller_latitude: data.seller_latitude
|
|
||
|
|
null
|
|
,
|
|
|
|
seller_longitude: data.seller_longitude
|
|
||
|
|
null
|
|
,
|
|
|
|
distance_meters: data.distance_meters
|
|
||
|
|
null
|
|
,
|
|
|
|
within_50m: data.within_50m
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
detected_at: data.detected_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await arrival_events.setService_request( data.service_request || null, {
|
|
transaction,
|
|
});
|
|
|
|
await arrival_events.setSeller( data.seller || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return arrival_events;
|
|
}
|
|
|
|
|
|
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 arrival_eventsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
seller_latitude: item.seller_latitude
|
|
||
|
|
null
|
|
,
|
|
|
|
seller_longitude: item.seller_longitude
|
|
||
|
|
null
|
|
,
|
|
|
|
distance_meters: item.distance_meters
|
|
||
|
|
null
|
|
,
|
|
|
|
within_50m: item.within_50m
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
detected_at: item.detected_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const arrival_events = await db.arrival_events.bulkCreate(arrival_eventsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return arrival_events;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const arrival_events = await db.arrival_events.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.seller_latitude !== undefined) updatePayload.seller_latitude = data.seller_latitude;
|
|
|
|
|
|
if (data.seller_longitude !== undefined) updatePayload.seller_longitude = data.seller_longitude;
|
|
|
|
|
|
if (data.distance_meters !== undefined) updatePayload.distance_meters = data.distance_meters;
|
|
|
|
|
|
if (data.within_50m !== undefined) updatePayload.within_50m = data.within_50m;
|
|
|
|
|
|
if (data.detected_at !== undefined) updatePayload.detected_at = data.detected_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await arrival_events.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.service_request !== undefined) {
|
|
await arrival_events.setService_request(
|
|
|
|
data.service_request,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.seller !== undefined) {
|
|
await arrival_events.setSeller(
|
|
|
|
data.seller,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return arrival_events;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const arrival_events = await db.arrival_events.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of arrival_events) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of arrival_events) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return arrival_events;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const arrival_events = await db.arrival_events.findByPk(id, options);
|
|
|
|
await arrival_events.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await arrival_events.destroy({
|
|
transaction
|
|
});
|
|
|
|
return arrival_events;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const arrival_events = await db.arrival_events.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!arrival_events) {
|
|
return arrival_events;
|
|
}
|
|
|
|
const output = arrival_events.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.service_request = await arrival_events.getService_request({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.seller = await arrival_events.getSeller({
|
|
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.service_requests,
|
|
as: 'service_request',
|
|
|
|
where: filter.service_request ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.service_request.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.service_request.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'seller',
|
|
|
|
where: filter.seller ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.seller.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.seller.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.seller_latitudeRange) {
|
|
const [start, end] = filter.seller_latitudeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
seller_latitude: {
|
|
...where.seller_latitude,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
seller_latitude: {
|
|
...where.seller_latitude,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.seller_longitudeRange) {
|
|
const [start, end] = filter.seller_longitudeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
seller_longitude: {
|
|
...where.seller_longitude,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
seller_longitude: {
|
|
...where.seller_longitude,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.distance_metersRange) {
|
|
const [start, end] = filter.distance_metersRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
distance_meters: {
|
|
...where.distance_meters,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
distance_meters: {
|
|
...where.distance_meters,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.detected_atRange) {
|
|
const [start, end] = filter.detected_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
detected_at: {
|
|
...where.detected_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
detected_at: {
|
|
...where.detected_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.within_50m) {
|
|
where = {
|
|
...where,
|
|
within_50m: filter.within_50m,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.arrival_events.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(
|
|
'arrival_events',
|
|
'distance_meters',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.arrival_events.findAll({
|
|
attributes: [ 'id', 'distance_meters' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['distance_meters', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.distance_meters,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|