add create project when when new id

This commit is contained in:
Barry Gitarts 2019-04-19 14:51:47 -04:00 committed by Barry G
parent d462929d34
commit 0b5da9d3a8
3 changed files with 24 additions and 4 deletions

View File

@ -4,6 +4,20 @@ import database from '../db'
import { getPledges, getAllPledges } from '../utils/pledges' import { getPledges, getAllPledges } from '../utils/pledges'
import { getProfilesById } from './profiles' import { getProfilesById } from './profiles'
export const createPledgeFromFunding = (pledge, newId, amount, oldPledge, project = 0) => {
const { owner, token, commitTime, nDelegates, pledgeState, delegates, profile } = oldPledge
pledge.idPledge = Number(newId)
pledge.owner = Number(owner)
pledge.amount = amount
pledge.token = token
pledge.commitTime = Number(commitTime)
pledge.nDelegates = Number(nDelegates)
pledge.pledgeState = Number(pledgeState)
pledge.intendedProject = Number(project)
pledge.delegates = delegates
pledge.profile.set(profile)
}
const createPledge = (pledge, data, profiles) => { const createPledge = (pledge, data, profiles) => {
const { id, owner, amount, blockNumber, token, commitTime, nDelegates, pledgeState, intendedProject, delegates } = data const { id, owner, amount, blockNumber, token, commitTime, nDelegates, pledgeState, intendedProject, delegates } = data
const profile = profiles.find(p => p.idProfile == owner) const profile = profiles.find(p => p.idProfile == owner)

View File

@ -70,11 +70,11 @@ const SubmissionSection = ({ classes, profiles, delegatePledges, projectId, open
if (Array.isArray(Transfer)) { if (Array.isArray(Transfer)) {
Transfer.forEach(async t => { Transfer.forEach(async t => {
const { to, amount } = t.returnValues const { to, amount } = t.returnValues
await pledge.transferTo(to, amount) await pledge.transferTo(to, amount, projectId)
}) })
} else { } else {
const { to, amount } = Transfer.returnValues const { to, amount } = Transfer.returnValues
await pledge.transferTo(to, amount) await pledge.transferTo(to, amount, projectId)
} }
}) })
.catch(e => { .catch(e => {

View File

@ -1,6 +1,7 @@
import { action, field, relation, json } from '@nozbe/watermelondb/decorators' import { action, field, relation, json } from '@nozbe/watermelondb/decorators'
import { Q } from '@nozbe/watermelondb' import { Q } from '@nozbe/watermelondb'
import { LiquidModel } from '../utils/models' import { LiquidModel } from '../utils/models'
import { createPledgeFromFunding } from '../actions/pledges'
const sanitizeValues = json => json const sanitizeValues = json => json
export default class Pledge extends LiquidModel { export default class Pledge extends LiquidModel {
@ -21,8 +22,7 @@ export default class Pledge extends LiquidModel {
@relation('profiles', 'profile_id') profile @relation('profiles', 'profile_id') profile
@json('delegates', sanitizeValues) delegates @json('delegates', sanitizeValues) delegates
@action async transferTo(to, amount) { @action async transferTo(to, amount, projectId) {
//TODO check if exists then update or create.
const toPledgeQuery = await this.collections.get('pledges').query( const toPledgeQuery = await this.collections.get('pledges').query(
Q.where('pledge_id', to) Q.where('pledge_id', to)
).fetch() ).fetch()
@ -38,6 +38,12 @@ export default class Pledge extends LiquidModel {
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString() pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
}) })
) )
} else {
args.push(
this.prepareCreate(pledge => {
createPledgeFromFunding(pledge, to, amount, this, projectId)
})
)
} }
await this.batch(...args) await this.batch(...args)
} }