Skip to content

Commit

Permalink
Add main logic to modify and commit the ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandoagf committed May 21, 2021
1 parent 66508fd commit 7f6232b
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import connectDB from './config/db'
import LedgerUpdate from './models/LedgerUpdate'
import {
fetchModifiedUsers,
manager,
createDiscourseIdentity,
createGithubIdentity,
log
} from './utils'

const main = async (): Promise<void> => {
await connectDB()
const modifiedUsers = await fetchModifiedUsers()

await manager.reloadLedger()
const ledger = manager.ledger

for (const user of modifiedUsers) {
// Start of Ledger modifications logic
const { discordId, username, discourse, github } = user
log(`Updating ledger entry for ${username}`)

const discordIdentityId = ledger
.accountByAddress(`N\u0000sourcecred\u0000discord\u0000MEMBER\u0000user\u0000${discordId}\u0000`)
.identity.id

if (discourse) {
const discourseAccount = ledger
.accountByAddress(`N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.1hive.org\u0000${discourse}\u0000`)

const discourseIdentityId = discourseAccount ? discourseAccount.identity.id : createDiscourseIdentity(discourse, ledger)

if (discordIdentityId !== discourseIdentityId) {
try {
ledger.mergeIdentities({ base: discordIdentityId, target: discourseIdentityId })
log(`Merged identity ${discourseIdentityId} into ${discordIdentityId}`)
} catch (err) {
log(`An error occurred when trying to merge identity ${discourseIdentityId} into ${discordIdentityId}: ${err}`)
}
}
}

if (github) {
const githubAccount = ledger
.accountByAddress(`N\u0000sourcecred\u0000github\u0000USERLIKE\u0000USER\u0000${github}\u0000`)

const githubIdentityId = githubAccount ? githubAccount.identity.id : createGithubIdentity(github, ledger)

if (discordIdentityId !== githubIdentityId) {
try {
ledger.mergeIdentities({ base: discordIdentityId, target: githubIdentityId })
log(`Merged identity ${githubIdentityId} into ${discordIdentityId}`)
} catch (err) {
log(`An error occurred when trying to merge identity ${githubIdentityId} into ${discordIdentityId}: ${err}`)
}
}
}

ledger.activate(discordIdentityId)
// End of Ledger modifications logic
}

const persistRes = await manager.persist()

if(persistRes.error) log(`An error occurred when trying to commit the new ledger: ${persistRes.error}`)
else {
await LedgerUpdate.create({ modifiedAt: Date.now() })
log('Accounts successfully modified')
}
}

main()

0 comments on commit 7f6232b

Please sign in to comment.