Skip to content

Commit

Permalink
Add document service layer #2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexblockfactory committed Feb 20, 2020
1 parent ff40435 commit aa9c248
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
76 changes: 29 additions & 47 deletions main/handlers/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ func DocumentHandler(e echo.Context) error {
return c.String(http.StatusNotFound, err.Error())
}

docApp := getDocApp(c, sess, ID)
docApp := documentService.GetDocApp(sess, ID)
if docApp == nil {
paymentRequired, err := paymentService.CheckIfWorkflowPaymentRequired(c.Session(false), ID)
if err != nil {
Expand Down Expand Up @@ -1092,7 +1092,7 @@ func DocumentNextHandler(e echo.Context) error {
if sess == nil {
return c.NoContent(http.StatusUnauthorized)
}
docApp := getDocApp(c, sess, ID)
docApp := documentService.GetDocApp(sess, ID)
if docApp == nil {
return c.String(http.StatusBadRequest, "app does not exist")
}
Expand Down Expand Up @@ -1149,7 +1149,7 @@ func DocumentPrevHandler(e echo.Context) error {
if ID != "" {
sess := c.Session(false)
if sess != nil {
docApp := getDocApp(c, sess, ID)
docApp := documentService.GetDocApp(sess, ID)
if docApp != nil {
resData := map[string]interface{}{
"status": docApp.Previous(),
Expand All @@ -1165,35 +1165,38 @@ func DocumentDataHandler(e echo.Context) error {
c := e.(*www.Context)
ID := c.Param("ID")
sess := c.Session(false)
if sess != nil {
docApp := getDocApp(c, sess, ID)
if docApp != nil {
input, err := helpers.ParseDataFromReq(c)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
verrs, err := docApp.UpdateData(input, false)
if len(verrs) > 0 {
verrs.Translate(func(key string, args ...string) string {
return c.I18n().T(key, args)
})
return c.JSON(http.StatusUnprocessableEntity, map[string]interface{}{"errors": verrs})
} else if err == nil {
return c.NoContent(http.StatusOK)
} else {
return c.String(http.StatusBadRequest, err.Error())
}
}
inputData, err := helpers.ParseDataFromReq(c)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.NoContent(http.StatusBadRequest)
if sess == nil {
return c.NoContent(http.StatusBadRequest)
}

verrs, err := documentService.Update(sess, ID, inputData)
if err != nil {
log.Println("[api][handler] DocumentDataHandler err: ", err.Error())
return c.NoContent(http.StatusBadRequest)
}

if len(verrs) > 0 {
verrs.Translate(func(key string, args ...string) string {
return c.I18n().T(key, args)
})
return c.JSON(http.StatusUnprocessableEntity, map[string]interface{}{"errors": verrs})
} else if err == nil {
return c.NoContent(http.StatusOK)
}

return c.String(http.StatusBadRequest, err.Error())
}

func DocumentFileGetHandler(e echo.Context) error {
c := e.(*www.Context)
sess := c.Session(false)
if sess != nil {
ID := c.Param("ID")
docApp := getDocApp(c, sess, ID)
docApp := documentService.GetDocApp(sess, ID)
if docApp != nil {
finfo, err := docApp.GetFile(c.Param("inputName"))
if err == nil && finfo != nil {
Expand Down Expand Up @@ -1229,7 +1232,7 @@ func DocumentFilePostHandler(e echo.Context) error {
sess := c.Session(false)
if sess != nil {
ID := c.Param("ID")
docApp := getDocApp(c, sess, ID)
docApp := documentService.GetDocApp(sess, ID)
if docApp != nil {
defer c.Request().Body.Close()
verrs, err := docApp.UpdateFile(fieldname, file.Meta{Name: fileName, ContentType: c.Request().Header.Get("Content-Type"), Size: 0}, c.Request().Body)
Expand Down Expand Up @@ -1261,7 +1264,7 @@ func DocumentPreviewHandler(e echo.Context) error {
if ID != "" && tmplID != "" && lang != "" {
sess := c.Session(false)
if sess != nil {
docApp := getDocApp(c, sess, ID)
docApp := documentService.GetDocApp(sess, ID)
if docApp != nil {
err := docApp.Preview(tmplID, lang, format, c.Response())
if err == nil {
Expand All @@ -1278,27 +1281,6 @@ func DocumentPreviewHandler(e echo.Context) error {
return c.NoContent(http.StatusNotFound)
}

func getDocApp(c *www.Context, sess *sys.Session, ID string) *app.DocumentFlowInstance {
if sess != nil {
var docApp *app.DocumentFlowInstance
sessDocAppID := "docApp_" + ID
v, ok := sess.GetMemory(sessDocAppID)
if !ok {
return nil
}
docApp = v.(*app.DocumentFlowInstance)
if docApp != nil && docApp.NeedToBeInitialized() {
err := docApp.Init(sess, c.System())
if err != nil {
log.Println("Init err", err)
return nil
}
}
return docApp
}
return nil
}

func UserDocumentListHandler(e echo.Context) error {
c := e.(*www.Context)
sess := c.Session(false)
Expand Down
39 changes: 39 additions & 0 deletions service/document.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package service

import (
"errors"
"fmt"
"github.com/ProxeusApp/proxeus-core/main/app"
"github.com/ProxeusApp/proxeus-core/sys"
"github.com/ProxeusApp/proxeus-core/sys/form"
"github.com/ProxeusApp/proxeus-core/sys/model"
"github.com/ProxeusApp/proxeus-core/sys/validate"
"github.com/ProxeusApp/proxeus-core/sys/workflow"
"log"
"regexp"
)

type (
DocumentService interface {
GetWorkflowSchema(auth model.Auth, workflowId string) (*model.WorkflowItem, map[string]interface{}, error)
Edit(auth model.Auth, userId string, formInput map[string]interface{}) error
GetDocApp(auth model.MemoryAuth, id string) *app.DocumentFlowInstance
Update(auth model.MemoryAuth, id string, data map[string]interface{}) (validate.ErrorMap, error)
}

DefaultDocumentService struct {
*baseService
}
)

var errDocAppNotFound = errors.New("doc app not found")

func NewDocumentService(system *sys.System) *DefaultDocumentService {
return &DefaultDocumentService{&baseService{system: system}}
}
Expand Down Expand Up @@ -97,3 +106,33 @@ func (me *DefaultDocumentService) marshaledFormsOf(wf *workflow.Workflow, a mode
})
return marshaledForms
}

func (me *DefaultDocumentService) GetDocApp(auth model.MemoryAuth, id string) *app.DocumentFlowInstance {
if auth == nil {
return nil
}
var docApp *app.DocumentFlowInstance
sessDocAppID := fmt.Sprintf("docApp_%s", id)
v, ok := auth.GetMemory(sessDocAppID)
if !ok {
return nil
}
docApp = v.(*app.DocumentFlowInstance)
if docApp != nil && docApp.NeedToBeInitialized() {
err := docApp.Init(auth, me.system)
if err != nil {
log.Println("Init err", err)
return nil
}
}
return docApp
}

func (me *DefaultDocumentService) Update(auth model.MemoryAuth, id string, data map[string]interface{}) (validate.ErrorMap, error) {
docApp := me.GetDocApp(auth, id)
if docApp == nil {
return nil, errDocAppNotFound
}

return docApp.UpdateData(data, false)
}
5 changes: 5 additions & 0 deletions sys/model/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Auth interface {
AccessRights() Role
}

type MemoryAuth interface {
Auth
GetMemory(k string) (interface{}, bool)
}

type AccessibleItem interface {
IsReadGrantedFor(auth Auth) bool
IsWriteGrantedFor(auth Auth) bool
Expand Down

0 comments on commit aa9c248

Please sign in to comment.