Skip to content

Commit

Permalink
feat: add transfer project endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Aug 29, 2022
1 parent bddd065 commit b23b6a1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
assertNoPendingCommits,
assertRecordExistance,
assertIfReadOnlyMode,
assertStagingTableIsEmpty,
} from '../utils/data-assertions';

import { createProjectRecordsFromCsv } from '../utils/csv-utils';
Expand Down Expand Up @@ -248,7 +249,19 @@ export const updateFromXLS = async (req, res) => {
}
};

export const update = async (req, res) => {
export const transfer = async (req, res) => {
try {
await assertStagingTableIsEmpty();
return update(req, res, true);
} catch (err) {
res.status(400).json({
message: err.message,
});
logger.error('Error adding update to stage', err);
}
};

const update = async (req, res, isTransfer = false) => {
try {
await assertIfReadOnlyMode();
await assertHomeOrgExists();
Expand All @@ -258,12 +271,19 @@ export const update = async (req, res) => {
req.body.warehouseProjectId,
);

await assertOrgIsHomeOrg(originalRecord.orgUid);
// transfers can operate on project records that belong to someone else
// none transfers operations must belong to your orgUid
if (!isTransfer) {
await assertOrgIsHomeOrg(originalRecord.orgUid);
}

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

const { orgUid } = await Organization.getHomeOrg();
newRecord.orgUid = orgUid;
// Why do we need this? the orgUid should already be in the record
//const { orgUid } = await Organization.getHomeOrg();
//newRecord.orgUid = orgUid;

const { orgUid } = newRecord;

const childRecordsKeys = [
'projectLocations',
Expand Down Expand Up @@ -316,8 +336,16 @@ export const update = async (req, res) => {
action: 'UPDATE',
table: Project.stagingTableName,
data: JSON.stringify(stagedRecord),
// If this is a transfer staging, push it directly into commited status
// Since we are downloading a offer file and shouldnt be allowed to operate
// until that file is either accepted or cancelled
commited: isTransfer,
};

if (isTransfer) {
stagedData.isTransfer = true;
}

await Staging.upsert(stagedData);

res.json({
Expand All @@ -331,6 +359,8 @@ export const update = async (req, res) => {
}
};

export { update };

export const destroy = async (req, res) => {
try {
await assertIfReadOnlyMode();
Expand Down
23 changes: 23 additions & 0 deletions src/database/migrations/20220825124702-add-isTransfer-column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

export default {
async up(queryInterface, Sequelize) {
await Promise.all(
['staging'].map((table) => {
queryInterface.addColumn(table, 'isTransfer', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
});
}),
);
},

async down(queryInterface) {
await Promise.all(
['staging'].map((table) => {
queryInterface.removeColumn(table, 'isTransfer');
}),
);
},
};
14 changes: 14 additions & 0 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ const dataLayerWasUpdated = async () => {
}),
);

// When a transfer is made, the climate warehouse is locked from making updates
// while waiting for the transfer to either be completed or rejected.
// This means that we know the transfer completed when the root hash changed
// and we can remove it from the pending staging table.
const homeOrg = organizations.find((org) => org.isHome === 1);
if (homeOrg) {
const updatedHomeInfo = updateStoreInfo.find(
(info) => info.storeId === homeOrg.registryId,
);
if (updatedHomeInfo.rootHash !== homeOrg.registryHash) {
await Staging.destroy({ where: { isTransfer: true } });
}
}

return updateStoreInfo;
};

Expand Down
5 changes: 5 additions & 0 deletions src/models/staging/staging.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ module.exports = {
allowNull: false,
defaultValue: false,
},
isTransfer: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
Expand Down
6 changes: 6 additions & 0 deletions src/routes/v1/resources/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ ProjectRouter.put(
ProjectController.update,
);

ProjectRouter.put(
'/transfer',
validator.body(projectsUpdateSchema),
ProjectController.transfer,
);

ProjectRouter.put('/xlsx', ProjectController.updateFromXLS);

ProjectRouter.delete(
Expand Down

0 comments on commit b23b6a1

Please sign in to comment.