-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-refund-claims.js
33 lines (31 loc) · 1019 Bytes
/
create-refund-claims.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
const Papa = require('papaparse')
const { refund: fields } = require('./fields')
module.exports = createRefundClaims
// use legacy true so that browsers auto-claim them
const insertRefunds = `
insert into claims (promotion_id, wallet_id, approximate_value, legacy_claimed)
values ($1, $2, $3, true)
`
async function createRefundClaims({ csvCorrected, promotionId }, refunds, client) {
await Promise.all(refunds.map(async (datum) => {
const paymentId = datum[fields.paymentId]
const amount = datum[fields.amount]
try {
await client.query(insertRefunds, [
promotionId,
paymentId,
amount
])
} catch (e) {
if (e && e.code === '23505') {
return console.log(`wallet ${paymentId} has already claimed refund promotion`)
} else {
throw e
}
} finally {
refund[fields.status] = 'DONE'
}
}))
fs.writeFileSync(csvCorrected, Papa.unparse(refunds))
console.log('please copy the `.corrected` file back to the spreadsheet')
}