~girishm/op-mattermost

d813ab329e3fca466b6978fa42128685f8a61ff9 — girishm 30 days ago 749d0fb
delete WP feature
3 files changed, 99 insertions(+), 9 deletions(-)

M resource/routes.js
M resource/uiActions.js
M resource/util.js
M resource/routes.js => resource/routes.js +15 -0
@@ 74,6 74,9 @@ module.exports = (app, axios) => {
      case 'showTimeLogDlg':
        uiActions.loadTimeLogDlg(req, res, axios);
        break;
      case 'cnfDelWP':
        uiActions.showCnfDelWP(req, res, axios);
        break;
      default:
        res.send("Invalid action type").status(400);
        break;  


@@ 119,6 122,18 @@ module.exports = (app, axios) => {
    uiActions.saveWP(req, res, axios);
  })

  app.post('/delWP', (req, res) => {
    console.log("Work package delete request: ", req);
    switch (req.body.context.action) {
      case 'delWP':
        uiActions.delWP(req, res, axios);
        break;
      default:
        uiActions.showDelWPSel(req, res, axios);
        break;
    }
  });

  app.post('/bye', (req, res) => {
    console.log("Request to showBye handler: ", req);
    let byeMsg = JSON.stringify({

M resource/uiActions.js => resource/uiActions.js +49 -3
@@ 261,7 261,7 @@ class UIactions {

  cnfDelTimeLog(req, res) {
    this.timeLogId = req.body.context.selected_option.slice(this.optLen);
    res.set('Content-Type', 'application/json').send(JSON.stringify(this.util.getCnfDelBtnJSON(this.intURL))).status(200);
    res.set('Content-Type', 'application/json').send(JSON.stringify(this.util.getCnfDelBtnJSON(this.intURL + "delTimeLog", this.util.cnfDelTimeLogMsg, "delSelTimeLog"))).status(200);
  }

  delTimeLog(req, res, axios) {


@@ 282,7 282,6 @@ class UIactions {
  };

  createWP(req, res, axios) {
    console.log("Request to createWP handler: ", req);
    this.projectId = req.body.context.selected_option.slice(this.optLen);
    axios({
      url: 'types',


@@ 336,7 335,6 @@ class UIactions {
  };

  saveWP(req, res, axios) {
    console.log("Request to saveWP handler: ", req);
    if (req.body.submission) {
      const { subject, type, assignee, notify } = req.body.submission;
      console.log("Submission data: ");


@@ 389,6 387,54 @@ class UIactions {
    }
  };

  showDelWPSel(req, res, axios) {
    axios({
      url: '/work_packages?sortBy=[["created_at","desc"]]',
      method: 'get',
      baseURL: this.opURL,
      auth: this.opAuth
    }).then((response) => {
      console.log("WP obtained from OP: %o", response);
      let wpOptArray = [];
      response.data._embedded.elements.forEach(element => {
        wpOptArray.push({
          "text": element.subject,
          "value": "opt" + element.id
        });
      });
      let wpOptJSON = this.util.getWpOptJSON(this.intURL, wpOptArray, "cnfDelWP");
      console.log("opt Array for WP: ", wpOptJSON);
      res.set('Content-Type', 'application/json').send(JSON.stringify(wpOptJSON)).status(200);
    });
  }

  showCnfDelWP(req, res, axios) {
    this.wpId = req.body.context.selected_option.slice(this.optLen);
    res.set('Content-Type', 'application/json').send(JSON.stringify(this.util.getCnfDelBtnJSON(this.intURL+ "delWP", this.util.cnfDelWPMsg, "delWP"))).status(200);
  }

  delWP(req, res, axios) {
    axios({
      url: 'work_packages/' + this.wpId,
      method: 'delete',
      headers: {'Content-Type': 'application/json'},
      baseURL: this.opURL,
      auth: this.opAuth
    }).then((response) => {
      console.log("WP deleted. Response %o", response);
      res.set('Content-Type', 'application/json').send(JSON.stringify(this.util.getWPDelMsgJSON(this.util.wpDelMsg))).status(200);
    }).catch((error) => {
      console.log("Error in work package deletion: ", error);
      if(error.response.status === 403) {
        this.message.showMsg(req, res, axios, this.util.wpForbiddenMsg);
      }
      else {
        this.message.showMsg(req, res, axios, this.util.wpDelErrMsg);
      }
      return false;
    });
  }

  showMenuBtn(req, res) {
    res.set('Content-Type', 'application/json').send(JSON.stringify(this.util.getMenuBtnJSON(this.intURL))).status(200);
  }

M resource/util.js => resource/util.js +35 -6
@@ 27,12 27,17 @@ class Util {
    this.timeLogFailMsg = "**That didn't work :pensive: An internal error occurred!**";
    this.timeLogDelMsg = "**Time log deleted!**";
    this.timeLogDelErrMsg = "**That didn't work :pensive: Couldn't delete time log\n Please try again...`/op`**";
    this.cnfDelTimeLogMsg = "**Confirm time log deletion?**"
    this.dateErrMsg = "**It seems that date was incorrect :thinking: Please enter a date within last one year and in YYYY-MM-DD format. **";
    this.billableHoursErrMsg = "**It seems that billable hours was incorrect :thinking: Please note billable hours should be less than or equal to logged hours. **";
    this.dlgCreateErrMsg = "**It's an internal problem. Dialog creation failed :pensive: Can you please try `/op` again?**";
    this.wpDtlEmptyMsg = "**Work package details not entered :( Let's try again...**\n `/op`";
    this.saveWPSuccessMsg = "\n**Work package created! You are awesome :sunglasses: **\n To log time for a work package try `/op`";
    this.wpFetchErrMsg = "**That didn't work :pensive: Couldn't fetch work packages from OP**";
    this.cnfDelWPMsg = "**Confirm work package deletion?** This will delete all associated time entries and child work packages";
    this.wpDelErrMsg = "**That didn't work :pensive: Couldn't delete work package\n Please try again... `/op`**";
    this.wpForbiddenMsg = "**You are not authorized to delete this work package**:angry:";
    this.wpDelMsg = "**Work package deleted successfully!**";
    this.activityFetchErrMsg = "**That didn't work :pensive: Couldn't fetch activities from OP**";
    this.typeFetchErrMsg = "**That didn't work :pensive: Couldn't to fetch types from OP**";
    this.dlgCancelMsg = "** If you would like to try again then, `/op` **";


@@ 288,6 293,22 @@ class Util {
    }
  }

  getWPDelMsgJSON(msg) {
    return {
      "update": {
        "response_type": "in_channel",
        "props": {
          "attachments": [
            {
              "text": msg,
              "actions": []
            }
          ]
        }
      }
    };
  }

  getWpCreateJSON(triggerId, url, typeArray, assigneeArray) {
    return {
      "trigger_id": triggerId,


@@ 378,6 399,15 @@ class Util {
                }
              },
              {
                "name": "Delete Work Package",
                "integration": {
                  "url": url + "delWP",
                  "context": {
                    "action": ""
                  }
                }
              },
              {
                "name": "Bye :wave:",
                "integration": {
                  "url": url + "bye",


@@ 393,29 423,28 @@ class Util {
    };
  }

  getCnfDelBtnJSON(url, timeLog) {
  getCnfDelBtnJSON(url, msg, action) {
    return {
      "update": {
        "response_type": "in_channel",
        "props": {
          "attachments": [
            {
              "pretext": timeLog,
              "text": "Confirm time log deletion?",
              "text": msg,
              "actions": [
                {
                  "name": "Yes, Delete!",
                  "integration": {
                    "url": url + "delTimeLog",
                    "url": url,
                    "context": {
                      "action": "delSelTimeLog"
                      "action": action
                    }
                  }
                },
                {
                  "name": "No, go back.",
                  "integration": {
                    "url": url + "delTimeLog",
                    "url": url,
                    "context": {
                      "action": ""
                    }