2023-12-24 10:21:40 -05:00
|
|
|
import DatabaseServer from '../components/DatabaseServer.js';
|
|
|
|
import {Model, DataTypes} from 'sequelize';
|
|
|
|
|
|
|
|
class dailyMsgs extends Model {
|
|
|
|
declare public day: number;
|
|
|
|
declare public total: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DailyMsgsSvc {
|
|
|
|
private model: typeof dailyMsgs;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.model = dailyMsgs;
|
|
|
|
this.model.init({
|
|
|
|
day: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
autoIncrement: true,
|
|
|
|
unique: true,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
total: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
allowNull: false,
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
tableName: 'dailymsgs',
|
|
|
|
createdAt: false,
|
|
|
|
updatedAt: false,
|
|
|
|
sequelize: DatabaseServer.seq
|
|
|
|
})
|
|
|
|
this.model.sync();
|
|
|
|
}
|
2024-01-16 06:28:03 -05:00
|
|
|
nukeDays = async()=>await this.model.destroy({truncate: true});
|
|
|
|
fetchDays = async()=>await this.model.findAll();
|
2023-12-24 10:21:40 -05:00
|
|
|
async newDay(formattedDate:number, total:number) {
|
|
|
|
if (await this.model.findOne({where: {day: formattedDate}})) return console.log('This day already exists!')
|
|
|
|
return await this.model.create({day: formattedDate, total: total});
|
|
|
|
// Save previous day's total messages into database when a new day starts.
|
|
|
|
}
|
2024-01-16 06:28:03 -05:00
|
|
|
updateDay = async(formattedDate:number, total:number)=>await this.model.update({total: total}, {where: {day: formattedDate}});
|
|
|
|
// THIS IS FOR DEVELOPMENT PURPOSES ONLY, NOT TO BE USED IN LIVE ENVIRONMENT!
|
2023-12-24 10:21:40 -05:00
|
|
|
}
|