Skip to content

Commit

Permalink
refactor respond page
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael Ibuan <[email protected]>
  • Loading branch information
iibuan committed Oct 15, 2024
1 parent 6106a0c commit c21e8a2
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 200 deletions.
13 changes: 13 additions & 0 deletions src/goapp/controller/item/item-controller-dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

import "main/model"

type ReassignItemCallback struct {
Id string `json:"Id"`
ApproverEmail string `json:"ApproverEmail"`
Expand All @@ -9,3 +11,14 @@ type ReassignItemCallback struct {
ApproveText string `json:"ApproveText"`
RejectText string `json:"RejectText"`
}

type RespondePageData struct {
ApplicationId string
ApplicationModuleId string
ItemId string
ApproverEmail string
IsApproved string
Data model.Item
RequireRemarks bool
Response 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 @@ -12,4 +12,5 @@ type ItemController interface {
type ItemPageController interface {
MyRequests(w http.ResponseWriter, r *http.Request)
MyApprovals(w http.ResponseWriter, r *http.Request)
RespondToItem(w http.ResponseWriter, r *http.Request)
}
80 changes: 80 additions & 0 deletions src/goapp/controller/item/item-page-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"main/pkg/session"
"main/service"
"net/http"

"github.com/gorilla/mux"
)

type itemPageController struct {
Expand Down Expand Up @@ -99,3 +101,81 @@ func (c *itemPageController) MyApprovals(w http.ResponseWriter, r *http.Request)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func (c *itemPageController) RespondToItem(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"],
ApproverEmail: user.Email,
IsApproved: params["isApproved"],
Data: *item,
RequireRemarks: itemIsAuthorized.RequireRemarks,
}

t, d := c.Service.Template.UseTemplate("response", r.URL.Path, user, data)
err = t.Execute(w, d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
}
10 changes: 10 additions & 0 deletions src/goapp/model/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ type ResponseCallback struct {
ResponseDate string `json:"responseDate"`
RespondedBy string `json:"respondedBy"`
}

type ItemIsAuthorized struct {
IsAuthorized bool `json:"isAuthorized"`
IsApproved *NullBool `json:"isApproved"`
RequireRemarks bool `json:"requireRemarks"`
}

type NullBool struct {
Value bool
}
2 changes: 1 addition & 1 deletion src/goapp/model/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model
type MasterPageData struct {
Header Headers
Profile AzureUser
Content interface{}
Content interface{} `json:"content"`
Footers []Footer
OrganizationName string
}
Expand Down
8 changes: 2 additions & 6 deletions src/goapp/public/css/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -1847,18 +1847,14 @@ select {
top: 50%;
}

.top-5 {
top: 1.25rem;
.top-8 {
top: 2rem;
}

.top-full {
top: 100%;
}

.top-8 {
top: 2rem;
}

.isolate {
isolation: isolate;
}
Expand Down
1 change: 1 addition & 0 deletions src/goapp/repository/item/item-repository-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ItemRepository interface {
GetItemsBy(itemOptions model.ItemOptions) ([]model.Item, error)
GetTotalItemsBy(itemOptions model.ItemOptions) (int, error)
InsertItem(appModuleId, subject, body, requesterEmail string) (string, error)
ItemIsAuthorized(appId, appModuleId, itemId, approverEmail string) (*model.ItemIsAuthorized, error)
UpdateItemApproverEmail(id, approverEmail, username string) error
UpdateItemCallback(id string, isCallbackFailed bool) error
UpdateItemDateSent(id string) error
Expand Down
33 changes: 33 additions & 0 deletions src/goapp/repository/item/item-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,39 @@ func (r *itemRepository) UpdateItemApproverEmail(id, approverEmail, username str
defer row.Close()
return nil
}
func (r *itemRepository) ItemIsAuthorized(appId, appModuleId, itemId, approverEmail string) (*model.ItemIsAuthorized, error) {
row, err := r.Query("PR_Items_IsAuthorized",
sql.Named("ApplicationId", appId),
sql.Named("ApplicationModuleId", appModuleId),
sql.Named("ItemId", itemId),
sql.Named("ApproverEmail", approverEmail),
)
if err != nil {
return nil, err
}
defer row.Close()

result, err := r.RowsToMap(row)
if err != nil {
return nil, err
}

i := model.ItemIsAuthorized{
IsAuthorized: result[0]["IsAuthorized"] == "1",
}

if result[0]["IsApproved"] != nil {
i.IsApproved = &model.NullBool{Value: result[0]["IsApproved"].(bool)}
} else {
i.IsApproved = nil
}

if result[0]["RequireRemarks"] != nil {
i.RequireRemarks = result[0]["RequireRemarks"].(bool)
}

return &i, nil
}

func (r *itemRepository) UpdateItemCallback(id string, isCallbackFailed bool) error {
row, err := r.Query("PR_Items_Update_Callback",
Expand Down
2 changes: 1 addition & 1 deletion src/goapp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
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(rtApprovals.ResponseHandler, 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("/loginredirect", rtPages.LoginRedirectHandler)
Expand Down
70 changes: 0 additions & 70 deletions src/goapp/routes/pages/approvals/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,76 +108,6 @@ func ResponseReassignedeHandler(w http.ResponseWriter, r *http.Request) {
}

}
func ResponseHandler(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"]

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,
}
template.UseTemplate(&w, r, "response", data)
}

}
}
}

func ProcessFailedCallbacks() {
db := connectSql()
Expand Down
1 change: 1 addition & 0 deletions src/goapp/service/item/item-service-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ItemService interface {
GetItemById(id string) (*model.Item, error)
GetAll(itemOptions model.ItemOptions) (model.Response, error)
InsertItem(item model.ItemInsertRequest) (string, error)
ItemIsAuthorized(appId, appModuleId, itemId, approverEmail string) (*model.ItemIsAuthorized, error)
UpdateItemApproverEmail(itemId, approverEmail, username string) error
UpdateItemCallback(itemId string, isCallbackFailed bool) error
UpdateItemDateSent(itemId string) error
Expand Down
8 changes: 8 additions & 0 deletions src/goapp/service/item/item-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func (s *itemService) InsertItem(item model.ItemInsertRequest) (string, error) {
return id, nil
}

func (s *itemService) ItemIsAuthorized(appId, appModuleId, itemId, approverEmail string) (*model.ItemIsAuthorized, error) {
itemIsAuthorized, err := s.Repository.Item.ItemIsAuthorized(appId, appModuleId, itemId, approverEmail)
if err != nil {
return nil, err
}
return itemIsAuthorized, nil
}

func (s *itemService) UpdateItemApproverEmail(itemId, approverEmail, username string) error {
err := s.Repository.Item.UpdateItemApproverEmail(itemId, approverEmail, username)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<main class="sm:flex">
<div style="text-align: center;">
<h1 class="text-4xl font-extrabold text-gray-900 tracking-tight sm:text-5xl">Processed</h1>
<p class="mt-1 text-base text-gray-500">You have already {{.response}} this item.</p>
<p class="mt-1 text-base text-gray-500">You have already {{.Response}} this item.</p>
</div>
</main>
</div>
Expand Down
Loading

0 comments on commit c21e8a2

Please sign in to comment.