Skip to content

Commit c3af638

Browse files
committed
add route at organisation level
1 parent a9224dd commit c3af638

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package organisation
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/factly/kavach-server/model"
8+
"github.com/factly/x/errorx"
9+
"github.com/factly/x/loggerx"
10+
"github.com/factly/x/renderx"
11+
"gorm.io/gorm"
12+
)
13+
14+
func info_token(w http.ResponseWriter, r *http.Request) {
15+
tokenBody := validationBody{}
16+
17+
err := json.NewDecoder(r.Body).Decode(&tokenBody)
18+
if err != nil {
19+
loggerx.Error(err)
20+
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
21+
return
22+
}
23+
24+
orgToken := model.OrganisationToken{}
25+
// to need to specify the organisation id as token itself is unique
26+
err = model.DB.Model(&model.OrganisationToken{}).Preload("Organisation").Where(&model.OrganisationToken{
27+
Token: tokenBody.Token,
28+
}).First(&orgToken).Error
29+
30+
if err != nil {
31+
loggerx.Error(err)
32+
if err == gorm.ErrRecordNotFound {
33+
renderx.JSON(w, http.StatusUnauthorized, map[string]interface{}{"valid": false})
34+
return
35+
}
36+
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
37+
return
38+
}
39+
40+
renderx.JSON(w, http.StatusOK, map[string]interface{}{"valid": true, "organisation_id": orgToken.OrganisationID, "token_id": orgToken.ID})
41+
}

server/action/organisation/route.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func Router() chi.Router {
2727
r.Get("/my", list)
2828
r.Post("/", create)
2929
r.Post("/token/validate", validate_token)
30+
r.Post("/token/info", info_token)
3031
// r.Get("/", all)
3132
r.Route("/{organisation_id}", func(r chi.Router) {
3233
r.Get("/", details)

0 commit comments

Comments
 (0)