cityssm/lottery-licence-manager

View on GitHub
handlers/locations-get/edit.ts

Summary

Maintainability
A
0 mins
Test Coverage
import type { RequestHandler } from "express";

import * as configFunctions from "../../helpers/functions.config.js";


import * as dateTimeFns from "@cityssm/expressjs-server-js/dateTimeFns.js";

import { getLicences } from "../../helpers/licencesDB/getLicences.js";
import { getLocation } from "../../helpers/licencesDB/getLocation.js";


const urlPrefix = configFunctions.getProperty("reverseProxy.urlPrefix");


export const handler: RequestHandler = (request, response, next) => {

  const locationID = Number(request.params.locationID);

  if (Number.isNaN(locationID)) {
    return next();
  }

  const location = getLocation(locationID, request.session);

  if (!location) {
    return response.redirect(urlPrefix + "/locations/?error=locationNotFound");
  }

  if (!location.canUpdate) {
    return response.redirect(urlPrefix + "/locations/" + locationID.toString() + "/?error=accessDenied-noUpdate");
  }

  const licences = getLicences({
    locationID
  }, request.session, {
      includeOrganization: true,
      limit: -1
    }).licences;

  return response.render("location-edit", {
    headTitle: location.locationDisplayName,
    location,
    licences,
    currentDateInteger: dateTimeFns.dateToInteger(new Date()),
    isCreate: false
  });
};


export default handler;