-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from thematters/issue-mat-master
Add migration script for issuing MAT. (master)
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const compact = require('lodash/compact') | ||
|
||
const uuidv4 = require('uuid/v4') | ||
|
||
const table = 'transaction' | ||
|
||
const purpose = 'system-subsidy' | ||
|
||
exports.up = async knex => { | ||
const items = [ | ||
{ | ||
userName: 'lalaland', | ||
role: 'admin', | ||
amount: 2000 | ||
}, | ||
{ | ||
userName: 'yingshinlee', | ||
role: 'admin', | ||
amount: 2000 | ||
}, | ||
{ | ||
userName: 'satsuki', | ||
role: 'admin', | ||
amount: 2000 | ||
}, | ||
{ | ||
userName: 'robertu', | ||
role: 'user', | ||
amount: 2000 | ||
}, | ||
{ | ||
userName: 'guo', | ||
role: 'admin', | ||
amount: 2000 | ||
}, | ||
{ | ||
userName: 'baize417', | ||
role: 'admin', | ||
amount: 1000 | ||
}, | ||
{ | ||
userName: 'andyischaos', | ||
role: 'admin', | ||
amount: 500 | ||
} | ||
] | ||
|
||
const users = compact( | ||
await Promise.all( | ||
items.map(async ({ userName, role, amount }) => { | ||
const user = await knex('user') | ||
.select('id') | ||
.where({ user_name: userName, role }) | ||
.first() | ||
if (user) { | ||
return { id: user.id, amount } | ||
} | ||
}) | ||
) | ||
) | ||
|
||
const result = await Promise.all( | ||
users.map(({ id, amount }) => | ||
knex(table) | ||
.insert({ | ||
uuid: uuidv4(), | ||
recipient_id: id, | ||
reference_id: id, | ||
purpose, | ||
amount | ||
}) | ||
.into(table) | ||
.returning(['id', 'recipient_id', 'amount']) | ||
) | ||
) | ||
|
||
console.log(result) | ||
} | ||
|
||
exports.down = () => {} |