29745/backend/src/db/api/weather_forecasts.js
2025-03-09 22:52:30 +00:00

496 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 Weather_forecastsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const weather_forecasts = await db.weather_forecasts.create(
{
id: data.id || undefined,
forecast_time: data.forecast_time || null,
temperature: data.temperature || null,
feels_like: data.feels_like || null,
humidity: data.humidity || null,
wind_speed: data.wind_speed || null,
wind_direction: data.wind_direction || null,
pressure: data.pressure || null,
rain_probability: data.rain_probability || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return weather_forecasts;
}
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_forecastsData = data.map((item, index) => ({
id: item.id || undefined,
forecast_time: item.forecast_time || null,
temperature: item.temperature || null,
feels_like: item.feels_like || null,
humidity: item.humidity || null,
wind_speed: item.wind_speed || null,
wind_direction: item.wind_direction || null,
pressure: item.pressure || null,
rain_probability: item.rain_probability || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const weather_forecasts = await db.weather_forecasts.bulkCreate(
weather_forecastsData,
{ transaction },
);
// For each item created, replace relation files
return weather_forecasts;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const weather_forecasts = await db.weather_forecasts.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.forecast_time !== undefined)
updatePayload.forecast_time = data.forecast_time;
if (data.temperature !== undefined)
updatePayload.temperature = data.temperature;
if (data.feels_like !== undefined)
updatePayload.feels_like = data.feels_like;
if (data.humidity !== undefined) updatePayload.humidity = data.humidity;
if (data.wind_speed !== undefined)
updatePayload.wind_speed = data.wind_speed;
if (data.wind_direction !== undefined)
updatePayload.wind_direction = data.wind_direction;
if (data.pressure !== undefined) updatePayload.pressure = data.pressure;
if (data.rain_probability !== undefined)
updatePayload.rain_probability = data.rain_probability;
updatePayload.updatedById = currentUser.id;
await weather_forecasts.update(updatePayload, { transaction });
return weather_forecasts;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const weather_forecasts = await db.weather_forecasts.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of weather_forecasts) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of weather_forecasts) {
await record.destroy({ transaction });
}
});
return weather_forecasts;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const weather_forecasts = await db.weather_forecasts.findByPk(id, options);
await weather_forecasts.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await weather_forecasts.destroy({
transaction,
});
return weather_forecasts;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const weather_forecasts = await db.weather_forecasts.findOne(
{ where },
{ transaction },
);
if (!weather_forecasts) {
return weather_forecasts;
}
const output = weather_forecasts.get({ plain: true });
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.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
forecast_time: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
forecast_time: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.forecast_timeRange) {
const [start, end] = filter.forecast_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
forecast_time: {
...where.forecast_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
forecast_time: {
...where.forecast_time,
[Op.lte]: end,
},
};
}
}
if (filter.temperatureRange) {
const [start, end] = filter.temperatureRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
temperature: {
...where.temperature,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
temperature: {
...where.temperature,
[Op.lte]: end,
},
};
}
}
if (filter.feels_likeRange) {
const [start, end] = filter.feels_likeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
feels_like: {
...where.feels_like,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
feels_like: {
...where.feels_like,
[Op.lte]: end,
},
};
}
}
if (filter.humidityRange) {
const [start, end] = filter.humidityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
humidity: {
...where.humidity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
humidity: {
...where.humidity,
[Op.lte]: end,
},
};
}
}
if (filter.wind_speedRange) {
const [start, end] = filter.wind_speedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
wind_speed: {
...where.wind_speed,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
wind_speed: {
...where.wind_speed,
[Op.lte]: end,
},
};
}
}
if (filter.wind_directionRange) {
const [start, end] = filter.wind_directionRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
wind_direction: {
...where.wind_direction,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
wind_direction: {
...where.wind_direction,
[Op.lte]: end,
},
};
}
}
if (filter.pressureRange) {
const [start, end] = filter.pressureRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
pressure: {
...where.pressure,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
pressure: {
...where.pressure,
[Op.lte]: end,
},
};
}
}
if (filter.rain_probabilityRange) {
const [start, end] = filter.rain_probabilityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rain_probability: {
...where.rain_probability,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rain_probability: {
...where.rain_probability,
[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_forecasts.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_forecasts', 'forecast_time', query),
],
};
}
const records = await db.weather_forecasts.findAll({
attributes: ['id', 'forecast_time'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['forecast_time', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.forecast_time,
}));
}
};