All files / src/endpoints setNotifierField.ts

16.66% Statements 2/12
0% Branches 0/2
0% Functions 0/2
16.66% Lines 2/12

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            2x   2x                                                    
import { Request, Response } from "express"
import pino from "pino"
 
import DynamicConfig, { VConfig } from "../modules/config/dynamic"
import { Notifiers } from "../modules/notifiers/types"
 
const logger = pino()
 
const valid = [
  VConfig.NotifierDiscordWebhook,
  VConfig.NotifierGotifyApplicationId,
  VConfig.NotifierGotifyToken,
  VConfig.NotifierGotifyUrl,
  VConfig.NotifierGotifyUserToken,
]
 
function isNotifierField(value: string): value is VConfig {
  return valid.includes(value as VConfig)
}
 
export async function setNotifierField(req: Request<{ field: Notifiers }, unknown, { value: string }>, res: Response) {
  const { field } = req.params
  const { value } = req.body
  logger.info("=================================== Setting Notifier Field ===================================")
 
  if (!isNotifierField(field)) {
    logger.error("Invalid notifier field %s", field)
    return res.status(400).send({ message: "Invalid notifier field" })
  }
 
  logger.info("Setting notifier field %s", field)
  await DynamicConfig.set(field, value)
  return res.status(201).send({ field, value })
}