import MonkeyError from "../utils/error"; import db from "../init/db"; import { ObjectId, Filter } from "mongodb"; const MAX_PRESETS = 10; const COLLECTION_NAME = "presets"; function getPresetKeyFilter(uid: string, keyId: string): Filter { return { _id: new ObjectId(keyId), uid, }; } interface PresetCreationResult { presetId: string; } class PresetDAO { // TODO: Add typings for presets/configs, must look into shared type declarations. static async getPresets(uid: string): Promise { const presets = await db .collection(COLLECTION_NAME) .find({ uid }) .sort({ timestamp: -1 }) .toArray(); // this needs to be changed to later take patreon into consideration return presets; } static async addPreset( uid: string, name: string, config: any ): Promise { const presets = await this.getPresets(uid); if (presets.length >= MAX_PRESETS) { throw new MonkeyError(409, "Too many presets"); } const preset = await db .collection(COLLECTION_NAME) .insertOne({ uid, name, config } as any); return { presetId: preset.insertedId.toHexString(), }; } static async editPreset( uid: string, presetId: string, name: string, config: any ): Promise { const presetUpdates = config && Object.keys(config).length > 0 ? { name, config } : { name }; await db .collection(COLLECTION_NAME) .updateOne(getPresetKeyFilter(uid, presetId), { $set: presetUpdates }); } static async removePreset(uid: string, presetId: string): Promise { const deleteResult = await db .collection(COLLECTION_NAME) .deleteOne(getPresetKeyFilter(uid, presetId)); if (deleteResult.deletedCount === 0) { throw new MonkeyError(404, "Preset not found"); } } } export default PresetDAO;