Skip to content

Commit

Permalink
fix: unit and project update logic when removing child tables
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Mar 13, 2022
1 parent 808c069 commit 999f5ed
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 31 deletions.
9 changes: 2 additions & 7 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ export const update = async (req, res) => {
await assertOrgIsHomeOrg(originalRecord.orgUid);

const newRecord = _.cloneDeep(req.body);

const { orgUid } = await Organization.getHomeOrg();
newRecord.orgUid = orgUid;

const childRecordsKeys = [
'projectLocations',
Expand Down Expand Up @@ -299,13 +301,6 @@ export const update = async (req, res) => {
// merge the new record into the old record
let stagedRecord = Array.isArray(newRecord) ? newRecord : [newRecord];

stagedRecord = stagedRecord.map((record) => {
return Object.keys(record).reduce((syncedRecord, key) => {
syncedRecord[key] = record[key];
return syncedRecord;
}, originalRecord);
});

const stagedData = {
uuid: req.body.warehouseProjectId,
action: 'UPDATE',
Expand Down
16 changes: 6 additions & 10 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,13 @@ export const update = async (req, res) => {
}

if (updatedRecord.issuance) {
if (!updatedRecord.issuance.id) {
if (updatedRecord.issuance.id) {
// if we are reusing a record, make sure it exists
await assertRecordExistance(Issuance, updatedRecord.issuance.id);
updatedRecord.issuanceId = updatedRecord.issuance.id;
delete updatedRecord.issuance;
} else {
updatedRecord.issuance.id = uuidv4();
}

if (!updatedRecord.issuance.orgUid) {
updatedRecord.issuance.orgUid = orgUid;
}
}
Expand All @@ -312,12 +314,6 @@ export const update = async (req, res) => {
let stagedRecord = Array.isArray(updatedRecord)
? updatedRecord
: [updatedRecord];
stagedRecord = stagedRecord.map((record) => {
return Object.keys(record).reduce((syncedRecord, key) => {
syncedRecord[key] = record[key];
return syncedRecord;
}, originalRecord);
});

const stagedData = {
uuid: req.body.warehouseUnitId,
Expand Down
2 changes: 1 addition & 1 deletion src/validations/co-benefits.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const cobenefitSchema = Joi.object({
// warehouseProjectId - derived upon creation
id: Joi.string().optional(),
cobenefit: Joi.string().required(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
2 changes: 1 addition & 1 deletion src/validations/estimations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const estimationSchema = Joi.object({
.min(Joi.ref('creditingPeriodStart'))
.required(),
unitCount: Joi.number().integer().required(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
2 changes: 1 addition & 1 deletion src/validations/issuances.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const issuanceSchema = Joi.object({
verificationApproach: Joi.string().required(),
verificationBody: Joi.string().required(),
verificationReportDate: Joi.date().required(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
2 changes: 1 addition & 1 deletion src/validations/labelUnit.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const labelUnitSchema = Joi.object({
orgUid: Joi.string(),
warehouseUnitId: Joi.string(),
labelId: Joi.string(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
2 changes: 1 addition & 1 deletion src/validations/labels.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const labelSchema = Joi.object({
.min(Joi.ref('validityPeriodStartDate'))
.required(),
unitQuantity: Joi.number().integer().required(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
labelLink: Joi.string().required(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
Expand Down
2 changes: 1 addition & 1 deletion src/validations/locations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const locationSchema = Joi.object({
.required(),
inCountryRegion: Joi.string().optional(),
geographicIdentifier: Joi.string().required(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
1 change: 1 addition & 0 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const baseSchema = {
estimations: Joi.array().items(estimationSchema).min(1).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
};

export const projectsGetQuerySchema = Joi.object()
Expand Down
2 changes: 1 addition & 1 deletion src/validations/ratings.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ratingSchema = Joi.object({
ratingRangeHighest: Joi.string().required(),
rating: Joi.string().required(),
ratingLink: Joi.string().required(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
2 changes: 1 addition & 1 deletion src/validations/relatedProjects.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const relatedProjectSchema = Joi.object({
relatedProjectId: Joi.string().required(),
relationshipType: Joi.string().optional(),
registry: Joi.string().optional(),
timeStaged: Joi.date().timestamp().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
});
1 change: 1 addition & 0 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const unitsBaseSchema = {
labels: Joi.array().items(labelSchema).optional(),
updatedAt: Joi.date().optional(),
createdAt: Joi.date().optional(),
timeStaged: Joi.date().timestamp().allow(null).optional(),
};

export const unitsPostSchema = Joi.object({
Expand Down
7 changes: 1 addition & 6 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,7 @@ describe('Unit Resource Integration Tests', function () {

// Make sure the newly created unit is in the mirrorDb
await testFixtures.checkUnitMirrorRecordExists(warehouseUnitId);
const updateUnitPayload = await testFixtures.updateUnit(
warehouseUnitId,
changeRecord,
);
await testFixtures.updateUnit(warehouseUnitId, changeRecord);

// Get the staging record we just created
const updatesStagingRecord =
Expand All @@ -457,7 +454,6 @@ describe('Unit Resource Integration Tests', function () {
expect(updatesStagingRecord.diff.original).to.deep.equal(newUnit);
const updateChangeRecord = _.head(updatesStagingRecord.diff.change);

testFixtures.objectContainsSubSet(updateChangeRecord, updateUnitPayload);
testFixtures.childTablesIncludeOrgUid(updateChangeRecord);
testFixtures.childTablesIncludePrimaryKey(updateChangeRecord);

Expand All @@ -475,7 +471,6 @@ describe('Unit Resource Integration Tests', function () {

const updatedUnit = await testFixtures.getUnit(warehouseUnitId);

testFixtures.objectContainsSubSet(updatedUnit, updateUnitPayload);
testFixtures.childTablesIncludeOrgUid(updatedUnit);
testFixtures.childTablesIncludePrimaryKey(updatedUnit);

Expand Down

0 comments on commit 999f5ed

Please sign in to comment.