liquid-funding/app/actions/profiles.js

63 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-01-16 20:50:49 +00:00
import { Q } from '@nozbe/watermelondb'
import database from '../db'
2019-01-19 18:12:36 +00:00
import { getLatestProfileEvents } from './lpEvents'
import { formatFundProfileEvent } from '../utils/events'
2019-01-16 20:50:49 +00:00
const profilesCollection = database.collections.get('profiles')
export const addProfile = async data => {
2019-01-18 19:46:03 +00:00
return await database.action(async () => {
2019-01-16 20:50:49 +00:00
const res = await profilesCollection.create(profile => {
2019-01-19 01:49:14 +00:00
const { id, addr, canceled, commitTime, type, name, url, idProfile } = data
profile.eventId = id
2019-01-18 19:46:03 +00:00
profile.addr = addr
profile.canceled = canceled
profile.commitTime = Number(commitTime)
profile.type = type
profile.name = name
profile.url = url
profile.idProfile = Number(idProfile)
2019-01-16 20:50:49 +00:00
})
return res
})
}
export const batchAddProfiles = async profiles => {
const batch = profiles.map(data => {
return profilesCollection.prepareCreate(profile => {
2019-01-19 01:49:14 +00:00
const { id, addr, canceled, commitTime, type, name, url, idProfile } = data
2019-01-19 18:12:36 +00:00
profile.eventId = id //TODO Possible FK relationship to LpEvent
2019-01-16 20:50:49 +00:00
profile.addr = addr
profile.canceled = canceled
2019-01-17 19:18:09 +00:00
profile.commitTime = Number(commitTime)
2019-01-16 20:50:49 +00:00
profile.type = type
profile.name = name
profile.url = url
2019-01-17 19:18:09 +00:00
profile.idProfile = Number(idProfile)
2019-01-16 20:50:49 +00:00
})
})
console.log({batch})
return await database.action(async () => await database.batch(...batch))
}
2019-01-19 18:12:36 +00:00
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
}
2019-01-16 20:50:49 +00:00
export const getProfileById = async id => {
const event = await profilesCollection.query(
Q.where('id_profile', id)
).fetch()
return event
}