Completed
Push — master ( 78d2f9...949122 )
by greg
42s
created

src/server/routes/operations/post/submit.js   A

Complexity

Total Complexity 9
Complexity/F 2.25

Size

Lines of Code 45
Function Count 4

Duplication

Duplicated Lines 45
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 9
nc 1
mnd 1
bc 7
fnc 4
dl 45
loc 45
rs 10
bpm 1.75
cpm 2.25
noi 1
c 1
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B submit.js ➔ route 38 38 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
import {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
  cmsOperations,
3
  abeExtend
4
} from '../../../../cli'
5
6
var route = function(req, res, next){
7
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
8
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
9
10
  let regUrl = /\/abe\/save\/(.*?)\/submit\//
11
  var workflow = 'draft'
12
  var match = req.originalUrl.match(regUrl)
13
  if (match != null && match[1] != null) {
14
    workflow = match[1]
15
  }
16
  var postUrl = req.originalUrl.replace(regUrl, '')
17
  var json = req.body.json
18
  
19
  var p
20
  if (workflow === 'publish') {
21
    p = cmsOperations.post.publish(
22
      postUrl, 
23
      json
24
    )
25
  }else {
26
    p = cmsOperations.post.draft(
27
      postUrl, 
28
      json, 
29
      workflow
30
    )
31
  }
32
33
  p.then((result) => {
34
    res.set('Content-Type', 'application/json')
35
    res.send(JSON.stringify(result))
36
  },
37
  (result) => {
38
    res.set('Content-Type', 'application/json')
39
    res.send(JSON.stringify(result))
40
  }).catch(function(e) {
41
    console.error('[ERROR] post-save.js', e)
42
  })
43
}
44
45
export default route