import { getRepository, Repository } from "typeorm"; import { Webhook } from "./entities/Webhook"; import { BaseRepository } from "./BaseRepository"; import { decrypt, encrypt } from "../utils/crypt"; export class Webhooks extends BaseRepository { repository: Repository = getRepository(Webhook); protected async _processEntityFromDB(entity) { entity.token = await decrypt(entity.token); return entity; } protected async _processEntityToDB(entity) { entity.token = await encrypt(entity.token); return entity; } async findByChannelId(channelId: string): Promise { const result = await this.repository.findOne({ where: { channel_id: channelId, }, }); return result ? this.processEntityFromDB(result) : null; } async create(data: Partial): Promise { data = await this.processEntityToDB(data); await this.repository.insert(data); } async delete(id: string): Promise { await this.repository.delete({ id }); } }