-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89b77bc
commit ff40435
Showing
7 changed files
with
207 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package service | ||
|
||
import ( | ||
"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/workflow" | ||
"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 | ||
} | ||
|
||
DefaultDocumentService struct { | ||
*baseService | ||
} | ||
) | ||
|
||
func NewDocumentService(system *sys.System) *DefaultDocumentService { | ||
return &DefaultDocumentService{&baseService{system: system}} | ||
} | ||
|
||
func (me *DefaultDocumentService) GetWorkflowSchema(auth model.Auth, workflowId string) (*model.WorkflowItem, map[string]interface{}, error) { | ||
|
||
wf, err := me.workflowDB().Get(auth, workflowId) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
fieldsAndRules := me.getAllFormFieldsWithRulesOf(wf.Data, auth) | ||
|
||
return wf, fieldsAndRules, nil | ||
} | ||
|
||
func (me *DefaultDocumentService) Edit(auth model.Auth, userId string, formInput map[string]interface{}) error { | ||
filenameRegex := regexp.MustCompile(`^[^\s][\p{L}\d.,_\-&: ]{3,}[^\s]$`) | ||
|
||
if n, ok := formInput["name"]; ok { | ||
if fname, ok := n.(string); ok { | ||
if len(fname) < 80 && filenameRegex.MatchString(fname) { | ||
usrDataItem, err := me.userDataDB().Get(auth, userId) | ||
if err != nil { | ||
return err | ||
} | ||
if n, ok := formInput["detail"]; ok { | ||
if fdetail, ok := n.(string); ok { | ||
usrDataItem.Detail = fdetail | ||
} | ||
} | ||
usrDataItem.Name = fname | ||
return me.userDataDB().Put(auth, usrDataItem) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (me *DefaultDocumentService) getAllFormFieldsWithRulesOf(wf *workflow.Workflow, auth model.Auth) map[string]interface{} { | ||
marshaledForms := me.marshaledFormsOf(wf, auth) | ||
fieldsAndRules := map[string]interface{}{} | ||
//collect all form fields | ||
for _, formItem := range marshaledForms { | ||
vars := form.Vars(formItem.Data) | ||
for _, v := range vars { | ||
fieldsAndRules[v] = form.RulesOf(formItem.Data, v) | ||
} | ||
} | ||
return fieldsAndRules | ||
} | ||
|
||
func (me *DefaultDocumentService) marshaledFormsOf(wf *workflow.Workflow, a model.Auth) map[string]*model.FormItem { | ||
if wf == nil { | ||
return nil | ||
} | ||
marshaledForms := map[string]*model.FormItem{} | ||
loop := &workflow.Looper{} | ||
//loop recursively and collect all forms | ||
wf.Loop(loop, func(l *workflow.Looper, node *workflow.Node) bool { | ||
if node.Type == "form" { | ||
if _, ok := marshaledForms[node.ID]; !ok { | ||
it, er := me.formDB().Get(a, node.ID) | ||
if er != nil { | ||
return true //continue | ||
} | ||
marshaledForms[it.ID] = it | ||
} | ||
} else if node.Type == "workflow" { // deep dive... | ||
it, er := me.workflowDB().Get(a, node.ID) | ||
if er != nil { | ||
return true //continue | ||
} | ||
it.LoopNodes(l, nil) | ||
} | ||
return true //continue | ||
}) | ||
return marshaledForms | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.