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