11import MonkeyError from "../utils/error" ;
22import * as db from "../init/db" ;
3- import { ObjectId , Filter } from "mongodb" ;
3+ import { ObjectId , Filter , Collection , WithId } from "mongodb" ;
44
55const MAX_PRESETS = 10 ;
6- const COLLECTION_NAME = "presets" ;
76
8- function getPresetKeyFilter ( uid : string , keyId : string ) : Filter < any > {
7+ type DBConfigPreset = SharedTypes . WithObjectId < SharedTypes . DBConfigPreset > ;
8+
9+ function getPresetKeyFilter (
10+ uid : string ,
11+ keyId : string
12+ ) : Filter < DBConfigPreset > {
913 return {
1014 _id : new ObjectId ( keyId ) ,
1115 uid,
@@ -16,10 +20,11 @@ interface PresetCreationResult {
1620 presetId : string ;
1721}
1822
19- // TODO: Add typings for presets/configs, must look into shared type declarations.
20- export async function getPresets ( uid : string ) : Promise < any [ ] > {
21- const presets = await db
22- . collection ( COLLECTION_NAME )
23+ export const getPresetsCollection = ( ) : Collection < WithId < DBConfigPreset > > =>
24+ db . collection < DBConfigPreset > ( "presets" ) ;
25+
26+ export async function getPresets ( uid : string ) : Promise < DBConfigPreset [ ] > {
27+ const presets = await getPresetsCollection ( )
2328 . find ( { uid } )
2429 . sort ( { timestamp : - 1 } )
2530 . toArray ( ) ; // this needs to be changed to later take patreon into consideration
@@ -36,9 +41,12 @@ export async function addPreset(
3641 throw new MonkeyError ( 409 , "Too many presets" ) ;
3742 }
3843
39- const preset = await db
40- . collection ( COLLECTION_NAME )
41- . insertOne ( { uid, name, config } as any ) ;
44+ const preset = await getPresetsCollection ( ) . insertOne ( {
45+ _id : new ObjectId ( ) ,
46+ uid,
47+ name,
48+ config,
49+ } ) ;
4250 return {
4351 presetId : preset . insertedId . toHexString ( ) ,
4452 } ;
@@ -54,24 +62,24 @@ export async function editPreset(
5462 config !== undefined && config !== null && Object . keys ( config ) . length > 0
5563 ? { name, config }
5664 : { name } ;
57- await db
58- . collection ( COLLECTION_NAME )
59- . updateOne ( getPresetKeyFilter ( uid , presetId ) , { $set : presetUpdates } ) ;
65+ await getPresetsCollection ( ) . updateOne ( getPresetKeyFilter ( uid , presetId ) , {
66+ $set : presetUpdates ,
67+ } ) ;
6068}
6169
6270export async function removePreset (
6371 uid : string ,
6472 presetId : string
6573) : Promise < void > {
66- const deleteResult = await db
67- . collection ( COLLECTION_NAME )
68- . deleteOne ( getPresetKeyFilter ( uid , presetId ) ) ;
74+ const deleteResult = await getPresetsCollection ( ) . deleteOne (
75+ getPresetKeyFilter ( uid , presetId )
76+ ) ;
6977
7078 if ( deleteResult . deletedCount === 0 ) {
7179 throw new MonkeyError ( 404 , "Preset not found" ) ;
7280 }
7381}
7482
7583export async function deleteAllPresets ( uid : string ) : Promise < void > {
76- await db . collection ( COLLECTION_NAME ) . deleteMany ( { uid } ) ;
84+ await getPresetsCollection ( ) . deleteMany ( { uid } ) ;
7785}
0 commit comments