rafaell-lycan/sabesp-mananciais-api

View on GitHub
src/api/routes/v1/index.ts

Summary

Maintainability
B
4 hrs
Test Coverage
import { Router } from 'express'
import { formatDate, validateDate } from '../../../utils/dates';
import { getDamnsInformation } from '../../../services/sabesp';
import { mapV1 } from '../../../utils/mapper';
import { logger } from '../../../utils/logger';

const routesV1 = Router()

async function getMappedDataByDate(date: string){
  const data = await getDamnsInformation(date);

  return mapV1(data)
}

routesV1.get('/', async (_req, res) => {
  const today = formatDate(new Date())
  const data = await getMappedDataByDate(today);

  res.json(data);
})

routesV1.get('/:date', async (req, res, next) => {
  const { date } = req.params;

  try {
    if(date && validateDate(date)) {
      const data = await getMappedDataByDate(date);

      res.json(data);
    }
  } catch (err) {
    logger.error(err);
    // throw new InternalServerError(err.message);
    next(err)
  }
})

export { routesV1 }