Skip to content

Commit

Permalink
fix: offer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Sep 3, 2022
1 parent d703885 commit 35fc124
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/controllers/offer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
278 changes: 144 additions & 134 deletions src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35fc124

Please sign in to comment.