-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.js
62 lines (51 loc) · 1.52 KB
/
models.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const _ = require('lodash')
const mergeModels = require('@tradle/merge-models')
const baseModels = require('./base-models')
const { categorizeApplicationModels } = require('./utils')
const stateModels = require('./state-models')
module.exports = ModelManager
function ModelManager ({ validate }) {
this.validate = validate
this.all = mergeModels()
.add(baseModels, { validate: false })
.get()
this.products = []
}
ModelManager.prototype.addProducts = function ({ models, products }) {
const newAllModels = _.extend({}, this.all, models ? models.all : {})
products.forEach(id => {
const model = newAllModels[id]
if (!model) {
throw new Error(`missing model for product ${id}`)
}
if (model.subClassOf !== 'tradle.FinancialProduct') {
throw new Error(`${id} is not a product!`)
}
})
this.products = _.uniq(products.concat(this.products || []))
this.biz = categorizeApplicationModels({
models: newAllModels,
products: this.products
})
if (models) {
['biz'].forEach(subset => {
if (!models[subset] || !models[subset].all) return
if (!this[subset]) {
this[subset] = {}
}
const all = _.extend(
{},
this[subset].all,
models[subset].all
)
this[subset] = _.extend({}, this[subset], models[subset])
this[subset].all = all
})
}
const opts = { validate: this.validate }
this.all = mergeModels()
.add(baseModels, opts)
// .add(this.private.all, opts)
.add(this.biz.all, opts)
.get()
}