diff --git a/server/dialog.go b/server/dialog.go index ce78838..f7f76f9 100644 --- a/server/dialog.go +++ b/server/dialog.go @@ -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, }, { diff --git a/server/misc.go b/server/misc.go index 7fb7f1d..5462017 100644 --- a/server/misc.go +++ b/server/misc.go @@ -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" @@ -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, @@ -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 } diff --git a/server/uiActions.go b/server/uiActions.go index 797ddbb..94c8e21 100644 --- a/server/uiActions.go +++ b/server/uiActions.go @@ -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" @@ -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 { @@ -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 {