view client/src/store/importschedule.js @ 2034:cc3ad4aa9b2f unify_imports

importschedule: add store
author Thomas Junk <thomas.junk@intevation.de>
date Fri, 25 Jan 2019 11:09:46 +0100
parents
children 4a0d4e50768d
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"
};

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

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

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: null,
    username: "",
    password: "",
    LOS: 1,
    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, id, cron, url, attributes } = payload;
      const eMailNotification = payload["send-email"];
      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);
      }
      if (attributes) {
        let { insecure, username, password, los, depth } = attributes;
        let sortBy = attributes["sort-by"];
        let minWidth = attributes["min-width"];
        let maxWidth = attributes["max-width"];
        let sourceOrganization = attributes["source-organization"];
        const featureType = attributes["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
};