517 lines
12 KiB
JavaScript
517 lines
12 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 Service_areasDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const service_areas = await db.service_areas.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
country: data.country
|
|
||
|
|
null
|
|
,
|
|
|
|
province: data.province
|
|
||
|
|
null
|
|
,
|
|
|
|
city: data.city
|
|
||
|
|
null
|
|
,
|
|
|
|
area_name: data.area_name
|
|
||
|
|
null
|
|
,
|
|
|
|
latitude: data.latitude
|
|
||
|
|
null
|
|
,
|
|
|
|
longitude: data.longitude
|
|
||
|
|
null
|
|
,
|
|
|
|
is_featured: data.is_featured
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return service_areas;
|
|
}
|
|
|
|
|
|
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 service_areasData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
country: item.country
|
|
||
|
|
null
|
|
,
|
|
|
|
province: item.province
|
|
||
|
|
null
|
|
,
|
|
|
|
city: item.city
|
|
||
|
|
null
|
|
,
|
|
|
|
area_name: item.area_name
|
|
||
|
|
null
|
|
,
|
|
|
|
latitude: item.latitude
|
|
||
|
|
null
|
|
,
|
|
|
|
longitude: item.longitude
|
|
||
|
|
null
|
|
,
|
|
|
|
is_featured: item.is_featured
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const service_areas = await db.service_areas.bulkCreate(service_areasData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return service_areas;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const service_areas = await db.service_areas.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.country !== undefined) updatePayload.country = data.country;
|
|
|
|
|
|
if (data.province !== undefined) updatePayload.province = data.province;
|
|
|
|
|
|
if (data.city !== undefined) updatePayload.city = data.city;
|
|
|
|
|
|
if (data.area_name !== undefined) updatePayload.area_name = data.area_name;
|
|
|
|
|
|
if (data.latitude !== undefined) updatePayload.latitude = data.latitude;
|
|
|
|
|
|
if (data.longitude !== undefined) updatePayload.longitude = data.longitude;
|
|
|
|
|
|
if (data.is_featured !== undefined) updatePayload.is_featured = data.is_featured;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await service_areas.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return service_areas;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const service_areas = await db.service_areas.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of service_areas) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of service_areas) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return service_areas;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const service_areas = await db.service_areas.findByPk(id, options);
|
|
|
|
await service_areas.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await service_areas.destroy({
|
|
transaction
|
|
});
|
|
|
|
return service_areas;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const service_areas = await db.service_areas.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!service_areas) {
|
|
return service_areas;
|
|
}
|
|
|
|
const output = service_areas.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.geo_pages_service_area = await service_areas.getGeo_pages_service_area({
|
|
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.country) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'service_areas',
|
|
'country',
|
|
filter.country,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.province) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'service_areas',
|
|
'province',
|
|
filter.province,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.city) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'service_areas',
|
|
'city',
|
|
filter.city,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.area_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'service_areas',
|
|
'area_name',
|
|
filter.area_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.latitudeRange) {
|
|
const [start, end] = filter.latitudeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
latitude: {
|
|
...where.latitude,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
latitude: {
|
|
...where.latitude,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.longitudeRange) {
|
|
const [start, end] = filter.longitudeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
longitude: {
|
|
...where.longitude,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
longitude: {
|
|
...where.longitude,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_featured) {
|
|
where = {
|
|
...where,
|
|
is_featured: filter.is_featured,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.service_areas.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(
|
|
'service_areas',
|
|
'area_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.service_areas.findAll({
|
|
attributes: [ 'id', 'area_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['area_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.area_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|