This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmanuallyMatchPostfinancePayment.js
61 lines (53 loc) · 1.79 KB
/
manuallyMatchPostfinancePayment.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
const Roles = require('../../../lib/Roles')
const logger = require('../../../lib/logger')
module.exports = async (_, args, {pgdb, req, t}) => {
Roles.ensureUserHasRole(req.user, 'supporter')
const { id } = args
const now = new Date()
const transaction = await pgdb.transactionBegin()
try {
const pfp = await transaction.public.postfinancePayments.findOne({id})
if (!pfp) {
logger.error('postfinancePayment not found', { req: req._log(), args })
throw new Error(t('api/payment/404'))
}
if (pfp.matched) {
logger.error('postfinacePayment is matched already', { req: req._log(), args })
throw new Error(t('api/postfinancePayment/match/noChange'))
}
const payments = await transaction.public.payments.find({
hrid: pfp.mitteilung,
status: 'PAID'
})
if (payments.length < 1) {
logger.error('can not set pfp to paid if no related PAID payment is found', {
req: req._log(),
args
})
throw new Error(t('api/postfinancePayment/match/noPayment', {hrid: pfp.mitteilung}))
}
const pfp2 = transaction.public.postfinancePayments.find({
'id !=': id,
matched: true
})
if (pfp2.length) {
logger.error('another postfinancePayment with the same hrid is matched already', {
req: req._log(),
args
})
throw new Error(t('api/postfinancePayment/match/duplicate', {hrid: pfp.mitteilung}))
}
await transaction.public.postfinancePayments.updateOne({
id
}, {
matched: true,
updatedAt: now
})
await transaction.transactionCommit()
} catch (e) {
await transaction.transactionRollback()
logger.info('transaction rollback', { req: req._log(), args, error: e })
throw e
}
return pgdb.public.postfinancePayments.findOne({id})
}