view client/src/store/imports.js @ 1858:108a049c8394

importqueue: fairwaydimensions removed typos. should work now
author Thomas Junk <thomas.junk@intevation.de>
date Thu, 17 Jan 2019 12:42:19 +0100
parents 9141abe7c75a
children 3bf2e5a91e50
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 { HTTP } from "@/lib/http";
import Vue from "vue";

/* eslint-disable no-unused-vars */
/* eslint-disable no-unreachable */
const STATES = {
  NEEDSAPPROVAL: "pending",
  APPROVED: "accepted",
  REJECTED: "declined"
};

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

const SCHEDULES = {
  DAILY: "daily",
  MONTHLY: "monthly"
};

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

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

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: null,
    minWidth: null,
    maxWidth: null,
    depth: null,
    sourceOrganization: null
  };
};

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

const imports = {
  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;
    },
    setImports: (state, imports) => {
      state.imports = imports;
    },
    setStaging: (state, staging) => {
      const enriched = staging.map(x => {
        return { ...x, status: STATES.NEEDSAPPROVAL };
      });
      state.staging = enriched;
    },
    setImportToReview: (state, id) => {
      if (!isNaN(parseFloat(id)) && isFinite(id)) {
        state.importToReview = id;
      }
    },
    toggleApproval: (state, change) => {
      const { id, newStatus } = change;
      const stagedResult = state.staging.find(e => {
        return e.id === id;
      });
      if (stagedResult.status === newStatus) {
        stagedResult.status = STATES.NEEDSAPPROVAL;
      } else {
        stagedResult.status = newStatus;
      }
    },
    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,
          minWidth,
          maxWidth,
          depth,
          sourceOrganization
        } = attributes;
        let sortBy = attributes["sort-by"];
        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);
          });
      });
    },
    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);
          });
      });
    },
    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);
          });
      });
    },
    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);
          });
      });
    },
    getImports({ commit }) {
      return new Promise((resolve, reject) => {
        HTTP.get("/imports", {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("setImports", response.data.imports);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    getStaging({ commit }) {
      return new Promise((resolve, reject) => {
        HTTP.get("/imports?states=pending", {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("setStaging", response.data.imports);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
};

export {
  imports,
  STATES,
  SCHEDULES,
  IMPORTTYPES,
  IMPORTTYPEKIND,
  initializeCurrentSchedule
};