diff --git a/src/adapters/localdb/models/counter-offer.js b/src/adapters/localdb/models/counter-offer.js index a8f8f3a..410c21f 100644 --- a/src/adapters/localdb/models/counter-offer.js +++ b/src/adapters/localdb/models/counter-offer.js @@ -4,8 +4,11 @@ const CounterOffer = new mongoose.Schema({ nostrEventId: { type: String, required: true }, // Nostr Event Id. takerAddr: { type: String, default: null }, takerNpub: { type: String, default: null }, + makerNpub: { type: String, default: null }, counterOfferAddr: { type: String, default: null }, - counterOfferUtxo: { type: String, default: null } + counterOfferUtxo: { type: String, default: null }, + takerOfferUtxo: { type: String, default: null }, + tokenId: { type: String, default: null } }) export default mongoose.model('counterOffer', CounterOffer) diff --git a/src/entities/counterOffer.js b/src/entities/counterOffer.js index 901daf0..41021ae 100644 --- a/src/entities/counterOffer.js +++ b/src/entities/counterOffer.js @@ -16,8 +16,11 @@ class CounterOfferEntity { nostrEventId, takerAddr, takerNpub, + makerNpub, counterOfferAddr, - counterOfferUtxo + counterOfferUtxo, + takerOfferUtxo, + tokenId } = offerData.data // Input Validation @@ -37,13 +40,25 @@ class CounterOfferEntity { if (counterOfferUtxo && typeof counterOfferUtxo !== 'string') { throw new Error("Property 'counterOfferUtxo' must be a string.") } + if (takerOfferUtxo && typeof takerOfferUtxo !== 'string') { + throw new Error("Property 'takerOfferUtxo' must be a string.") + } + if (tokenId && typeof tokenId !== 'string') { + throw new Error("Property 'tokenId' must be a string.") + } + if (makerNpub && typeof makerNpub !== 'string') { + throw new Error("Property 'makerNpub' must be a string.") + } const validatedCounterOfferData = { nostrEventId, takerAddr, takerNpub, counterOfferAddr, - counterOfferUtxo + counterOfferUtxo, + takerOfferUtxo, + tokenId, + makerNpub } return validatedCounterOfferData diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index b9675c5..a1a8f72 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -608,6 +608,8 @@ class OfferUseCases { // Create counter offer entity for nostr event id if it not exist const existingCounterOffer = await this.CounterOfferModel.findOne(({ nostrEventId: eventId })) if (!existingCounterOffer) { + // Add taker offer utxo to counter offer data. + offerData.data.takerOfferUtxo = offerData.data.utxoTxid const counterOEntity = this.counterOfferEntity.validate(offerData) // Create new counter offfer model and save it. const counterOffer = new this.CounterOfferModel(counterOEntity) diff --git a/test/unit/entities/counterOffer.entity.unit.js b/test/unit/entities/counterOffer.entity.unit.js index 893cc9e..8c1916a 100644 --- a/test/unit/entities/counterOffer.entity.unit.js +++ b/test/unit/entities/counterOffer.entity.unit.js @@ -11,7 +11,7 @@ let sandbox let uut describe('#CounterOffer-Entity', () => { - before(async () => {}) + before(async () => { }) beforeEach(() => { uut = new CounterOfferEntity() @@ -24,7 +24,7 @@ describe('#CounterOffer-Entity', () => { describe('#validate', () => { it('should throw an error if data is not provided', () => { try { - uut.validate({ }) + uut.validate({}) } catch (err) { assert.include(err.message, 'Input to counterOffer.validate() must be an object with a data property.') } @@ -59,12 +59,26 @@ describe('#CounterOffer-Entity', () => { assert.include(err.message, "Property 'takerNpub' must be a string") } }) + it('should throw an error if provided makerNpub is not string', () => { + try { + const inObj = { + nostrEventId: 'nostr event id', + takerAddr: 'taker address', + takerNpub: 'npub', + makerNpub: 1234 + } + uut.validate({ data: inObj }) + } catch (err) { + assert.include(err.message, "Property 'makerNpub' must be a string") + } + }) it('should throw an error if provided counterOfferAddr is not string', () => { try { const inObj = { nostrEventId: 'nostr event id', takerAddr: 'taker address', takerNpub: 'npub', + makerNpub: 'npub', counterOfferAddr: 1234 } uut.validate({ data: inObj }) @@ -78,6 +92,7 @@ describe('#CounterOffer-Entity', () => { nostrEventId: 'nostr event id', takerAddr: 'taker address', takerNpub: 'npub', + makerNpub: 'npub', counterOfferAddr: 'counter offer address', counterOfferUtxo: 1234 } @@ -86,13 +101,50 @@ describe('#CounterOffer-Entity', () => { assert.include(err.message, "Property 'counterOfferUtxo' must be a string") } }) + it('should throw an error if provided takerOfferUtxo is not string', () => { + try { + const inObj = { + nostrEventId: 'nostr event id', + takerAddr: 'taker address', + takerNpub: 'npub', + makerNpub: 'npub', + counterOfferAddr: 'counter offer address', + counterOfferUtxo: 'utxo txid', + takerOfferUtxo: 1234 + } + uut.validate({ data: inObj }) + } catch (err) { + assert.include(err.message, "Property 'takerOfferUtxo' must be a string") + } + }) + it('should throw an error if provided tokenId is not string', () => { + try { + const inObj = { + nostrEventId: 'nostr event id', + takerAddr: 'taker address', + takerNpub: 'npub', + makerNpub: 'npub', + counterOfferAddr: 'counter offer address', + counterOfferUtxo: 'utxo txid', + takerOfferUtxo: 'utxo txid', + tokenId: 1234 + } + uut.validate({ data: inObj }) + } catch (err) { + assert.include(err.message, "Property 'tokenId' must be a string") + } + }) + it('should return a counter offer object', () => { const inObj = { nostrEventId: 'nostr event id', takerAddr: 'taker address', takerNpub: 'npub', + makerNpub: 'npub', counterOfferAddr: 'counter offer address', - counterOfferUtxo: 'utxo txid' + counterOfferUtxo: 'utxo txid', + takerOfferUtxo: 'utxo txid', + tokenId: 'token id' } const counterOfferEntity = uut.validate({ data: inObj }) @@ -106,11 +158,20 @@ describe('#CounterOffer-Entity', () => { assert.property(counterOfferEntity, 'takerNpub') assert.equal(counterOfferEntity.takerNpub, inObj.takerNpub) + assert.property(counterOfferEntity, 'makerNpub') + assert.equal(counterOfferEntity.makerNpub, inObj.makerNpub) + assert.property(counterOfferEntity, 'counterOfferAddr') assert.equal(counterOfferEntity.counterOfferAddr, inObj.counterOfferAddr) assert.property(counterOfferEntity, 'counterOfferUtxo') assert.equal(counterOfferEntity.counterOfferUtxo, inObj.counterOfferUtxo) + + assert.property(counterOfferEntity, 'takerOfferUtxo') + assert.equal(counterOfferEntity.takerOfferUtxo, inObj.takerOfferUtxo) + + assert.property(counterOfferEntity, 'tokenId') + assert.equal(counterOfferEntity.tokenId, inObj.tokenId) }) }) })