From 4e58e3430becc921aafa6a11aaaa03e4223a2314 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Sat, 19 Jan 2019 13:12:36 -0500 Subject: [PATCH] sync profiles from LpEvents model --- app/actions/lpEvents.js | 18 ++++++++++++++++-- app/actions/profiles.js | 19 ++++++++++++++++++- app/model/lpEvents.js | 6 +++++- app/model/schema.js | 5 +++-- app/utils/events.js | 9 ++++----- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/app/actions/lpEvents.js b/app/actions/lpEvents.js index bc1a2e1..5e176b5 100644 --- a/app/actions/lpEvents.js +++ b/app/actions/lpEvents.js @@ -1,5 +1,6 @@ import { Q } from '@nozbe/watermelondb' import database from '../db' +import { GIVER_ADDED, DELEGATE_ADDED, PROJECT_ADDED } from '../utils/events' const lpCollection = database.collections.get('lp_events') export const addEvent = async data => { @@ -18,17 +19,30 @@ export const addEvent = async data => { export const batchAddEvents = async events => { const batch = events.map(e => { return lpCollection.prepareCreate(lpEvent => { - const { event, address, id, blockNumber } = e + const { event, address, id, blockNumber, returnValues } = e lpEvent.eventId = id lpEvent.address = address lpEvent.event = event lpEvent.blockNumber = blockNumber + lpEvent.returnValues = returnValues }) }) return await database.action(async () => await database.batch(...batch)) } -//TODO getProfileEvents +export const getLatestProfileEvents = async eventIds => { + const events = await lpCollection.query( + Q.where( + 'id', + Q.notIn(eventIds) + ), + Q.where( + 'event', + Q.oneOf([GIVER_ADDED, DELEGATE_ADDED, PROJECT_ADDED]) + ) + ).fetch() + return events +} export const getLpEventById = async id => { diff --git a/app/actions/profiles.js b/app/actions/profiles.js index 5923849..8ad4a10 100644 --- a/app/actions/profiles.js +++ b/app/actions/profiles.js @@ -1,5 +1,7 @@ import { Q } from '@nozbe/watermelondb' import database from '../db' +import { getLatestProfileEvents } from './lpEvents' +import { formatFundProfileEvent } from '../utils/events' const profilesCollection = database.collections.get('profiles') export const addProfile = async data => { @@ -23,7 +25,7 @@ export const batchAddProfiles = async profiles => { const batch = profiles.map(data => { return profilesCollection.prepareCreate(profile => { const { id, addr, canceled, commitTime, type, name, url, idProfile } = data - profile.eventId = id + profile.eventId = id //TODO Possible FK relationship to LpEvent profile.addr = addr profile.canceled = canceled profile.commitTime = Number(commitTime) @@ -37,6 +39,21 @@ export const batchAddProfiles = async profiles => { 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 batchAddProfiles(formattedEvents) +} + +export const getAllProfiles = async () => { + const events = await profilesCollection.query().fetch() + return events +} + export const getProfileById = async id => { const event = await profilesCollection.query( Q.where('id_profile', id) diff --git a/app/model/lpEvents.js b/app/model/lpEvents.js index 06708aa..a7e5b1f 100644 --- a/app/model/lpEvents.js +++ b/app/model/lpEvents.js @@ -1,6 +1,8 @@ import { Model } from '@nozbe/watermelondb' -import { action, field } from '@nozbe/watermelondb/decorators' +import { action, field, json } from '@nozbe/watermelondb/decorators' + +const sanitizeValues = json => json export default class LpEvent extends Model { static table = 'lp_events' @@ -12,6 +14,8 @@ export default class LpEvent extends Model { @field('block_number') blockNumber + @json('return_values', sanitizeValues) returnValues + @action async addEvent(data) { return await this.create(lpEvent => { const { event, address, id, blockNumber } = data diff --git a/app/model/schema.js b/app/model/schema.js index ec892fb..7899394 100644 --- a/app/model/schema.js +++ b/app/model/schema.js @@ -6,10 +6,11 @@ export default appSchema({ tableSchema({ name: 'lp_events', columns: [ - { name: 'event_id', type: 'string' }, + { name: 'event_id', type: 'string', isIndexed: true }, { name: 'address', type: 'string' }, - { name: 'event', type: 'string' }, + { name: 'event', type: 'string', isIndexed: true }, { name: 'block_number', type: 'number', isIndexed: true }, + { name : 'return_values', type: 'string', isOptional: true } ] }), tableSchema({ diff --git a/app/utils/events.js b/app/utils/events.js index 0d28ceb..bbf3350 100644 --- a/app/utils/events.js +++ b/app/utils/events.js @@ -5,9 +5,9 @@ import { getLastBlockStored } from '../actions/lpEvents' const AUTHORIZE_PAYMENT = 'AuthorizePayment' -const GIVER_ADDED = 'GiverAdded' -const DELEGATE_ADDED = 'DelegateAdded' -const PROJECT_ADDED = 'ProjectAdded' +export const GIVER_ADDED = 'GiverAdded' +export const DELEGATE_ADDED = 'DelegateAdded' +export const PROJECT_ADDED = 'ProjectAdded' const ALL_EVENTS = 'allEvents' const lookups = { [GIVER_ADDED]: { @@ -49,13 +49,12 @@ const getPastVaultEvents = async (event, raw = false) => { const { getPledgeAdmin } = LiquidPledging.methods export const formatFundProfileEvent = async event => { const lookup = lookups[event.event] - const { id, returnValues: { url, idProject } } = event + const { id, returnValues: { url } } = event const idProfile = event.returnValues[lookup.id] const { addr, commitTime, name, canceled } = await getPledgeAdmin(idProfile).call() return { id, idProfile, - idProject, url, commitTime, name,