Skip to content

Commit

Permalink
feat: add readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 28, 2022
1 parent 001c4e3 commit a65047f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .env.copy
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ DATALAYER_URL=https://localhost:8562
WALLET_URL=https://localhost:9256
USE_SIMULATOR=false
PICKLIST_URL=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/picklists.json
DEFAULT_ORGANIZATIONS=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/cw-organizations.json
DEFAULT_ORGANIZATIONS=https://climate-warehouse.s3.us-west-2.amazonaws.com/public/cw-organizations.json
READ_ONLY=false
4 changes: 4 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
assertHomeOrgExists,
assertWalletIsSynced,
assertDataLayerAvailable,
assertIfReadOnlyMode,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
Expand All @@ -12,6 +13,7 @@ export const findAll = async (req, res) => {

export const create = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsSynced();

Expand Down Expand Up @@ -40,6 +42,7 @@ export const create = async (req, res) => {
// eslint-disable-next-line
export const importOrg = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsSynced();

Expand All @@ -62,6 +65,7 @@ export const importOrg = async (req, res) => {

export const subscribeToOrganization = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
assertNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
assertIfReadOnlyMode,
} from '../utils/data-assertions';

import { createProjectRecordsFromCsv } from '../utils/csv-utils';
Expand All @@ -32,6 +33,7 @@ import {

export const create = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -185,6 +187,7 @@ export const findOne = async (req, res) => {

export const updateFromXLS = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -215,6 +218,7 @@ export const updateFromXLS = async (req, res) => {

export const update = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -304,6 +308,7 @@ export const update = async (req, res) => {

export const destroy = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -335,6 +340,7 @@ export const destroy = async (req, res) => {

export const batchUpload = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
assertNoPendingCommits,
assertWalletIsSynced,
assertDataLayerAvailable,
assertIfReadOnlyMode,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
Expand Down Expand Up @@ -41,6 +42,7 @@ export const findAll = async (req, res) => {

export const commit = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertHomeOrgExists();
await assertDataLayerAvailable();
await assertWalletIsSynced();
Expand All @@ -58,6 +60,7 @@ export const commit = async (req, res) => {

export const destroy = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertHomeOrgExists();
await assertStagingRecordExists(req.body.uuid);
await Staging.destroy({
Expand All @@ -78,6 +81,7 @@ export const destroy = async (req, res) => {

export const clean = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertHomeOrgExists();
await Staging.destroy({
where: {},
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
assertNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
assertIfReadOnlyMode,
} from '../utils/data-assertions';

import { createUnitRecordsFromCsv } from '../utils/csv-utils';
Expand All @@ -35,6 +36,7 @@ import xlsx from 'node-xlsx';

export const create = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertNoPendingCommits();
await assertHomeOrgExists();
Expand Down Expand Up @@ -222,6 +224,7 @@ export const findOne = async (req, res) => {

export const updateFromXLS = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -249,6 +252,7 @@ export const updateFromXLS = async (req, res) => {

export const update = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -335,6 +339,7 @@ export const update = async (req, res) => {

export const destroy = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -365,6 +370,7 @@ export const destroy = async (req, res) => {

export const split = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down Expand Up @@ -442,6 +448,7 @@ export const split = async (req, res) => {

export const batchUpload = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assertNoPendingCommits();
Expand Down
6 changes: 6 additions & 0 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export const assertDataLayerAvailable = async () => {
}
};

export const assertIfReadOnlyMode = async () => {
if (process.env.READ_ONLY === 'true') {
throw new Error('You can not use this API in read-only mode');
}
};

export const assertNoPendingCommits = async () => {
if (process.env.USE_SIMULATOR === 'true') {
const pendingCommits = await Staging.findAll({
Expand Down

0 comments on commit a65047f

Please sign in to comment.