All files / src/modules/config dynamic.ts

63.33% Statements 19/30
0% Branches 0/2
27.27% Functions 3/11
63.33% Lines 19/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72        7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x     7x 7x 7x         7x     7x                                                                               7x    
import Redis from "ioredis"
 
import { redis } from "../../redis"
 
export enum VConfig {
  CurrentAccountId = "current-account-id",
  RoleBudgetBillsId = "role-budget-bills-id",
  RoleBudgetLeftoversId = "role-budget-leftovers-id",
  AutoImportCron = "auto-import-cron",
  BudgetSumUpCron = "budget-sum-up-cron",
  Notifier = "notifier",
  NotifierDiscordWebhook = "discord-webhook",
  NotifierGotifyUrl = "gotify-url",
  NotifierGotifyToken = "gotify-token",
  NotifierGotifyUserToken = "gotify-user-token",
  NotifierGotifyApplicationId = "gotify-application-id",
  Locale = "locale",
}
 
export enum AConfig {
  HiddenCategories = "hidden-categories",
  HiddenBudgets = "hidden-budgets",
}
 
class DynamicConfig {
  private client: Redis
  private readonly namespace = "sparkleft:config"
 
  constructor(client: Redis) {
    this.client = client
  }
 
  private buildKey(key: VConfig | AConfig): string {
    return `${this.namespace}:${key}`
  }
 
  async get(key: VConfig): Promise<string | null> {
    return this.client.get(this.buildKey(key))
  }
 
  async set(key: VConfig, value: string): Promise<void> {
    if (!value) {
      await this.client.del(this.buildKey(key))
      return
    }
    await this.client.set(this.buildKey(key), value)
  }
 
  async delete(key: VConfig): Promise<void> {
    await this.client.del(this.buildKey(key))
  }
 
  async has(key: VConfig): Promise<boolean> {
    return (await this.client.exists(this.buildKey(key))) === 1
  }
 
  async lrem(key: AConfig, count: number, value: string): Promise<number> {
    return this.client.lrem(this.buildKey(key), count, value)
  }
 
  async lrange(key: AConfig, start: number, stop: number): Promise<string[]> {
    return this.client.lrange(this.buildKey(key), start, stop)
  }
 
  async rpush(key: AConfig, value: string): Promise<number> {
    return this.client.rpush(this.buildKey(key), value)
  }
}
 
const instance = new DynamicConfig(redis)
export default instance