594 lines
15 KiB
JavaScript
594 lines
15 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 Weather_snapshotsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const weather_snapshots = await db.weather_snapshots.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
observed_datetime: data.observed_datetime
|
|
||
|
|
null
|
|
,
|
|
|
|
temperature_c: data.temperature_c
|
|
||
|
|
null
|
|
,
|
|
|
|
temperature_f: data.temperature_f
|
|
||
|
|
null
|
|
,
|
|
|
|
condition_text: data.condition_text
|
|
||
|
|
null
|
|
,
|
|
|
|
icon_code: data.icon_code
|
|
||
|
|
null
|
|
,
|
|
|
|
wind_kph: data.wind_kph
|
|
||
|
|
null
|
|
,
|
|
|
|
humidity_percent: data.humidity_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await weather_snapshots.setTown( data.town || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return weather_snapshots;
|
|
}
|
|
|
|
|
|
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 weather_snapshotsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
observed_datetime: item.observed_datetime
|
|
||
|
|
null
|
|
,
|
|
|
|
temperature_c: item.temperature_c
|
|
||
|
|
null
|
|
,
|
|
|
|
temperature_f: item.temperature_f
|
|
||
|
|
null
|
|
,
|
|
|
|
condition_text: item.condition_text
|
|
||
|
|
null
|
|
,
|
|
|
|
icon_code: item.icon_code
|
|
||
|
|
null
|
|
,
|
|
|
|
wind_kph: item.wind_kph
|
|
||
|
|
null
|
|
,
|
|
|
|
humidity_percent: item.humidity_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const weather_snapshots = await db.weather_snapshots.bulkCreate(weather_snapshotsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return weather_snapshots;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const weather_snapshots = await db.weather_snapshots.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.observed_datetime !== undefined) updatePayload.observed_datetime = data.observed_datetime;
|
|
|
|
|
|
if (data.temperature_c !== undefined) updatePayload.temperature_c = data.temperature_c;
|
|
|
|
|
|
if (data.temperature_f !== undefined) updatePayload.temperature_f = data.temperature_f;
|
|
|
|
|
|
if (data.condition_text !== undefined) updatePayload.condition_text = data.condition_text;
|
|
|
|
|
|
if (data.icon_code !== undefined) updatePayload.icon_code = data.icon_code;
|
|
|
|
|
|
if (data.wind_kph !== undefined) updatePayload.wind_kph = data.wind_kph;
|
|
|
|
|
|
if (data.humidity_percent !== undefined) updatePayload.humidity_percent = data.humidity_percent;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await weather_snapshots.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.town !== undefined) {
|
|
await weather_snapshots.setTown(
|
|
|
|
data.town,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return weather_snapshots;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const weather_snapshots = await db.weather_snapshots.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of weather_snapshots) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of weather_snapshots) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return weather_snapshots;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const weather_snapshots = await db.weather_snapshots.findByPk(id, options);
|
|
|
|
await weather_snapshots.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await weather_snapshots.destroy({
|
|
transaction
|
|
});
|
|
|
|
return weather_snapshots;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const weather_snapshots = await db.weather_snapshots.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!weather_snapshots) {
|
|
return weather_snapshots;
|
|
}
|
|
|
|
const output = weather_snapshots.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.town = await weather_snapshots.getTown({
|
|
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.towns,
|
|
as: 'town',
|
|
|
|
where: filter.town ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.town.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.town.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.condition_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'weather_snapshots',
|
|
'condition_text',
|
|
filter.condition_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.icon_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'weather_snapshots',
|
|
'icon_code',
|
|
filter.icon_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.observed_datetimeRange) {
|
|
const [start, end] = filter.observed_datetimeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
observed_datetime: {
|
|
...where.observed_datetime,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
observed_datetime: {
|
|
...where.observed_datetime,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.temperature_cRange) {
|
|
const [start, end] = filter.temperature_cRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
temperature_c: {
|
|
...where.temperature_c,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
temperature_c: {
|
|
...where.temperature_c,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.temperature_fRange) {
|
|
const [start, end] = filter.temperature_fRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
temperature_f: {
|
|
...where.temperature_f,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
temperature_f: {
|
|
...where.temperature_f,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.wind_kphRange) {
|
|
const [start, end] = filter.wind_kphRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
wind_kph: {
|
|
...where.wind_kph,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
wind_kph: {
|
|
...where.wind_kph,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.humidity_percentRange) {
|
|
const [start, end] = filter.humidity_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
humidity_percent: {
|
|
...where.humidity_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
humidity_percent: {
|
|
...where.humidity_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.weather_snapshots.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(
|
|
'weather_snapshots',
|
|
'condition_text',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.weather_snapshots.findAll({
|
|
attributes: [ 'id', 'condition_text' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['condition_text', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.condition_text,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|