models/invoice.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 25
Function Count 1

Duplication

Duplicated Lines 25
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 25
loc 25
rs 10
wmc 2
mnd 1
bc 2
fnc 1
bpm 2
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A invoiceSchema.pre(ꞌsaveꞌ) 12 12 2

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
var mongoose = require('mongoose')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
var invoiceItem = require('./common/line-item')
3
4
var invoiceSchema = new mongoose.Schema({
5
  date: { type: Date, default: Date.now },
6
  customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true },
7
  items: [invoiceItem],
8
  totalVat: { type: Number, default: 0.00 },
9
  total: { type: Number, default: 0.00 }
10
})
11
12
invoiceSchema.pre('save', function (next) {
13
  let overallTotal = 0.00
14
  // let overallVat
15
  // All the items should give the total
16
  for (let item in this.items) {
17
    overallTotal += item.total
18
    // overallVat += item.subTotal * item.vatRate
19
  }
20
21
  this.total = overallTotal
22
  next()
23
})
24
25
module.exports = mongoose.model('Invoice', invoiceSchema)
26