diff --git a/app/actions/pledges.js b/app/actions/pledges.js new file mode 100644 index 0000000..6a2119a --- /dev/null +++ b/app/actions/pledges.js @@ -0,0 +1,56 @@ +import { pick } from 'ramda' +import { Q } from '@nozbe/watermelondb' +import database from '../db' +import { getLatestProfileEvents } from './lpEvents' +import { formatFundProfileEvent } from '../utils/events' + +const createPledge = (pledge, data) => { + const { id, owner, amount, token, commitTime, nDelegates, pledgeState, intendedProject } = data + pledge.pledgeId = id + pledge.owner = Number(owner) + pledge.amount = Number(amount) + pledge.token = token + pledge.commitTime = Number(commitTime) + pledge.nDelegates = Number(nDelegates) + pledge.pledgeState = pledgeState + pledge.intendedProject = Number(intendedProject) + pledge.profile.set(Number(id)) +} + +const pledgesCollection = database.collections.get('pledges') +export const addPledge = async data => { + return await database.action(async () => { + const res = await pledgesCollection.create(pledge => createPledge(pledge, data)) + return res + }) +} + +export const batchAddPledges = async profiles => { + const batch = profiles.map(data => { + return pledgesCollection.prepareCreate(pledge => createPledge(pledge, data)) + }) + 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) +} + +export const getAllProfiles = async () => { + const events = await pledgesCollection.query().fetch() + return events +} + +export const getPledgeById = async id => { + const event = await pledgesCollection.query( + Q.where('id_profile', id) + ).fetch() + return event +} diff --git a/app/db.js b/app/db.js index c5a290b..39a2def 100644 --- a/app/db.js +++ b/app/db.js @@ -4,6 +4,7 @@ import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs' import schema from './model/schema' import LpEvent from './model/lpEvents' import Profile from './model/profile' +import Pledge from './model/pledge' const dbName = 'LiquidFunding' const adapter = new LokiJSAdapter({ @@ -15,7 +16,8 @@ const database = new Database({ adapter, modelClasses: [ LpEvent, - Profile + Profile, + Pledge ], actionsEnabled: true, }) diff --git a/app/model/pledge.js b/app/model/pledge.js new file mode 100644 index 0000000..6e13a58 --- /dev/null +++ b/app/model/pledge.js @@ -0,0 +1,21 @@ +import { action, field, relation } from '@nozbe/watermelondb/decorators' +import { LiquidModel } from '../utils/models' + + +export default class Pledge extends LiquidModel { + static table = 'pledges' + static associations = { + profiles: { type: 'belongs_to', key: 'id_profile' }, + } + + @field('pledge_id') pledgeId + @field('owner_id') owner + @field('amount') amount + @field('token') token + @field('commit_time') commitTime + @field('n_delegates') nDelegates + @field('intended_project') intendedProject + @field('pledge_state') pledgeState + @relation('profiles', 'id_profile') profile + +} diff --git a/app/model/profile.js b/app/model/profile.js index 04149f5..4843692 100644 --- a/app/model/profile.js +++ b/app/model/profile.js @@ -1,9 +1,12 @@ -import { action, field } from '@nozbe/watermelondb/decorators' +import { action, field, children } from '@nozbe/watermelondb/decorators' import { LiquidModel } from '../utils/models' export default class Profile extends LiquidModel { static table = 'profiles' + static associations = { + pledges: { type: 'has_many', foreignKey: 'id_profile' } + } @field('addr') addr @field('event_id') eventId @@ -13,6 +16,7 @@ export default class Profile extends LiquidModel { @field('name') name @field('url') url @field('id_profile') idProfile + @children('pledges') pledges @action async markAsCanceled() { await this.update(profile => { diff --git a/app/model/schema.js b/app/model/schema.js index 7899394..235f238 100644 --- a/app/model/schema.js +++ b/app/model/schema.js @@ -23,9 +23,22 @@ export default appSchema({ { name: 'type', type: 'string' }, { name: 'name', type: 'string' }, { name: 'url', type: 'string' }, - { name: 'id_profile', type: 'number' } + { name: 'id_profile', type: 'number', isIndexed: true } + ] + }), + tableSchema({ + name: 'pledges', + columns: [ + { name: 'pledge_id', type: 'number' }, + { name: 'owner_id', type: 'number', isIndexed: true }, + { name: 'amount', type: 'number' }, + { name: 'token', type: 'string' }, + { name: 'commit_time', type: 'number' }, + { name: 'n_delegates', type: 'number' }, + { name: 'intended_project', type: 'number' }, + { name: 'pledge_state', type: 'number' }, + { name: 'id_profile', type: 'number', isIndexed: true } ] - }) ] })