mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
feat(entry): Created 'Entry' Entity, Use Case, and Adapter
This commit is contained in:
@@ -4,11 +4,12 @@
|
||||
|
||||
// Load Mongoose models.
|
||||
const Users = require('./models/users')
|
||||
|
||||
const Entry = require('./models/entry')
|
||||
class LocalDB {
|
||||
constructor () {
|
||||
// Encapsulate dependencies
|
||||
this.Users = Users
|
||||
this.Entry = Entries
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const Entry = new mongoose.Schema({
|
||||
entry: { type: String },
|
||||
description: { type: String },
|
||||
slpAddress: { type: String },
|
||||
signature: { type: String },
|
||||
category: { type: String },
|
||||
balance: { type: Number },
|
||||
merit: { type: Number }
|
||||
})
|
||||
|
||||
module.exports = mongoose.model('entry', Entry)
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
Entry Entity
|
||||
*/
|
||||
class Entry {
|
||||
validate ({
|
||||
entry,
|
||||
description,
|
||||
slpAddress,
|
||||
signature,
|
||||
category
|
||||
} = {}) {
|
||||
// Input Validation
|
||||
if (!entry || typeof entry !== 'string') {
|
||||
throw new Error("Property 'entry' must be a string!")
|
||||
}
|
||||
if (!description || typeof description !== 'string') {
|
||||
throw new Error("Property 'description' must be a string!")
|
||||
}
|
||||
if (!slpAddress || typeof slpAddress !== 'string') {
|
||||
throw new Error("Property 'slpAddress' must be a string!")
|
||||
}
|
||||
if (!signature || typeof signature !== 'string') {
|
||||
throw new Error("Property 'signature' must be a string!")
|
||||
}
|
||||
if (!category || typeof category !== 'string') {
|
||||
throw new Error("Property 'category' must be a string!")
|
||||
}
|
||||
|
||||
const entryData = {
|
||||
entry,
|
||||
description,
|
||||
slpAddress,
|
||||
signature,
|
||||
category
|
||||
}
|
||||
|
||||
return entryData
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Entry
|
||||
@@ -0,0 +1,97 @@
|
||||
const { wlogger } = require('../adapters/wlogger')
|
||||
|
||||
class EntryLib {
|
||||
constructor (localConfig = {}) {
|
||||
// console.log('User localConfig: ', localConfig)
|
||||
this.adapters = localConfig.adapters
|
||||
if (!this.adapters) {
|
||||
throw new Error(
|
||||
'Instance of adapters must be passed in when instantiating User Use Cases library.'
|
||||
)
|
||||
}
|
||||
|
||||
// Encapsulate dependencies
|
||||
this.EntryModel = this.adapters.localdb.Entries
|
||||
}
|
||||
|
||||
// Create a new entry model and add it to the Mongo database.
|
||||
async createEntry (entryObj) {
|
||||
try {
|
||||
const {
|
||||
entry,
|
||||
description,
|
||||
slpAddress,
|
||||
signature,
|
||||
category
|
||||
} = entryObj
|
||||
|
||||
// Input Validation
|
||||
if (!entry || typeof entry !== 'string') {
|
||||
throw new Error("Property 'entry' must be a string!")
|
||||
}
|
||||
if (!description || typeof description !== 'string') {
|
||||
throw new Error("Property 'description' must be a string!")
|
||||
}
|
||||
if (!slpAddress || typeof slpAddress !== 'string') {
|
||||
throw new Error("Property 'slpAddress' must be a string!")
|
||||
}
|
||||
if (!signature || typeof signature !== 'string') {
|
||||
throw new Error("Property 'signature' must be a string!")
|
||||
}
|
||||
if (!category || typeof category !== 'string') {
|
||||
throw new Error("Property 'category' must be a string!")
|
||||
}
|
||||
|
||||
let psfBalance
|
||||
|
||||
try {
|
||||
// Verify that the entry was signed by a specific BCH address.
|
||||
const isValidSignature = _this.bchjs._verifySignature(body)
|
||||
if (!isValidSignature) {
|
||||
throw new Error('Invalid signature')
|
||||
}
|
||||
|
||||
// Verify psf tokens balance
|
||||
if (_this.env === 'test') {
|
||||
psfBalance = 100
|
||||
} else {
|
||||
psfBalance = 100// await _this.bchjs.getPSFTokenBalance(slpAddress)
|
||||
}
|
||||
|
||||
if (psfBalance < 10) {
|
||||
throw new Error('Insufficient psf balance')
|
||||
}
|
||||
} catch (err) {
|
||||
ctx.throw(406, err.message)
|
||||
}
|
||||
let merit
|
||||
if (_this.env === 'test') {
|
||||
merit = 100
|
||||
} else {
|
||||
merit = 100// await _this.bchjs.getMerit(slpAddress)
|
||||
}
|
||||
|
||||
const updatedEntry = {
|
||||
entry: entry.trim(),
|
||||
slpAddress: slpAddress.trim(),
|
||||
description: description.trim(),
|
||||
signature: signature.trim(),
|
||||
category: category.trim(),
|
||||
balance: psfBalance,
|
||||
merit
|
||||
}
|
||||
|
||||
const entryModel = new this.EntryModel(updatedEntry)
|
||||
|
||||
await entryModel.save()
|
||||
|
||||
return { id: entry._id.toString() }
|
||||
} catch (err) {
|
||||
// console.log('createUser() error: ', err)
|
||||
wlogger.error('Error in use-cases/entry.js/createEntry()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EntryLib
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
const UserUseCases = require('./user')
|
||||
const EntryUseCases = require('./entry')
|
||||
|
||||
class UseCases {
|
||||
constructor (localConfig = {}) {
|
||||
@@ -17,6 +18,7 @@ class UseCases {
|
||||
|
||||
// console.log('use-cases/index.js localConfig: ', localConfig)
|
||||
this.user = new UserUseCases(localConfig)
|
||||
this.entry = new EntryUseCases(localConfig)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user