Skip to content

Commit d9d852e

Browse files
feat: Filter by request type
Signed-off-by: Jerrico Dela Cruz <94591636+jerricotandelacruz@users.noreply.github.com>
1 parent ebca4f4 commit d9d852e

File tree

7 files changed

+140
-15
lines changed

7 files changed

+140
-15
lines changed

src/goapp/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func setPageRoutes() {
2727
}
2828

2929
func setApiRoutes() {
30-
30+
httpRouter.GET("/api/request/types", m.Chain(rtApi.GetRequestTypes, m.AzureAuth()))
3131
httpRouter.POST("/api/request", rtApprovals.ApprovalRequestHandler)
3232
httpRouter.POST("/api/process", rtApprovals.ProcessResponseHandler)
3333
httpRouter.GET("/api/items/type/{type:[0-2]+}/status/{status:[0-3]+}", m.Chain(rtApi.GetItems, m.AzureAuth()))

src/goapp/routes/apis/item.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ func GetItems(w http.ResponseWriter, r *http.Request) {
9494
filter, _ := strconv.Atoi(params["filter"][0])
9595
offset, _ := strconv.Atoi(params["offset"][0])
9696
search := params["search"][0]
97-
data, total, err := GetItemsBy(ItemType(itemType), ItemStatus(itemStatus), user, search, offset, filter)
97+
requestType := ""
98+
if params["requestType"] != nil {
99+
if params["requestType"][0] != "" {
100+
requestType = params["requestType"][0]
101+
}
102+
}
103+
104+
data, total, err := GetItemsBy(ItemType(itemType), ItemStatus(itemStatus), requestType, user, search, offset, filter)
98105
if err != nil {
99106
http.Error(w, err.Error(), http.StatusInternalServerError)
100107
return
@@ -111,7 +118,7 @@ func GetItems(w http.ResponseWriter, r *http.Request) {
111118
json.NewEncoder(w).Encode(result)
112119
}
113120

114-
func GetItemsBy(itemType ItemType, itemStatus ItemStatus, user, search string, offset, filter int) ([]Item, int, error) {
121+
func GetItemsBy(itemType ItemType, itemStatus ItemStatus, requestType, user, search string, offset, filter int) ([]Item, int, error) {
115122
dbConnectionParam := sql.ConnectionParam{
116123
ConnectionString: os.Getenv("APPROVALSYSTEMDB_CONNECTION_STRING"),
117124
}
@@ -129,6 +136,9 @@ func GetItemsBy(itemType ItemType, itemStatus ItemStatus, user, search string, o
129136
params["User"] = user
130137
}
131138

139+
if requestType != "" {
140+
params["RequestType"] = requestType
141+
}
132142
params["IsApproved"] = itemStatus
133143
params["Search"] = search
134144

@@ -212,3 +222,50 @@ func GetItemsBy(itemType ItemType, itemStatus ItemStatus, user, search string, o
212222

213223
return items, total, nil
214224
}
225+
226+
type RequestType struct {
227+
Id string `json:"id"`
228+
Name string `json:"name"`
229+
}
230+
231+
func GetRequestTypes(w http.ResponseWriter, r *http.Request) {
232+
w.Header().Set("Content-Type", "application/json")
233+
234+
result, err := GetRequestType()
235+
if err != nil {
236+
http.Error(w, err.Error(), http.StatusInternalServerError)
237+
return
238+
}
239+
240+
w.WriteHeader(http.StatusOK)
241+
json.NewEncoder(w).Encode(result)
242+
}
243+
244+
func GetRequestType() ([]RequestType, error) {
245+
dbConnectionParam := sql.ConnectionParam{
246+
ConnectionString: os.Getenv("APPROVALSYSTEMDB_CONNECTION_STRING"),
247+
}
248+
249+
db, err := sql.Init(dbConnectionParam)
250+
if err != nil {
251+
return nil, err
252+
}
253+
defer db.Close()
254+
255+
result, err := db.ExecuteStoredProcedureWithResult("PR_ApplicationModules_Select", nil)
256+
if err != nil {
257+
return nil, err
258+
}
259+
260+
var requestTypes []RequestType
261+
262+
for _, v := range result {
263+
requestType := RequestType{
264+
Id: fmt.Sprintf("%v", v["Id"]),
265+
Name: fmt.Sprintf("%v", v["Name"]),
266+
}
267+
requestTypes = append(requestTypes, requestType)
268+
}
269+
270+
return requestTypes, nil
271+
}

src/goapp/templates/myapprovals.html

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div x-show="activeTab == tabs[1]">
2727
<div class="p-5" x-data="list({
2828
enabledSearch: false,
29-
otherState: { responseType: 'All'},
29+
otherState: { responseType: 'All', requestType: {id : '', name: 'All'} },
3030
callback: closedCallback,
3131
renderItem: closedRenderItem
3232
})">
@@ -44,6 +44,20 @@
4444
</select>
4545
</div>
4646
</div>
47+
<div class="sm:block">
48+
<div class="content-start">
49+
<label for="filter" class="block text-sm font-medium text-gray-700">Filter by Request Type</label>
50+
<select @change="(e) => {
51+
state.other.requestType = requestTypes.find((obj) => { return e.target.value == obj.name})
52+
load()
53+
}"
54+
id="reponseType" name="responseType" class="block mt-1 pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
55+
<template x-for="item in requestTypes" :key="item.id">
56+
<option x-text="item.name"></option>
57+
</template>
58+
</select>
59+
</div>
60+
</div>
4761
<div class="flex justify-between sm:justify-end">
4862
<div class="sm:col-span-3">
4963
<label for="search" class="block text-sm font-medium text-gray-700">Search</label>
@@ -64,10 +78,13 @@
6478
return {
6579
tabs : ['pending', 'closed'],
6680
activeTab : '',
81+
requestTypes : [{id : '', name: 'All'}],
6782
currentSelected: -1,
6883
data: JSON.parse('{{ . }}'),
6984
async init(){
7085
this.activeTab = this.tabs[0];
86+
requestTypes = await getRequestTypes()
87+
this.requestTypes = [...this.requestTypes, ...requestTypes]
7188
},
7289
onChangeTabs(tab){
7390
this.activeTab = this.tabs[tab];
@@ -157,7 +174,7 @@
157174
}
158175

159176
async function pendingCallback(e){
160-
return await getItemsBy(1, 0, e.filter, e.page, e.search)
177+
return await getItemsBy(1, 0, '', e.filter, e.page, e.search)
161178
}
162179

163180
//CLOSED REQUEST
@@ -213,14 +230,21 @@
213230
async function closedCallback(e){
214231
const responses = [{name: 'All', value: 3}, {name: 'Rejected', value: 2}, {name: 'Approved', value: 1}]
215232
selectedResponseType = responses.find((obj) => { return obj.name == e.other.responseType}).value
216-
return await getItemsBy(1, selectedResponseType, e.filter, e.page, e.search)
233+
return await getItemsBy(1, selectedResponseType, e.other.requestType.id, e.filter, e.page, e.search)
217234
}
218235

219-
async function getItemsBy(type, status, filter, page, search){
236+
async function getItemsBy(type, status, requestType, filter, page, search){
220237
const offset = filter * page;
221238
search = encodeURIComponent(search)
239+
requestTypeParam = requestType == '' ? '' : '&requestType=' + requestType
240+
const res = await fetch(`/api/items/type/${type}/status/${status}?filter=${filter}&offset=${offset}&search=${search}${requestTypeParam}`)
241+
const data = await res.json()
242+
return data
243+
}
244+
222245

223-
const res = await fetch(`/api/items/type/${type}/status/${status}?filter=${filter}&offset=${offset}&search=${search}`)
246+
async function getRequestTypes(){
247+
const res = await fetch(`/api/request/types`)
224248
const data = await res.json()
225249
return data
226250
}

src/goapp/templates/myrequests.html

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<div x-show="activeTab == tabs[1]">
3131
<div class="p-5" x-data="list({
3232
enabledSearch: false,
33-
otherState: { responseType: 'All'},
33+
otherState: { responseType: 'All', requestType: {id : '', name: 'All'} },
3434
callback: closedCallback,
3535
renderItem: closedRenderItem
3636
})">
@@ -48,6 +48,22 @@
4848
</select>
4949
</div>
5050
</div>
51+
<div class="sm:block">
52+
<div class="content-start">
53+
<label for="filter" class="block text-sm font-medium text-gray-700">Filter by Request Type</label>
54+
<select @change="(e) => {
55+
console.log(requestTypes)
56+
state.other.requestType = requestTypes.find((obj) => { return e.target.value == obj.name})
57+
58+
load()
59+
}"
60+
id="reponseType" name="responseType" class="block mt-1 pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
61+
<template x-for="item in requestTypes" :key="item.id">
62+
<option x-text="item.name"></option>
63+
</template>
64+
</select>
65+
</div>
66+
</div>
5167
<div class="flex justify-between sm:justify-end">
5268
<div class="sm:col-span-3">
5369
<label for="search" class="block text-sm font-medium text-gray-700">Search</label>
@@ -68,8 +84,11 @@
6884
return {
6985
tabs : ['pending', 'closed'],
7086
activeTab : '',
87+
requestTypes : [{id : '', name: 'All'}],
7188
async init(){
7289
this.activeTab = this.tabs[0];
90+
requestTypes = await getRequestTypes()
91+
this.requestTypes = [...this.requestTypes, ...requestTypes]
7392
},
7493
onChangeTabs(tab){
7594
this.activeTab = this.tabs[tab];
@@ -117,7 +136,7 @@
117136
}
118137

119138
async function pendingCallback(e){
120-
return await getItemsBy(0, 0, e.filter, e.page, e.search)
139+
return await getItemsBy(0, 0, '', e.filter, e.page, e.search)
121140
}
122141

123142
//CLOSED REQUEST
@@ -173,14 +192,20 @@
173192
async function closedCallback(e){
174193
const responses = [{name: 'All', value: 3}, {name: 'Rejected', value: 2}, {name: 'Approved', value: 1}]
175194
selectedResponseType = responses.find((obj) => { return obj.name == e.other.responseType}).value
176-
return await getItemsBy(0, selectedResponseType, e.filter, e.page, e.search)
195+
return await getItemsBy(0, selectedResponseType, e.other.requestType.id, e.filter, e.page, e.search)
177196
}
178197

179-
async function getItemsBy(type, status, filter, page, search){
198+
async function getItemsBy(type, status, requestType, filter, page, search){
180199
const offset = filter * page;
181200
search = encodeURIComponent(search)
201+
requestTypeParam = requestType == '' ? '' : '&requestType=' + requestType
202+
const res = await fetch(`/api/items/type/${type}/status/${status}?filter=${filter}&offset=${offset}&search=${search}${requestTypeParam}`)
203+
const data = await res.json()
204+
return data
205+
}
182206

183-
const res = await fetch(`/api/items/type/${type}/status/${status}?filter=${filter}&offset=${offset}&search=${search}`)
207+
async function getRequestTypes(){
208+
const res = await fetch(`/api/request/types`)
184209
const data = await res.json()
185210
return data
186211
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE PROCEDURE [dbo].[PR_ApplicationModules_Select]
2+
AS
3+
BEGIN
4+
SET NOCOUNT ON
5+
SELECT
6+
CONVERT(varchar(36), [Id], 1) AS [Id],
7+
[Name]
8+
FROM [dbo].[ApplicationModules]
9+
END

src/sqldb/Stored Procedures/PR_Items_Select.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ CREATE PROCEDURE [dbo].[PR_Items_Select]
55
@Filter INT = 10,
66
@ItemType bit = NULL, -- NULL - ALL / 0 - REQUESTOR / 1 - APPROVER
77
@User varchar(100) = NULL,
8-
@IsApproved int = 4
8+
@IsApproved int = 4,
9+
@RequestType varchar(100) = NULL
910
)
1011
AS
1112
BEGIN
@@ -47,6 +48,10 @@ BEGIN
4748
(@IsApproved = 2 AND i.IsApproved = 0) OR -- Rejected
4849
(@IsApproved = 3 AND i.IsApproved IS NOT NULL) -- Closed (Rejected, Approved)
4950
-- If the value of IsApproved is 4 then select all
51+
) AND
52+
(
53+
@RequestType IS NULL OR
54+
(@RequestType IS NOT NULL AND i.ApplicationModuleId = @RequestType)
5055
)
5156
ORDER BY I.Created DESC
5257
OFFSET @Offset ROWS

src/sqldb/Stored Procedures/PR_Items_Total.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ CREATE PROCEDURE [dbo].[PR_Items_Total]
33
@Search VARCHAR(50) = '',
44
@ItemType bit = NULL, -- NULL - ALL / 0 - REQUESTOR / 1 - APPROVER,
55
@User varchar(100) = NULL,
6-
@IsApproved int = -1 -- -1 - ALL / NULL - PENDING / 0 - REJECTED / 1 - APPROVED
6+
@IsApproved int = 4,
7+
@RequestType varchar(100) = NULL
78
)
89
AS
910
BEGIN
@@ -38,6 +39,10 @@ BEGIN
3839
(@IsApproved = 2 AND i.IsApproved = 0) OR -- Rejected
3940
(@IsApproved = 3 AND i.IsApproved IS NOT NULL) -- Closed (Rejected, Approved)
4041
-- If the value of IsApproved is 4 then select all
42+
) AND
43+
(
44+
@RequestType IS NULL OR
45+
(@RequestType IS NOT NULL AND i.ApplicationModuleId = @RequestType)
4146
)
4247
) AS Items
4348
END

0 commit comments

Comments
 (0)