11import { Collection , ObjectId , UpdateResult } from "mongodb" ;
22import * as db from "../init/db" ;
3- import _ from "lodash" ;
43import { Config , PartialConfig } from "@monkeytype/schemas/configs" ;
54
6- const configLegacyProperties = [
7- "swapEscAndTab" ,
8- "quickTab" ,
9- "chartStyle" ,
10- "chartAverage10" ,
11- "chartAverage100" ,
12- "alwaysShowCPM" ,
13- "resultFilters" ,
14- "chartAccuracy" ,
15- "liveSpeed" ,
16- "extraTestColor" ,
17- "savedLayout" ,
18- "showTimerBar" ,
19- "showDiscordDot" ,
20- "maxConfidence" ,
21- "capsLockBackspace" ,
22- "showAvg" ,
23- "enableAds" ,
24- ] ;
25-
26- type DBConfig = {
5+ const configLegacyProperties : Record < string , "" > = {
6+ "config. swapEscAndTab" : " ",
7+ "config. quickTab" : " ",
8+ "config. chartStyle" : " ",
9+ "config. chartAverage10" : " ",
10+ "config. chartAverage100" : " ",
11+ "config. alwaysShowCPM" : " ",
12+ "config. resultFilters" : " ",
13+ "config. chartAccuracy" : " ",
14+ "config. liveSpeed" : " ",
15+ "config. extraTestColor" : " ",
16+ "config. savedLayout" : " ",
17+ "config. showTimerBar" : " ",
18+ "config. showDiscordDot" : " ",
19+ "config. maxConfidence" : " ",
20+ "config. capsLockBackspace" : " ",
21+ "config. showAvg" : " ",
22+ "config. enableAds" : " ",
23+ } ;
24+
25+ export type DBConfig = {
2726 _id : ObjectId ;
2827 uid : string ;
2928 config : PartialConfig ;
3029} ;
3130
32- // Export for use in tests
33- export const getConfigCollection = ( ) : Collection < DBConfig > =>
31+ const getConfigCollection = ( ) : Collection < DBConfig > =>
3432 db . collection < DBConfig > ( "configs" ) ;
3533
3634export async function saveConfig (
3735 uid : string ,
3836 config : Partial < Config >
3937) : Promise < UpdateResult > {
40- const configChanges = _ . mapKeys ( config , ( _value , key ) => `config.${ key } ` ) ;
41-
42- const unset = _ . fromPairs (
43- _ . map ( configLegacyProperties , ( key ) => [ `config.${ key } ` , "" ] )
44- ) as Record < string , "" > ;
38+ const configChanges = Object . fromEntries (
39+ Object . entries ( config ) . map ( ( [ key , value ] ) => [ `config.${ key } ` , value ] )
40+ ) ;
4541
4642 return await getConfigCollection ( ) . updateOne (
4743 { uid } ,
48- { $set : configChanges , $unset : unset } ,
44+ { $set : configChanges , $unset : configLegacyProperties } ,
4945 { upsert : true }
5046 ) ;
5147}
@@ -58,3 +54,7 @@ export async function getConfig(uid: string): Promise<DBConfig | null> {
5854export async function deleteConfig ( uid : string ) : Promise < void > {
5955 await getConfigCollection ( ) . deleteOne ( { uid } ) ;
6056}
57+
58+ export const __testing = {
59+ getConfigCollection,
60+ } ;
0 commit comments