Skip to content

Commit

Permalink
Merge pull request #140 from Avanade/117-refactor-reassign-request-page
Browse files Browse the repository at this point in the history
117 refactor reassign request page
  • Loading branch information
jerricotandelacruz authored Oct 16, 2024
2 parents 3378c03 + f86ddc4 commit 2595520
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 92 deletions.
2 changes: 2 additions & 0 deletions src/goapp/controller/item/item-controller-dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ type RespondePageData struct {
Data model.Item
RequireRemarks bool
Response string
ApproveText string
RejectText string
}
1 change: 1 addition & 0 deletions src/goapp/controller/item/item-controller-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type ItemPageController interface {
MyRequests(w http.ResponseWriter, r *http.Request)
MyApprovals(w http.ResponseWriter, r *http.Request)
RespondToItem(w http.ResponseWriter, r *http.Request)
ReassignApproval(w http.ResponseWriter, r *http.Request)
}
77 changes: 77 additions & 0 deletions src/goapp/controller/item/item-page-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,80 @@ func (c *itemPageController) RespondToItem(w http.ResponseWriter, r *http.Reques
}
}
}

func (c *itemPageController) ReassignApproval(w http.ResponseWriter, r *http.Request) {
session, _ := session.Store.Get(r, "auth-session")

var profile map[string]interface{}
u := session.Values["profile"]
profile, ok := u.(map[string]interface{})
if !ok {
http.Error(w, "Error getting user data", http.StatusInternalServerError)
return
}
user := model.AzureUser{
Name: profile["name"].(string),
Email: profile["preferred_username"].(string),
}

params := mux.Vars(r)

itemIsAuthorized, err := c.Service.Item.ItemIsAuthorized(
params["appGuid"],
params["appModuleGuid"],
params["itemGuid"],
user.Email,
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if !itemIsAuthorized.IsAuthorized {
t, d := c.Service.Template.UseTemplate("Unauthorized", r.URL.Path, user, nil)
err = t.Execute(w, d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
if itemIsAuthorized.IsApproved != nil {
var text string
if itemIsAuthorized.IsApproved.Value {
text = "approved"
} else {
text = "rejected"
}

data := RespondePageData{
Response: text,
}

t, d := c.Service.Template.UseTemplate("already-processed", r.URL.Path, user, data)
err = t.Execute(w, d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
item, err := c.Service.Item.GetItemById(params["itemGuid"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

data := RespondePageData{
ApplicationId: params["appGuid"],
ApplicationModuleId: params["appModuleGuid"],
ItemId: params["itemGuid"],
Data: *item,
ApproveText: params["ApproveText"],
RejectText: params["RejectText"],
}

t, d := c.Service.Template.UseTemplate("reassign", r.URL.Path, user, data)
err = t.Execute(w, d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
}
2 changes: 1 addition & 1 deletion src/goapp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func setPageRoutes() {
httpRouter.GET("/", m.Chain(ctrl.ItemPage.MyRequests, m.AzureAuth()))
httpRouter.GET("/myapprovals", m.Chain(ctrl.ItemPage.MyApprovals, m.AzureAuth()))
httpRouter.GET("/response/{appGuid}/{appModuleGuid}/{itemGuid}/{isApproved}", m.Chain(ctrl.ItemPage.RespondToItem, m.AzureAuth()))
httpRouter.GET("/responsereassigned/{appGuid}/{appModuleGuid}/{itemGuid}/{isApproved}/{ApproveText}/{RejectText}", m.Chain(rtApprovals.ResponseReassignedeHandler, m.AzureAuth()))
httpRouter.GET("/responsereassigned/{appGuid}/{appModuleGuid}/{itemGuid}/{isApproved}/{ApproveText}/{RejectText}", m.Chain(ctrl.ItemPage.ReassignApproval, m.AzureAuth()))

httpRouter.GET("/loginredirect", rtPages.LoginRedirectHandler)
httpRouter.GET("/login/azure", rtAzure.LoginHandler)
Expand Down
85 changes: 0 additions & 85 deletions src/goapp/routes/pages/approvals/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
session "main/pkg/session"
"main/pkg/sql"
template "main/pkg/template"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
)

func connectSql() (db *sql.DB) {
Expand All @@ -22,92 +18,11 @@ func connectSql() (db *sql.DB) {
return
}

func handleErrorReturn(w http.ResponseWriter, err error) {
if err != nil {
fmt.Printf("ERROR: %+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
}
}

func handleError(err error) {
if err != nil {
fmt.Printf("ERROR: %+v", err)
}
}
func ResponseReassignedeHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
var username string
sessionaz, _ := session.Store.Get(r, "auth-session")
iprofile := sessionaz.Values["profile"]

if iprofile != nil {
profile := iprofile.(map[string]interface{})
username = profile["preferred_username"].(string)
}
params := mux.Vars(r)

appGuid := params["appGuid"]
appModuleGuid := params["appModuleGuid"]
itemGuid := params["itemGuid"]
isApproved := params["isApproved"]
ApproveText := params["ApproveText"]
RejectText := params["RejectText"]
sqlParamsIsAuth := map[string]interface{}{
"ApplicationId": appGuid,
"ApplicationModuleId": appModuleGuid,
"ItemId": itemGuid,
"ApproverEmail": username,
}

sqlParamsItems := map[string]interface{}{
"Id": itemGuid,
}

db := connectSql()
defer db.Close()
resIsAuth, err := db.ExecuteStoredProcedureWithResult("PR_RESPONSE_IsAuthorized", sqlParamsIsAuth)
handleErrorReturn(w, err)

isAuth := resIsAuth[0]["IsAuthorized"]
if isAuth == "0" {
template.UseTemplate(&w, r, "Unauthorized", nil)
} else {
isProcessed := resIsAuth[0]["IsApproved"]
if isProcessed != nil {
var text string
if isProcessed == true {
text = "approved"
} else {
text = "rejected"
}
data := map[string]interface{}{
"response": text,
}
template.UseTemplate(&w, r, "AlreadyProcessed", data)
} else {
resItems, err := db.ExecuteStoredProcedureWithResult("PR_Items_Select_ById", sqlParamsItems)

handleErrorReturn(w, err)
requireRemarks := resIsAuth[0]["RequireRemarks"]
data := map[string]interface{}{
"ApplicationId": appGuid,
"ApplicationModuleId": appModuleGuid,
"ItemId": itemGuid,
"ApproverEmail": username,
"IsApproved": isApproved,
"Data": resItems[0],
"RequireRemarks": requireRemarks,
"ApproveText": ApproveText,
"RejectText": RejectText,
}
template.UseTemplate(&w, r, "responsereassign", data)
}

}
}

}

func ProcessFailedCallbacks() {
db := connectSql()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{ define "content" }}
<div x-data="data()" x-init="onLoad()">
<h1 class="text-4xl">{{.Data.Subject}}</h1>
<h1 class="text-2xl mb-5">Reassign Approval Request</h1>
<span x-html="body"></span>

<form onsubmit="event.preventDefault()" id="form">
Expand Down Expand Up @@ -103,21 +103,17 @@ <h3 class="text-lg leading-6 font-medium text-gray-900" x-text="modalText"></h3>
}
function data(){
return {
body: "{{.Data.Body}}",
body: `{{.Data.Body}}`,
form: {
applicationId: "{{.ApplicationId}}",
applicationModuleId: "{{.ApplicationModuleId}}",
itemId: "{{.ItemId}}",
approverEmail: "{{.ApproverEmail}}",
ApproveText: "{{.ApproveText}}",
RejectText: "{{.RejectText}}",
remarks: "",
isApproved: "{{.IsApproved}}",
name : '',
approver : [],
status : null,
},
requireRemarks: "{{.RequireRemarks}}",
showModal: false,
status: "",
modalText: "Please wait...",
Expand Down

0 comments on commit 2595520

Please sign in to comment.