zappyzep/backend/src/data/GuildReminders.ts
2024-04-09 20:57:18 +03:00

56 lines
1.4 KiB
TypeScript

import { Repository } from "typeorm";
import { BaseGuildRepository } from "./BaseGuildRepository.js";
import { dataSource } from "./dataSource.js";
import { Reminder } from "./entities/Reminder.js";
export class GuildReminders extends BaseGuildRepository {
private reminders: Repository<Reminder>;
constructor(guildId) {
super(guildId);
this.reminders = dataSource.getRepository(Reminder);
}
async getDueReminders(): Promise<Reminder[]> {
return this.reminders
.createQueryBuilder()
.where("guild_id = :guildId", { guildId: this.guildId })
.andWhere("remind_at <= NOW()")
.getMany();
}
async getRemindersByUserId(userId: string): Promise<Reminder[]> {
return this.reminders.find({
where: {
guild_id: this.guildId,
user_id: userId,
},
});
}
find(id: number) {
return this.reminders.findOne({
where: { id },
});
}
async delete(id) {
await this.reminders.delete({
guild_id: this.guildId,
id,
});
}
async add(userId: string, channelId: string, remindAt: string, body: string, created_at: string) {
const result = await this.reminders.insert({
guild_id: this.guildId,
user_id: userId,
channel_id: channelId,
remind_at: remindAt,
body,
created_at,
});
return (await this.find(result.identifiers[0].id))!;
}
}