From ef4f2503c96ab048c554dcbaf695a726cbe73393 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Fri, 31 Jul 2020 13:59:25 +0100 Subject: [PATCH] DeleteInvite, user defaults, and email list modification --- api.go | 99 ++++++++++++++++++++++++++++++++++++++++++++ data/static/admin.js | 2 +- main.go | 4 ++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 396ee4c..e0cab13 100644 --- a/api.go +++ b/api.go @@ -362,3 +362,102 @@ func (ctx *appContext) SetNotify(gc *gin.Context) { ctx.storage.storeInvites() } } + +type deleteReq struct { + Code string `json:"code"` +} + +func (ctx *appContext) DeleteInvite(gc *gin.Context) { + var req deleteReq + gc.BindJSON(&req) + var ok bool + fmt.Println(req.Code) + fmt.Println(ctx.storage.invites[req.Code]) + _, ok = ctx.storage.invites[req.Code] + if ok { + fmt.Println("deleting invite") + delete(ctx.storage.invites, req.Code) + ctx.storage.storeInvites() + gc.JSON(200, map[string]bool{"success": true}) + return + } + respond(401, "Code doesn't exist", gc) +} + +type userResp struct { + UserList []respUser `json:"users"` +} + +type respUser struct { + Name string `json:"name"` + Email string `json:"email,omitempty"` +} + +func (ctx *appContext) GetUsers(gc *gin.Context) { + var resp userResp + resp.UserList = []respUser{} + users, status, err := ctx.jf.getUsers(false) + if !(status == 200 || status == 204) || err != nil { + respond(500, "Couldn't get users", gc) + return + } + for _, jfUser := range users { + var user respUser + user.Name = jfUser["Name"].(string) + if email, ok := ctx.storage.emails[jfUser["Id"].(string)]; ok { + user.Email = email.(string) + } + resp.UserList = append(resp.UserList, user) + } + gc.JSON(200, resp) +} + +func (ctx *appContext) ModifyEmails(gc *gin.Context) { + var req map[string]string + gc.BindJSON(&req) + users, status, err := ctx.jf.getUsers(false) + if !(status == 200 || status == 204) || err != nil { + respond(500, "Couldn't get users", gc) + return + } + for _, jfUser := range users { + if address, ok := req[jfUser["Name"].(string)]; ok { + ctx.storage.emails[jfUser["Id"].(string)] = address + } + } + ctx.storage.storeEmails() + gc.JSON(200, map[string]bool{"success": true}) +} + +type defaultsReq struct { + Username string `json:"username"` + Homescreen bool `json:"homescreen"` +} + +func (ctx *appContext) SetDefaults(gc *gin.Context) { + var req defaultsReq + gc.BindJSON(&req) + user, status, err := ctx.jf.userByName(req.Username, false) + if !(status == 200 || status == 204) || err != nil { + respond(500, "Couldn't get user", gc) + return + } + userId := user["Id"].(string) + policy := user["Policy"].(map[string]interface{}) + ctx.storage.policy = policy + ctx.storage.storePolicy() + if req.Homescreen { + configuration := user["Configuration"].(map[string]interface{}) + var displayprefs map[string]interface{} + displayprefs, status, err = ctx.jf.getDisplayPreferences(userId) + if !(status == 200 || status == 204) || err != nil { + respond(500, "Couldn't get displayprefs", gc) + return + } + ctx.storage.configuration = configuration + ctx.storage.displayprefs = displayprefs + ctx.storage.storeConfiguration() + ctx.storage.storeDisplayprefs() + } + gc.JSON(200, map[string]bool{"success": true}) +} diff --git a/data/static/admin.js b/data/static/admin.js index 0f846ba..77a09ea 100644 --- a/data/static/admin.js +++ b/data/static/admin.js @@ -384,7 +384,7 @@ function generateInvites(empty = false) { req.onreadystatechange = function() { if (this.readyState == 4) { var data = this.response; - if (data['invites'].length == 0) { + if (data['invites'] == null || data['invites'].length == 0) { document.getElementById('invites').textContent = ''; addItem(parseInvite([], true)); } else { diff --git a/main.go b/main.go index 57e4029..ce71cfd 100644 --- a/main.go +++ b/main.go @@ -148,6 +148,10 @@ func main() { api.POST("/generateInvite", ctx.GenerateInvite) api.GET("/getInvites", ctx.GetInvites) api.POST("/setNotify", ctx.SetNotify) + api.POST("/deleteInvite", ctx.DeleteInvite) + api.GET("/getUsers", ctx.GetUsers) + api.POST("/modifyUsers", ctx.ModifyEmails) + api.POST("/setDefaults", ctx.SetDefaults) router.Run(":8080") }