diff --git a/src/controllers/offer.controller.js b/src/controllers/offer.controller.js index 0e02b10e..b15277fc 100644 --- a/src/controllers/offer.controller.js +++ b/src/controllers/offer.controller.js @@ -1,3 +1,5 @@ +import _ from 'lodash'; + import { Meta, Staging } from '../models'; import { @@ -14,6 +16,9 @@ import { import { deserializeMaker, deserializeTaker } from '../utils/datalayer-utils'; import * as datalayer from '../datalayer/persistance'; +import { getConfig } from '../utils/config-loader'; + +const CONFIG = getConfig().APP; export const generateOfferFile = async (req, res) => { try { @@ -77,12 +82,16 @@ export const importOfferFile = async (req, res) => { const offerFileBuffer = req.files.file.data; const offerFile = offerFileBuffer.toString('utf-8'); + const offerParsed = JSON.parse(offerFile); + offerParsed.fee = _.get(CONFIG, 'DEFAULT_FEE', 300000000); + delete offerParsed.success; + const offerJSON = JSON.stringify(offerParsed); - await datalayer.verifyOffer(offerFile); + await datalayer.verifyOffer(offerJSON); await Meta.upsert({ metaKey: 'activeOffer', - metaValue: offerFile, + metaValue: offerJSON, }); res.json({ @@ -106,8 +115,12 @@ export const commitImportedOfferFile = async (req, res) => { await assertWalletIsSynced(); await assertNoPendingCommits(); - const offerFile = Meta.findOne({ where: { metaKey: 'activeOffer' } }); - const response = await datalayer.takeOffer(offerFile); + const offerFile = await Meta.findOne({ + where: { metaKey: 'activeOffer' }, + raw: true, + }); + + const response = await datalayer.takeOffer(JSON.parse(offerFile.metaValue)); res.json({ message: 'Offer Accepted.', diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index e8bddbf0..e03c7d50 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -319,11 +319,7 @@ class Organization extends Model { allSubscribedOrganizations.map((organization) => { const onResult = (data) => { const updateData = data - .filter( - (dataEntry) => - dataEntry.key !== 'registryId' || - !dataEntry.key.includes('meta_'), - ) + .filter((pair) => !pair.key.includes('meta_')) .reduce((update, current) => { update[current.key] = current.value; return update; @@ -331,14 +327,14 @@ class Organization extends Model { // will return metadata fields. i.e.: { meta_key1: 'value1', meta_key2: 'value2' } const metadata = data - .filter((dataEntry) => dataEntry.key.includes('meta_')) + .filter((pair) => pair.key.includes('meta_')) .reduce((update, current) => { update[current.key] = current.value; return update; }, {}); Organization.update( - { ...updateData, metadata }, + { ..._.omit(updateData, ['registryId']), metadata }, { where: { orgUid: organization.orgUid }, }, diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index f0dcb626..75d7d1aa 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -54,13 +54,18 @@ class Staging extends Model { raw: true, }); + // The record still has the orgUid of the makerProjectRecord, + // we will update this to the correct orgUId later + const takerOrganization = await Organization.findOne({ + where: { orgUid: makerProjectRecord.orgUid }, + raw: true, + }); + const maker = { inclusions: [] }; const taker = { inclusions: [] }; - // The record still has the orgUid of the makerProjectRecord, - // we will update this to the correct orgUId later - taker.storeId = makerProjectRecord.orgUid; - maker.storeId = myOrganization.orgUid; + taker.storeId = takerOrganization.registryId; + maker.storeId = myOrganization.registryId; const takerProjectRecord = await Project.findOne({ where: { warehouseProjectId: makerProjectRecord.warehouseProjectId }, @@ -223,9 +228,7 @@ class Staging extends Model { metaValue: offerResponse.offer.trade_id, }); - console.log(offerResponse); - - return _.omit(offerResponse, ['status']); + return _.omit(offerResponse, ['success']); } catch (error) { throw new Error(error.message); }