Completed
Pull Request — master (#63)
by
unknown
02:04
created

get-list-workflow.js ➔ route   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
nc 2
nop 4
dl 0
loc 61
rs 9.5147
c 1
b 1
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B get-list-workflow.js ➔ ... ➔ ??? 0 26 3
A get-list-workflow.js ➔ ... ➔ Array.forEach.call 0 7 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import path from 'path'
2
import fse from 'fs-extra'
3
import Handlebars from 'handlebars'
4
import {
5
  abeExtend
6
  ,coreUtils
7
  ,config
8
} from '../../cli'
9
10
var route = function(router, req, res, next) {
11
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
12
  var routes = router.stack
13
  var urls = []
14
  var html = ''
15
16
  Array.prototype.forEach.call(routes, function(route) {
17
    urls.push({
18
      url: route.route.path,
19
      method: Object.keys(route.route.methods)[0].toUpperCase(),
20
      regex: route.route.path.replace(/\*$/, '') + '.*'
21
    })
22
  })
23
24
  var page = path.join(__dirname + '/../views/list-workflow.html')
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
25
  if (coreUtils.file.exist(page)) {
26
    html = fse.readFileSync(page, 'utf8')
27
  }
28
29
  var workflowUrl = {}
30
  var previous = ""
31
  var next = ""
32
  Array.prototype.forEach.call(config.users.workflow, (flow) => {
33
    var current = false
34
    if (flow != 'publish') {
35
      Array.prototype.forEach.call(config.users.workflow, (flowCheck) => {
36
        if (current) {
37
          next = flowCheck
38
          current = false
39
        }
40
        if (flow === flowCheck) {
41
          current = true
42
        }
43
      })
44
    }else {
45
      next = "draft"
46
    }
47
    workflowUrl[flow] = [
48
      {url: `/abe/operations/edit/${flow}`, action: 'edit', workflow: flow, previous: previous, next: next},
49
      {url: `/abe/operations/delete/${flow}`, action: 'delete', workflow: flow, previous: previous, next: next},
50
      {url: `/abe/operations/submit/${flow}`, action: 'submit', workflow: flow, previous: previous, next: next}
51
    ]
52
53
    if (flow !== 'draft') {
54
      workflowUrl[flow].push({url: `/abe/operations/reject/${flow}`, action: 'reject', workflow: flow, previous: previous, next: next})
55
    }
56
    previous = flow
57
  })
58
  var template = Handlebars.compile(html, {noEscape: true})
59
  var tmp = template({
60
    urls: urls,
61
    user: res.user,
62
    config: JSON.stringify(config),
63
    roles: config.users.roles,
64
    workflow: config.users.workflow,
65
    workflowUrl: workflowUrl
66
  })
67
  
68
  res.cookie('csrf-token', res.locals.csrfToken)
69
  return res.send(tmp)
70
}
71
72
export default route