From b804a2450417af9191c9ebb5df3c0ba327aa5f71 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Thu, 29 Jul 2021 19:51:45 -0400 Subject: [PATCH] feat(entry): Created 'Entry' Entity, Use Case, and Adapter --- src/adapters/localdb/index.js | 3 +- src/adapters/localdb/models/entry.js | 13 ++++ src/entities/entry.js | 41 ++++++++++++ src/use-cases/entry.js | 97 ++++++++++++++++++++++++++++ src/use-cases/index.js | 2 + 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/adapters/localdb/models/entry.js create mode 100644 src/entities/entry.js create mode 100644 src/use-cases/entry.js diff --git a/src/adapters/localdb/index.js b/src/adapters/localdb/index.js index 1589241..528e9cf 100644 --- a/src/adapters/localdb/index.js +++ b/src/adapters/localdb/index.js @@ -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 } } diff --git a/src/adapters/localdb/models/entry.js b/src/adapters/localdb/models/entry.js new file mode 100644 index 0000000..0db1355 --- /dev/null +++ b/src/adapters/localdb/models/entry.js @@ -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) diff --git a/src/entities/entry.js b/src/entities/entry.js new file mode 100644 index 0000000..c94cb26 --- /dev/null +++ b/src/entities/entry.js @@ -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 diff --git a/src/use-cases/entry.js b/src/use-cases/entry.js new file mode 100644 index 0000000..911bff6 --- /dev/null +++ b/src/use-cases/entry.js @@ -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 diff --git a/src/use-cases/index.js b/src/use-cases/index.js index 015368f..4d7f980 100644 --- a/src/use-cases/index.js +++ b/src/use-cases/index.js @@ -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) } }