view client/src/store/importschedule.js @ 2295:b6cd295bf75d

import_schedule: default for sort-by: 'hydro_scamin'
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 18 Feb 2019 11:16:13 +0100
parents 1baae9d31b3d
children 716c0eba89a2
line wrap: on
line source

/* This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Thomas Junk <thomas.junk@intevation.de>
 */

import Vue from "vue";
import { HTTP } from "@/lib/http";

/* eslint-disable no-unused-vars */
/* eslint-disable no-unreachable */

const IMPORTTYPES = {
  BOTTLENECK: "bottleneck",
  WATERWAYAXIS: "waterwayaxis",
  GAUGEMEASUREMENT: "gaugemeasurement",
  FAIRWAYAVAILABILITY: "fairwayavailability",
  WATERWAYAREA: "waterwayarea",
  FAIRWAYDIMENSION: "fairwaydimension",
  WATERWAYGAUGES: "waterwaygauges",
  DISTANCEMARKSVIRTUAL: "distancemarksvirtual",
  DISTANCEMARKSASHORE: "distancemarksashore"
};

const KINDIMPORTTYPE = {
  bn: "bottleneck",
  fa: "fairwayavailability",
  gm: "gaugemeasurement",
  wx: "waterwayaxis",
  wa: "waterwayarea",
  fd: "fairwaydimension",
  wg: "waterwaygauge",
  dmv: "distancemarksvirtual",
  dma: "distancemarksashore"
};

const IMPORTTYPEKIND = {
  bottleneck: "bn",
  fairwayavailability: "fa",
  gaugemeasurement: "gm",
  waterwayaxis: "wx",
  waterwayarea: "wa",
  fairwaydimension: "fd",
  waterwaygauges: "wg",
  distancemarksvirtual: "dmv",
  distancemarksashore: "dma"
};

const initializeCurrentSchedule = () => {
  return {
    id: null,
    importType: null,
    schedule: null,
    import_: null,
    importSource: null,
    eMailNotification: false,
    scheduled: false,
    easyCron: true,
    cronString: "* * * * ",
    cronMode: "",
    minutes: null,
    month: null,
    hour: null,
    day: null,
    dayOfMonth: null,
    simple: null,
    url: null,
    insecure: false,
    triggerActive: true,
    featureType: null,
    sortBy: "hydro_scamin",
    username: "",
    password: "",
    LOS: 3,
    minWidth: null,
    maxWidth: null,
    depth: null,
    sourceOrganization: null
  };
};

// initial state
const init = () => {
  return {
    schedules: [],
    importScheduleDetailVisible: false,
    currentSchedule: initializeCurrentSchedule()
  };
};

const importschedule = {
  init,
  namespaced: true,
  state: init(),
  mutations: {
    clearCurrentSchedule: state => {
      state.currentSchedule = initializeCurrentSchedule();
    },
    setImportScheduleDetailInvisible: state => {
      state.importScheduleDetailVisible = false;
    },
    setImportScheduleDetailVisible: state => {
      state.importScheduleDetailVisible = true;
    },
    setSchedules: (state, schedules) => {
      state.schedules = schedules;
    },
    unmarshallCurrentSchedule: (state, payload) => {
      const { kind, config, id } = payload;
      const eMailNotification = config["send-email"];
      const { cron, url } = config;
      Vue.set(state.currentSchedule, "import_", KINDIMPORTTYPE[kind]);
      Vue.set(state.currentSchedule, "id", id);
      if (cron) {
        Vue.set(state.currentSchedule, "scheduled", true);
        Vue.set(state.currentSchedule, "easyCron", false);
        Vue.set(state.currentSchedule, "cronString", cron);
      }
      if (eMailNotification) {
        Vue.set(state.currentSchedule, "eMailNotification", eMailNotification);
      }
      if (url) {
        Vue.set(state.currentSchedule, "url", url);
      }
      let { insecure, username, password, los, depth } = config;
      let sortBy = config["sort-by"];
      let minWidth = config["min-width"];
      let maxWidth = config["max-width"];
      let sourceOrganization = config["source-organization"];
      const featureType = config["feature-type"];
      insecure = insecure == "true";
      if (insecure) {
        Vue.set(state.currentSchedule, "insecure", insecure);
      }
      if (featureType) {
        Vue.set(state.currentSchedule, "featureType", featureType);
      }
      if (sortBy) {
        Vue.set(state.currentSchedule, "sortBy", sortBy);
      }
      if (username) {
        Vue.set(state.currentSchedule, "username", username);
      }
      if (password) {
        Vue.set(state.currentSchedule, "password", password);
      }
      if (los) {
        Vue.set(state.currentSchedule, "LOS", los);
      }
      if (minWidth) {
        Vue.set(state.currentSchedule, "minWidth", minWidth);
      }
      if (maxWidth) {
        Vue.set(state.currentSchedule, "maxWidth", maxWidth);
      }
      if (depth) {
        Vue.set(state.currentSchedule, "depth", depth);
      }
      if (sourceOrganization) {
        Vue.set(
          state.currentSchedule,
          "sourceOrganization",
          sourceOrganization
        );
      }
    }
  },
  actions: {
    loadSchedule({ commit }, id) {
      return new Promise((resolve, reject) => {
        HTTP.get("/imports/config/" + id, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("unmarshallCurrentSchedule", response.data);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    updateCurrentSchedule({ commit }, payload) {
      const { data, id } = payload;
      return new Promise((resolve, reject) => {
        HTTP.patch("imports/config/" + id, data, {
          headers: {
            "X-Gemma-Auth": localStorage.getItem("token")
          }
        })
          .then(response => {
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    saveCurrentSchedule({ commit }, data) {
      return new Promise((resolve, reject) => {
        HTTP.post("imports/config", data, {
          headers: {
            "X-Gemma-Auth": localStorage.getItem("token")
          }
        })
          .then(response => {
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    loadSchedules({ commit }) {
      return new Promise((resolve, reject) => {
        HTTP.get("/imports/config", {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("setSchedules", response.data);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    deleteSchedule({ commit }, id) {
      return new Promise((resolve, reject) => {
        HTTP.delete("imports/config/" + id, {
          headers: {
            "X-Gemma-Auth": localStorage.getItem("token")
          }
        })
          .then(response => {
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    triggerImport({ commit }, { type, data }) {
      return new Promise((resolve, reject) => {
        HTTP.post("imports/" + type, data, {
          headers: {
            "X-Gemma-Auth": localStorage.getItem("token")
          }
        })
          .then(response => {
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
};

export {
  importschedule,
  initializeCurrentSchedule,
  IMPORTTYPES,
  IMPORTTYPEKIND
};