diff --git a/app/actions/pledges.js b/app/actions/pledges.js index 6a2119a..4803e1b 100644 --- a/app/actions/pledges.js +++ b/app/actions/pledges.js @@ -1,11 +1,11 @@ -import { pick } from 'ramda' import { Q } from '@nozbe/watermelondb' import database from '../db' -import { getLatestProfileEvents } from './lpEvents' -import { formatFundProfileEvent } from '../utils/events' +import { getAllPledges } from '../utils/pledges' +import { getProfilesById } from './profiles' -const createPledge = (pledge, data) => { +const createPledge = (pledge, data, profiles) => { const { id, owner, amount, token, commitTime, nDelegates, pledgeState, intendedProject } = data + const profile = profiles.find(p => p.idProfile == owner) pledge.pledgeId = id pledge.owner = Number(owner) pledge.amount = Number(amount) @@ -14,7 +14,7 @@ const createPledge = (pledge, data) => { pledge.nDelegates = Number(nDelegates) pledge.pledgeState = pledgeState pledge.intendedProject = Number(intendedProject) - pledge.profile.set(Number(id)) + pledge.profile.id = profile.id } const pledgesCollection = database.collections.get('pledges') @@ -25,25 +25,30 @@ export const addPledge = async data => { }) } -export const batchAddPledges = async profiles => { - const batch = profiles.map(data => { - return pledgesCollection.prepareCreate(pledge => createPledge(pledge, data)) +export const batchAddPledges = async (pledges, profiles = []) => { + const batch = pledges.map(data => { + return pledgesCollection.prepareCreate(pledge => createPledge(pledge, data, profiles)) }) console.log({batch}) return await database.action(async () => await database.batch(...batch)) } -export const addFormattedProfiles = async () => { - const allProfiles = await getAllProfiles() - const allEventIds = allProfiles.map(p => p.eventId) - const events = await getLatestProfileEvents(allEventIds) - const formattedEvents = await Promise.all( - events.map(formatFundProfileEvent) - ) - await batchAddPledges(formattedEvents) +const getLastPledge = pledges => { + const pledgeId = pledges.length + ? pledges.sort((a,b) => b.pledgeId - a.pledgeId)[0].pledgeId + : 1 + return pledgeId +} +export const getAndAddPledges = async () => { + const pledges = await getLocalPledges() + const pledgeId = getLastPledge(pledges) + const newPledges = await getAllPledges(pledgeId + 1) + const pledgeIds = newPledges.map(p => p.owner) + const profiles = await getProfilesById(pledgeIds) + batchAddPledges(newPledges, profiles) } -export const getAllProfiles = async () => { +export const getLocalPledges = async () => { const events = await pledgesCollection.query().fetch() return events } diff --git a/app/actions/profiles.js b/app/actions/profiles.js index 8ad4a10..7cf8f2d 100644 --- a/app/actions/profiles.js +++ b/app/actions/profiles.js @@ -60,3 +60,14 @@ export const getProfileById = async id => { ).fetch() return event } + +export const getProfilesById = async ids => { + const event = await profilesCollection.query( + Q.where( + 'id_profile', + Q.oneOf(ids) + ) + ).fetch() + return event +} + diff --git a/app/model/pledge.js b/app/model/pledge.js index 6e13a58..676a2a1 100644 --- a/app/model/pledge.js +++ b/app/model/pledge.js @@ -5,7 +5,7 @@ import { LiquidModel } from '../utils/models' export default class Pledge extends LiquidModel { static table = 'pledges' static associations = { - profiles: { type: 'belongs_to', key: 'id_profile' }, + profiles: { type: 'belongs_to', key: 'profile_id' }, } @field('pledge_id') pledgeId @@ -16,6 +16,6 @@ export default class Pledge extends LiquidModel { @field('n_delegates') nDelegates @field('intended_project') intendedProject @field('pledge_state') pledgeState - @relation('profiles', 'id_profile') profile + @relation('profiles', 'profile_id') profile } diff --git a/app/model/profile.js b/app/model/profile.js index 4843692..e531b33 100644 --- a/app/model/profile.js +++ b/app/model/profile.js @@ -5,7 +5,7 @@ import { LiquidModel } from '../utils/models' export default class Profile extends LiquidModel { static table = 'profiles' static associations = { - pledges: { type: 'has_many', foreignKey: 'id_profile' } + pledges: { type: 'has_many', foreignKey: 'profile_id' } } @field('addr') addr diff --git a/app/model/schema.js b/app/model/schema.js index 235f238..b7f75db 100644 --- a/app/model/schema.js +++ b/app/model/schema.js @@ -37,7 +37,7 @@ export default appSchema({ { name: 'n_delegates', type: 'number' }, { name: 'intended_project', type: 'number' }, { name: 'pledge_state', type: 'number' }, - { name: 'id_profile', type: 'number', isIndexed: true } + { name: 'profile_id', type: 'string', isIndexed: true } ] }) ]