models/expense.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 29
Function Count 2

Duplication

Duplicated Lines 29
Ratio 100 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A expenseSchema.pre(ꞌsaveꞌ) 10 10 1

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 expenseItem = require('./common/line-item')
3
4
var expenseSchema = new mongoose.Schema({
5
  date: { type: Date, default: Date.now },
6
  company: { type: mongoose.Schema.Types.ObjectId, ref: 'Company', required: true },
7
  category: ['Purchases', 'Motor Fuel', 'Motor Serv/Repair',
8
    'Motor Tax/Ins', 'Van DOE', 'Public Laibility Insurance',
9
    'Advertising', 'Tele/bband', 'Training', 'Office Expenses',
10
    'Sub Contractor', 'Landfill', 'Ber Cert Fees', 'Bank Fees',
11
    'Sundry', 'Heating', 'Sponsorship', 'Equipment Hire', 'Electricity'],
12
  items: [expenseItem],
13
  totalVat: { type: Number, default: 0.00 },
14
  totalExVat: { type: Number, default: 0.00 },
15
  total: { type: Number, default: 0.00 }
16
})
17
18
expenseSchema.pre('save', function (next) {
19
  let overallTotal = 0.00
20
    // All the items should give the total
21
  this.items.forEach(function (item) {
22
    overallTotal = overallTotal + item.total
23
  })
24
25
  this.total = overallTotal
26
  next()
27
})
28
29
module.exports = mongoose.model('Expense', expenseSchema)
30