Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Chia-Network/climate-warehouse i…
Browse files Browse the repository at this point in the history
…nto develop
  • Loading branch information
MichaelTaylor3D committed Jul 18, 2022
2 parents f45f971 + dac4c8c commit 138d55d
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export const updateFromXLS = async (req, res) => {
message: 'Updates from xlsx added to staging',
});
} catch (error) {
logger.error(error);
res.status(400).json({
message: 'Batch Upload Failed.',
error: error.message,
Expand Down Expand Up @@ -368,6 +369,7 @@ export const batchUpload = async (req, res) => {
'CSV processing complete, your records have been added to the staging table.',
});
} catch (error) {
logger.error('Batch Upload Failed.', error);
res.status(400).json({
message: 'Batch Upload Failed.',
error: error.message,
Expand Down
1 change: 1 addition & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const commit = async (req, res) => {
await Staging.pushToDataLayer(
_.get(req, 'query.table', null),
_.get(req, 'body.comment', ''),
_.get(req, 'body.author', ''),
_.get(req, 'body.ids', []),
);

Expand Down
3 changes: 3 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from '../utils/xls';

import { formatModelAssociationName } from '../utils/model-utils.js';
import { logger } from '../config/logger.cjs';

export const create = async (req, res) => {
try {
Expand Down Expand Up @@ -245,6 +246,7 @@ export const updateFromXLS = async (req, res) => {
message: 'Updates from xlsx added to staging',
});
} catch (error) {
logger.error('Batch Upload Failed.', error);
res.status(400).json({
message: 'Batch Upload Failed.',
error: error.message,
Expand Down Expand Up @@ -456,6 +458,7 @@ export const batchUpload = async (req, res) => {
'CSV processing complete, your records have been added to the staging table.',
});
} catch (error) {
logger.error('Batch Upload Failed.', error);
res.status(400).json({
message: 'Batch Upload Failed.',
error: error.message,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

export default {
async up(queryInterface, Sequelize) {
await Promise.all(
['audit'].map((table) => {
queryInterface.addColumn(table, 'author', {
type: Sequelize.STRING,
allowNull: true,
});
}),
);
},

async down(queryInterface) {
await Promise.all(
['audit'].map((table) => {
queryInterface.removeColumn(table, 'author');
}),
);
},
};
5 changes: 5 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import AddCommentColumnToDataModelTables from './20220428144558-add-comment-colu
import AddSerialNumberFields from './20220504180739-add-serial-number-fields';
import AddDescriptionFieldToProjects from './20220509125335-add-description-field-to-projects';
import RepopulateVirtualTables from './20220515223227-re-populate-virtual-tables';
import AddAuthorColumnToAuditTable from './20220708210357-adding-author-column-to-audit-table';

export const migrations = [
{
Expand Down Expand Up @@ -124,4 +125,8 @@ export const migrations = [
migration: RepopulateVirtualTables,
name: '20220515223227-re-populate-virtual-tables',
},
{
migration: AddAuthorColumnToAuditTable,
name: '20220708210357-adding-author-column-to-audit-table',
}
];
3 changes: 3 additions & 0 deletions src/models/audit/audit.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports = {
required: true,
allowNull: false,
},
author: {
type: Sequelize.STRING,
},
comment: {
type: Sequelize.STRING,
},
Expand Down
13 changes: 12 additions & 1 deletion src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class Project extends Model {
};
}

static async generateChangeListFromStagedData(stagedData, comment) {
static async generateChangeListFromStagedData(stagedData, comment, author) {
const [insertRecords, updateRecords, deleteChangeList] =
Staging.seperateStagingDataIntoActionGroups(stagedData, 'Projects');

Expand Down Expand Up @@ -364,6 +364,16 @@ class Project extends Model {
isUpdateComment,
);

const currentAuthor = currentDataLayer.filter(
(kv) => kv.key === 'author',
);
const isUpdateAuthor = currentAuthor.length > 0;
const authorChangeList = keyValueToChangeList(
'author',
`{"author": "${author}"}`,
isUpdateAuthor,
);

return {
projects: [
..._.get(insertChangeList, 'project', []),
Expand Down Expand Up @@ -406,6 +416,7 @@ class Project extends Model {
..._.get(deletedAssociationsChangeList, 'projectRatings', []),
],
comment: commentChangeList,
author: authorChangeList,
};
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Staging extends Model {
return [insertRecords, updateRecords, deleteChangeList];
};

static async pushToDataLayer(tableToPush, comment, ids = []) {
static async pushToDataLayer(tableToPush, comment, author, ids = []) {
let stagedRecords;

if (tableToPush) {
Expand Down Expand Up @@ -246,11 +246,13 @@ class Staging extends Model {
const unitsChangeList = await Unit.generateChangeListFromStagedData(
stagedRecords,
comment,
author,
);

const projectsChangeList = await Project.generateChangeListFromStagedData(
stagedRecords,
comment,
author,
);

const unifiedChangeList = {
Expand Down
13 changes: 12 additions & 1 deletion src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class Unit extends Model {
};
}

static async generateChangeListFromStagedData(stagedData, comment) {
static async generateChangeListFromStagedData(stagedData, comment, author) {
const [insertRecords, updateRecords, deleteChangeList] =
Staging.seperateStagingDataIntoActionGroups(stagedData, 'Units');

Expand Down Expand Up @@ -332,6 +332,16 @@ class Unit extends Model {
isUpdateComment,
);

const currentAuthor = currentDataLayer.filter(
(kv) => kv.key === 'author',
);
const isUpdateAuthor = currentAuthor.length > 0;
const authorChangeList = keyValueToChangeList(
'author',
`{"author": "${author}"}`,
isUpdateAuthor,
);

return {
units: [
..._.get(insertChangeList, 'unit', []),
Expand All @@ -354,6 +364,7 @@ class Unit extends Model {
..._.get(deletedAssociationsChangeList, 'label_units', []),
],
comment: commentChangeList,
author: authorChangeList,
};
}
}
Expand Down
1 change: 0 additions & 1 deletion src/models/units/units.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = {
// Please insert reference to labels table.
unitOwner: {
type: Sequelize.STRING,
required: true,
},
countryJurisdictionOfOwner: {
type: Sequelize.STRING,
Expand Down
11 changes: 11 additions & 0 deletions src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ const syncOrganizationAudit = async (organization) => {
diff.key === '636f6d6d656e74' || diff.key === '0x636f6d6d656e74',
);

// 0x617574686F72 is hex for 'author'
const author = kvDiff.filter(
(diff) =>
diff.key === '617574686F72' || diff.key === '0x617574686F72',
);

await Promise.all(
kvDiff.map(async (diff) => {
const key = decodeHex(diff.key);
Expand All @@ -131,6 +137,11 @@ const syncOrganizationAudit = async (organization) => {
'comment',
'',
),
author: _.get(
JSON.parse(decodeHex(_.get(author, '[0].value', '7b7d'))),
'author',
'',
),
});
}
}),
Expand Down
1 change: 1 addition & 0 deletions src/validations/staging.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const stagingRetrySchema = Joi.object({
});

export const commitStagingSchema = Joi.object({
author: Joi.string().optional(),
comment: Joi.string().optional(),
ids: Joi.array().items(Joi.string()).optional(),
});
Expand Down
2 changes: 1 addition & 1 deletion src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const unitsBaseSchema = {
// issuanceId - derived upon unit creation
// orgUid - derived upon unit creation
projectLocationId: Joi.string().optional(),
unitOwner: Joi.string().required(),
unitOwner: Joi.string(),
countryJurisdictionOfOwner: Joi.string()
.custom(pickListValidation('countries', 'countryJurisdictionOfOwner'))
.required(),
Expand Down

0 comments on commit 138d55d

Please sign in to comment.