Skip to content

Commit

Permalink
time validation
Browse files Browse the repository at this point in the history
  • Loading branch information
girish17 committed Sep 14, 2023
1 parent 4773d6b commit 91bc8a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions server/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func GetLogTimeDlg(pluginURL string, triggerID string, options []*model.PostActi
DisplayName: "Comment",
Name: "comments",
Type: "textarea",
Default: "none",
Placeholder: "Please mention comments if any",
Optional: true,
}, {
Expand Down
38 changes: 34 additions & 4 deletions server/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
Expand Down Expand Up @@ -177,6 +180,21 @@ func GetAttachmentJSON(pluginURL string) string {
}`
}

func GetTimeEntriesBodyJSON(submission map[string]interface{}, loggedHours string, billableHours string) ([]byte, error) {
var timeEntriesBody TimeEntryPostBody
timeEntriesBody.Links.Project.Href = apiVersionStr + "projects/" + projectID
timeEntriesBody.Links.WorkPackage.Href = apiVersionStr + "work_packages/" + wpID
activityID = strings.Split(submission["activity"].(string), "opt")[1]
timeEntriesBody.Links.Activity.Href = apiVersionStr + "time_entries/activities/" + activityID
timeEntriesBody.SpentOn = submission["spent_on"].(string)
timeEntriesBody.Comment.Raw = submission["comments"].(string)
spentHoursFloat, _ := strconv.ParseFloat(loggedHours, 64)
loggedHoursDuration := time.Duration(spentHoursFloat*3600) * time.Second
timeEntriesBody.Hours = fmt.Sprintf("PT%fH", loggedHoursDuration.Hours())
timeEntriesBody.CustomField = billableHours
return json.Marshal(timeEntriesBody)
}

func getUpdatePostMsg(userID string, channelID string, msg string) *model.Post {
var post = &model.Post{
Id: menuPost.Id,
Expand All @@ -195,10 +213,22 @@ func setOPStr(p plugin.MattermostPlugin) {
p.API.LogInfo("opURLStr: " + OpURLStr + " apiKeyStr: " + APIKeyStr)
}

func checkDate() bool {
return true
func checkDate(dateStr string) bool {
layout := "2006-01-02"
date, err := time.Parse(layout, dateStr)
if err != nil {
return false
}
currentDate := time.Now()
oneYearAgo := currentDate.AddDate(-1, 0, 0)
if date.After(oneYearAgo) && date.Before(currentDate) {
return true
}
return false
}

func checkHours(_ string) bool {
return true
func checkHours(billableHours string, hoursLogged string) bool {
hoursLoggedFloat, _ := strconv.ParseFloat(hoursLogged, 64)
billableHoursFloat, _ := strconv.ParseFloat(billableHours, 64)
return billableHoursFloat <= hoursLoggedFloat
}
23 changes: 5 additions & 18 deletions server/uiActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
Expand Down Expand Up @@ -340,22 +338,11 @@ func HandleSubmission(p plugin.MattermostPlugin, _ http.ResponseWriter, r *http.
post = getUpdatePostMsg(user.Id, jsonBody["channel_id"].(string), messages.DlgCancelMsg)
} else {
submission := jsonBody["submission"].(map[string]interface{})
if checkDate() {
if checkDate(submission["spent_on"].(string)) {
billableHours := submission["billable_hours"].(string)
if checkHours(billableHours) {
var timeEntriesBody TimeEntryPostBody
timeEntriesBody.Links.Project.Href = apiVersionStr + "projects/" + projectID
timeEntriesBody.Links.WorkPackage.Href = apiVersionStr + "work_packages/" + wpID
activityID = strings.Split(submission["activity"].(string), "opt")[1]
timeEntriesBody.Links.Activity.Href = apiVersionStr + "time_entries/activities/" + activityID
timeEntriesBody.SpentOn = submission["spent_on"].(string)
timeEntriesBody.Comment.Raw = submission["comments"].(string)
spentHoursFloat, _ := strconv.ParseFloat(submission["spent_hours"].(string), 64)
loggedHoursDuration := time.Duration(spentHoursFloat*3600) * time.Second
timeEntriesBody.Hours = fmt.Sprintf("PT%fH", loggedHoursDuration.Hours())
timeEntriesBody.CustomField = billableHours
p.API.LogDebug("Time entries body: ", timeEntriesBody)
timeEntriesBodyJSON, _ := json.Marshal(timeEntriesBody)
loggedHours := submission["spent_hours"].(string)
if checkHours(billableHours, loggedHours) {
timeEntriesBodyJSON, _ := GetTimeEntriesBodyJSON(submission, loggedHours, billableHours)
resp, err := PostTimeEntry(timeEntriesBodyJSON, OpURLStr, APIKeyStr)
p.API.LogDebug("Time entries body JSON: ", string(timeEntriesBodyJSON))
if err == nil {
Expand All @@ -376,7 +363,7 @@ func HandleSubmission(p plugin.MattermostPlugin, _ http.ResponseWriter, r *http.
post = getUpdatePostMsg(user.Id, jsonBody["channel_id"].(string), messages.TimeEntrySaveFailMsg)
}
} else {
p.API.LogInfo("Billable hours incorrect: ", jsonBody["billable_hours"])
p.API.LogInfo("Billable hours incorrect: ", billableHours)
post = getUpdatePostMsg(user.Id, jsonBody["channel_id"].(string), messages.BillableHourMsg)
}
} else {
Expand Down

0 comments on commit 91bc8a5

Please sign in to comment.