Skip to content

Commit

Permalink
DeleteInvite, user defaults, and email list modification
Browse files Browse the repository at this point in the history
  • Loading branch information
hrfee committed Jul 31, 2020
1 parent e5ebcef commit ef4f250
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
99 changes: 99 additions & 0 deletions api.go
Expand Up @@ -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})
}
2 changes: 1 addition & 1 deletion data/static/admin.js
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Expand Up @@ -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")
}

0 comments on commit ef4f250

Please sign in to comment.