zappyzep/backend/src/data/GuildReminders.ts

57 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-07-01 12:17:45 +00:00
import { Repository } from "typeorm";
import { BaseGuildRepository } from "./BaseGuildRepository";
2023-07-01 12:17:45 +00:00
import { dataSource } from "./dataSource";
2019-02-20 00:01:14 +02:00
import { Reminder } from "./entities/Reminder";
export class GuildReminders extends BaseGuildRepository {
2019-02-20 00:01:14 +02:00
private reminders: Repository<Reminder>;
constructor(guildId) {
super(guildId);
2023-07-01 12:17:45 +00:00
this.reminders = dataSource.getRepository(Reminder);
2019-02-20 00:01:14 +02:00
}
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) {
2023-07-01 12:17:45 +00:00
return this.reminders.findOne({
where: { id },
});
}
2019-02-20 00:01:14 +02:00
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({
2019-02-20 00:01:14 +02:00
guild_id: this.guildId,
user_id: userId,
channel_id: channelId,
remind_at: remindAt,
body,
created_at,
2019-02-20 00:01:14 +02:00
});
return (await this.find(result.identifiers[0].id))!;
2019-02-20 00:01:14 +02:00
}
}