From 5fc26dcc7cb605ca155ceca72ea87a6361f826ec Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 9 Mar 2022 14:25:37 -0800 Subject: [PATCH] fix(order): Adding 'orderStatus' to Order model --- src/adapters/localdb/models/order.js | 3 ++- src/entities/order.js | 22 ++++++++++------------ src/use-cases/order/index.js | 3 +++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/adapters/localdb/models/order.js b/src/adapters/localdb/models/order.js index e8119b1..cf85686 100644 --- a/src/adapters/localdb/models/order.js +++ b/src/adapters/localdb/models/order.js @@ -17,7 +17,8 @@ const Order = new mongoose.Schema({ timestamp: { type: String }, localTimestamp: { type: String }, p2wdbTxid: { type: String }, - p2wdbHash: { type: String } + p2wdbHash: { type: String }, + orderStatus: { type: String } }) module.exports = mongoose.model('order', Order) diff --git a/src/entities/order.js b/src/entities/order.js index 040af00..3bb1987 100644 --- a/src/entities/order.js +++ b/src/entities/order.js @@ -4,6 +4,10 @@ It's destroyed when the UTXO described in the Signal has been detected as spent. */ class OrderEntity { + constructor () { + this.orderStatus = ['posted', 'taken', 'completed'] + } + validate (orderData = {}) { // Throw an error if input object does not have a data property if (!orderData.data) { @@ -12,17 +16,7 @@ class OrderEntity { ) } - const { - messageType, - messageClass, - tokenId, - buyOrSell, - rateInSats, - minSatsToExchange, - numTokens, - utxoTxid, - utxoVout - } = orderData.data + const {messageType, messageClass, tokenId, buyOrSell, rateInSats, minSatsToExchange, numTokens, utxoTxid, utxoVout, orderStatus} = orderData.data // Input Validation if (!messageType || typeof messageType !== 'number') { @@ -52,6 +46,9 @@ class OrderEntity { if (typeof utxoVout !== 'number') { throw new Error("Property 'utxoVout' must be an integer number.") } + if (orderStatus && !this.orderStatus.includes(orderStatus)) { + throw new Error("Property 'orderStatus' must be a valid string") + } const validatedOrderData = { messageType, @@ -66,7 +63,8 @@ class OrderEntity { timestamp: orderData.timestamp, localTimestamp: orderData.localTimeStamp, txid: orderData.txid, - p2wdbHash: orderData.hash + p2wdbHash: orderData.hash, + orderStatus: orderStatus || this.orderStatus[0] } return validatedOrderData diff --git a/src/use-cases/order/index.js b/src/use-cases/order/index.js index 9ee758c..cc5331c 100644 --- a/src/use-cases/order/index.js +++ b/src/use-cases/order/index.js @@ -43,6 +43,9 @@ class OrderUseCases { console.log('utxoStatus: ', utxoStatus) if (utxoStatus === null) return false + // A new order gets a status of 'posted' + orderObj.data.orderStatus = 'posted' + const orderEntity = this.orderEntity.validate(orderObj) console.log('orderEntity: ', orderEntity)