2021-06-10 02:51:59 +03:00
|
|
|
const { Op } = require('sequelize');
|
|
|
|
const Weather = require('../models/Weather');
|
2021-06-22 15:49:00 +03:00
|
|
|
const Logger = require('./Logger');
|
|
|
|
const logger = new Logger();
|
2021-11-05 01:39:35 +03:00
|
|
|
const loadConfig = require('./loadConfig');
|
2021-06-10 02:51:59 +03:00
|
|
|
|
|
|
|
const clearWeatherData = async () => {
|
2021-11-05 01:39:35 +03:00
|
|
|
const { WEATHER_API_KEY: secret } = await loadConfig();
|
|
|
|
|
2021-06-10 02:51:59 +03:00
|
|
|
const weather = await Weather.findOne({
|
2021-11-05 01:39:35 +03:00
|
|
|
order: [['createdAt', 'DESC']],
|
2021-06-10 02:51:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (weather) {
|
|
|
|
await Weather.destroy({
|
|
|
|
where: {
|
|
|
|
id: {
|
2021-11-05 01:39:35 +03:00
|
|
|
[Op.lt]: weather.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-06-10 02:51:59 +03:00
|
|
|
}
|
|
|
|
|
2021-11-05 01:39:35 +03:00
|
|
|
if (secret) {
|
|
|
|
logger.log('Old weather data was deleted');
|
|
|
|
}
|
|
|
|
};
|
2021-06-10 02:51:59 +03:00
|
|
|
|
2021-11-05 01:39:35 +03:00
|
|
|
module.exports = clearWeatherData;
|