sync profiles from LpEvents model

This commit is contained in:
Barry Gitarts 2019-01-19 13:12:36 -05:00
parent ac844c4722
commit 4e58e3430b
5 changed files with 46 additions and 11 deletions

View File

@ -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 => {

View File

@ -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)

View File

@ -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

View File

@ -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({

View File

@ -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,