From 35fc1249b09e5da2cf5faac097553b15b53dbf8b Mon Sep 17 00:00:00 2001 From: Daniel Stefan Date: Sat, 3 Sep 2022 17:18:40 +0300 Subject: [PATCH] fix: offer fixes --- src/controllers/offer.controller.js | 2 +- src/models/staging/staging.model.js | 278 ++++++++++++++-------------- 2 files changed, 145 insertions(+), 135 deletions(-) diff --git a/src/controllers/offer.controller.js b/src/controllers/offer.controller.js index 146ab5d8..3eaad3d0 100644 --- a/src/controllers/offer.controller.js +++ b/src/controllers/offer.controller.js @@ -39,7 +39,7 @@ export const cancelActiveOffer = async (req, res) => { throw new Error(`There is no active offer to cancel`); } - await datalayer.cancelActiveOffer(activeOffer.metaValue); + await datalayer.cancelOffer(activeOffer.metaValue); await Meta.destroy({ where: { metaKey: 'activeOfferTradeId' } }); res.json({ diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index 8bfc1c44..ca583436 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -41,181 +41,191 @@ class Staging extends Model { } static generateOfferFile = async () => { - const stagingRecord = await Staging.findOne({ - where: { isTransfer: true }, - raw: true, - }); - - const makerProjectRecord = _.head(JSON.parse(stagingRecord.data)); + try { + const stagingRecord = await Staging.findOne({ + where: { isTransfer: true }, + raw: true, + }); - const myOrganization = await Organization.findOne({ - where: { isHome: true }, - raw: true, - }); + const makerProjectRecord = _.head(JSON.parse(stagingRecord.data)); - 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; - - const takerProjectRecord = await Project.findOne({ - where: { warehouseProjectId: makerProjectRecord.warehouseProjectId }, - include: Project.getAssociatedModels().map((association) => { - return { - model: association.model, - as: formatModelAssociationName(association), - }; - }), - }); + const myOrganization = await Organization.findOne({ + where: { isHome: true }, + raw: true, + }); - takerProjectRecord.projectStatus = 'Transitioned'; + 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; + + const takerProjectRecord = await Project.findOne({ + where: { warehouseProjectId: makerProjectRecord.warehouseProjectId }, + include: Project.getAssociatedModels().map((association) => { + return { + model: association.model, + as: formatModelAssociationName(association), + }; + }), + }); - const issuanceIds = takerProjectRecord.issuances.reduce((ids, issuance) => { - if (!ids.includes(issuance.id)) { - ids.push(issuance.id); - } - return ids; - }, []); + takerProjectRecord.projectStatus = 'Transitioned'; - let unitTakerRecords = await Unit.findAll({ - where: { - issuanceId: { [Op.in]: issuanceIds }, - }, - raw: true, - }); - - // Makers get an unlatered copy of all the project units from the taker - const unitMakerRecords = _.cloneDeep(unitTakerRecords); + const issuanceIds = takerProjectRecord.issuances.reduce( + (ids, issuance) => { + if (!ids.includes(issuance.id)) { + ids.push(issuance.id); + } + return ids; + }, + [], + ); - unitTakerRecords = unitTakerRecords.map((record) => { - record.unitStatus = 'Exported'; - return record; - }); + let unitTakerRecords = await Unit.findAll({ + where: { + issuanceId: { [Op.in]: issuanceIds }, + }, + raw: true, + }); - const primaryProjectKeyMap = { - project: 'warehouseProjectId', - projectLocations: 'id', - labels: 'id', - issuances: 'id', - coBenefits: 'id', - relatedProjects: 'id', - estimations: 'id', - projectRatings: 'id', - }; + // Makers get an unlatered copy of all the project units from the taker + const unitMakerRecords = _.cloneDeep(unitTakerRecords); - const primaryUnitKeyMap = { - unit: 'warehouseUnitId', - labels: 'id', - label_units: 'id', - issuances: 'id', - }; + unitTakerRecords = unitTakerRecords.map((record) => { + record.unitStatus = 'Exported'; + return record; + }); - const takerProjectXslsSheets = createXlsFromSequelizeResults({ - rows: [takerProjectRecord], - model: Project, - toStructuredCsv: true, - }); + const primaryProjectKeyMap = { + project: 'warehouseProjectId', + projectLocations: 'id', + labels: 'id', + issuances: 'id', + coBenefits: 'id', + relatedProjects: 'id', + estimations: 'id', + projectRatings: 'id', + }; + + const primaryUnitKeyMap = { + unit: 'warehouseUnitId', + labels: 'id', + label_units: 'id', + issuances: 'id', + }; + + const takerProjectXslsSheets = createXlsFromSequelizeResults({ + rows: [takerProjectRecord], + model: Project, + toStructuredCsv: true, + }); - const makerProjectXslsSheets = createXlsFromSequelizeResults({ - rows: [makerProjectRecord], - model: Project, - toStructuredCsv: true, - }); + const makerProjectXslsSheets = createXlsFromSequelizeResults({ + rows: [makerProjectRecord], + model: Project, + toStructuredCsv: true, + }); - const takerUnitXslsSheets = createXlsFromSequelizeResults({ - rows: unitTakerRecords, - model: Unit, - toStructuredCsv: true, - }); + const takerUnitXslsSheets = createXlsFromSequelizeResults({ + rows: unitTakerRecords, + model: Unit, + toStructuredCsv: true, + }); - const makerUnitXslsSheets = createXlsFromSequelizeResults({ - rows: unitMakerRecords, - model: Unit, - toStructuredCsv: true, - }); + const makerUnitXslsSheets = createXlsFromSequelizeResults({ + rows: unitMakerRecords, + model: Unit, + toStructuredCsv: true, + }); - const makerProjectInclusions = await transformFullXslsToChangeList( - makerProjectXslsSheets, - 'insert', - primaryProjectKeyMap, - ); + const makerProjectInclusions = await transformFullXslsToChangeList( + makerProjectXslsSheets, + 'insert', + primaryProjectKeyMap, + ); - const takerProjectInclusions = await transformFullXslsToChangeList( - takerProjectXslsSheets, - 'insert', - primaryProjectKeyMap, - ); + const takerProjectInclusions = await transformFullXslsToChangeList( + takerProjectXslsSheets, + 'insert', + primaryProjectKeyMap, + ); - const takerUnitInclusions = await transformFullXslsToChangeList( - takerUnitXslsSheets, - 'insert', - primaryUnitKeyMap, - ); + const takerUnitInclusions = await transformFullXslsToChangeList( + takerUnitXslsSheets, + 'insert', + primaryUnitKeyMap, + ); - const makerUnitInclusions = await transformFullXslsToChangeList( - makerUnitXslsSheets, - 'insert', - primaryUnitKeyMap, - ); + const makerUnitInclusions = await transformFullXslsToChangeList( + makerUnitXslsSheets, + 'insert', + primaryUnitKeyMap, + ); - /* Object.keys(maker.inclusions).forEach((table) => { + /* Object.keys(maker.inclusions).forEach((table) => { maker.inclusions[table] = maker.inclusions[table] .filter((inclusion) => inclusion.action !== 'delete') .map((inclusion) => ({ key: inclusion.key, value: inclusion.value })); });*/ - taker.inclusions.push( - ...takerProjectInclusions.project - .filter((inclusion) => inclusion.action !== 'delete') - .map((inclusion) => ({ - key: inclusion.key, - value: inclusion.value, - })), - ); - - if (takerUnitInclusions?.unit) { taker.inclusions.push( - ...takerUnitInclusions.unit + ...takerProjectInclusions.project .filter((inclusion) => inclusion.action !== 'delete') .map((inclusion) => ({ key: inclusion.key, value: inclusion.value, })), ); - } - maker.inclusions.push( - ...makerProjectInclusions.project - .filter((inclusion) => inclusion.action !== 'delete') - .map((inclusion) => ({ - key: inclusion.key, - value: inclusion.value, - })), - ); + if (takerUnitInclusions?.unit) { + taker.inclusions.push( + ...takerUnitInclusions.unit + .filter((inclusion) => inclusion.action !== 'delete') + .map((inclusion) => ({ + key: inclusion.key, + value: inclusion.value, + })), + ); + } - if (makerUnitInclusions?.unit) { maker.inclusions.push( - ...makerUnitInclusions.unit + ...makerProjectInclusions.project .filter((inclusion) => inclusion.action !== 'delete') .map((inclusion) => ({ key: inclusion.key, value: inclusion.value, })), ); - } - const offerInfo = generateOffer(maker, taker); - const offer = makeOffer(offerInfo); + if (makerUnitInclusions?.unit) { + maker.inclusions.push( + ...makerUnitInclusions.unit + .filter((inclusion) => inclusion.action !== 'delete') + .map((inclusion) => ({ + key: inclusion.key, + value: inclusion.value, + })), + ); + } - await Meta.upsert({ - metaKey: 'activeOfferTradeId', - metaValue: offer.trade_id, - }); + const offerInfo = generateOffer(maker, taker); + const offerResponse = await makeOffer(offerInfo); + if (!offerResponse.success) { + throw new Error(offerResponse.error); + } - return offer; + await Meta.upsert({ + metaKey: 'activeOfferTradeId', + metaValue: offerResponse.offer.trade_id, + }); + + return offerResponse.offer; + } catch (error) { + throw new Error(error.message); + } }; // If the record was commited but the diff.original is null