Skip to content

Commit

Permalink
fix: add offers edits
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Sep 12, 2022
1 parent ed4a1a7 commit 6d28378
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
21 changes: 17 additions & 4 deletions src/controllers/offer.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

import { Meta, Staging } from '../models';

import {
Expand All @@ -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 {
Expand Down Expand Up @@ -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({
Expand All @@ -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.',
Expand Down
10 changes: 3 additions & 7 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,26 +319,22 @@ 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;
}, {});

// 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 },
},
Expand Down
17 changes: 10 additions & 7 deletions src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 6d28378

Please sign in to comment.