-
Notifications
You must be signed in to change notification settings - Fork 1
/
application-mixin.js
39 lines (33 loc) · 1.21 KB
/
application-mixin.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
const co = require('co').wrap
const { PREVLINK } = require('@tradle/constants')
const buildResource = require('@tradle/build-resource')
const { parseStub } = require('./utils')
module.exports = function applicationMixin (target) {
/**
* update PERMALINK, PREVLINK on application, save new application version
*/
target.saveNewVersionOfApplication = function ({ user, application }) {
return this.createNewVersionOfApplication(application)
.then(application => this.saveApplication({ user, application }))
}
target.createNewVersionOfApplication = function (application) {
application = buildResource.version(application)
this.state.updateApplication({
application,
properties: {
dateModified: Date.now()
}
})
return target.sign(application)
}
target.saveApplication = co(function* ({ user, application }) {
// application._time = application.dateModified
if (!user) {
const { permalink } = parseStub(application.applicant)
user = yield this.bot.users.get(permalink)
}
this.state.updateApplicationStub({ user, application })
// const method = application[PREVLINK] ? 'update' : 'save'
return this.bot.save(application)
})
}