import _ from "lodash"; import db from "../init/db"; import { Filter, ObjectId, MatchKeysAndValues } from "mongodb"; import MonkeyError from "../utils/error"; const COLLECTION_NAME = "ape-keys"; function getApeKeyFilter( uid: string, keyId: string ): Filter { return { _id: new ObjectId(keyId), uid, }; } export async function getApeKeys(uid: string): Promise { return await db .collection(COLLECTION_NAME) .find({ uid }) .toArray(); } export async function getApeKey( keyId: string ): Promise { return await db .collection(COLLECTION_NAME) .findOne({ _id: new ObjectId(keyId) }); } export async function countApeKeysForUser(uid: string): Promise { const apeKeys = await getApeKeys(uid); return _.size(apeKeys); } export async function addApeKey(apeKey: MonkeyTypes.ApeKey): Promise { const insertionResult = await db .collection(COLLECTION_NAME) .insertOne(apeKey); return insertionResult.insertedId.toHexString(); } async function updateApeKey( uid: string, keyId: string, updates: MatchKeysAndValues ): Promise { const updateResult = await db .collection(COLLECTION_NAME) .updateOne(getApeKeyFilter(uid, keyId), { $inc: { useCount: _.has(updates, "lastUsedOn") ? 1 : 0 }, $set: _.pickBy(updates, (value) => !_.isNil(value)), }); if (updateResult.modifiedCount === 0) { throw new MonkeyError(404, "ApeKey not found"); } } export async function editApeKey( uid: string, keyId: string, name: string, enabled: boolean ): Promise { const apeKeyUpdates = { name, enabled, modifiedOn: Date.now(), }; await updateApeKey(uid, keyId, apeKeyUpdates); } export async function updateLastUsedOn( uid: string, keyId: string ): Promise { const apeKeyUpdates = { lastUsedOn: Date.now(), }; await updateApeKey(uid, keyId, apeKeyUpdates); } export async function deleteApeKey(uid: string, keyId: string): Promise { const deletionResult = await db .collection(COLLECTION_NAME) .deleteOne(getApeKeyFilter(uid, keyId)); if (deletionResult.deletedCount === 0) { throw new MonkeyError(404, "ApeKey not found"); } }