Skip to content

Commit

Permalink
Merge pull request #60 from Chia-Network/feat/add-cadt-proper-fizes
Browse files Browse the repository at this point in the history
feat: add volume fixes for author comment
  • Loading branch information
MichaelTaylor3D authored Dec 8, 2023
2 parents 9a933fa + 3fdf623 commit 3729ea9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 89 deletions.
30 changes: 28 additions & 2 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ const getMirrors = async (storeId) => {
}
};

const getValue = async (storeId, storeKey) => {
const url = `${CONFIG.DATALAYER_URL}/get_value`;
const { cert, key, timeout } = getBaseOptions();

try {
const response = await superagent
.post(url)
.key(key)
.cert(cert)
.timeout(timeout)
.send({ id: storeId, key: storeKey });

const data = response.body;

if (data.success) {
return data.value;
}

return false;
} catch (error) {
logger.error(error);
return false;
}
};

const addMirror = async (storeId, url, forceAddMirror = false) => {
await wallet.waitForAllTransactionsToConfirm();
const homeOrg = await Organization.getHomeOrg();
Expand Down Expand Up @@ -379,7 +404,7 @@ const getRoots = async (storeIds) => {
};

const clearPendingRoots = async (storeId) => {
const url = `${CONFIG.DATALAYER_URL}/clear_pending_roots`;
const url = `${CONFIG().DATALAYER_URL}/clear_pending_roots`;
const { cert, key, timeout } = getBaseOptions();

try {
Expand Down Expand Up @@ -412,7 +437,7 @@ const pushChangeListToDataLayer = async (storeId, changelist) => {
try {
await wallet.waitForAllTransactionsToConfirm();

const url = `${CONFIG.DATALAYER_URL}/batch_update`;
const url = `${CONFIG().DATALAYER_URL}/batch_update`;
const { cert, key, timeout } = getBaseOptions();

const response = await superagent
Expand Down Expand Up @@ -710,4 +735,5 @@ export {
verifyOffer,
takeOffer,
clearPendingRoots,
getValue,
};
9 changes: 9 additions & 0 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ const removeMirror = (storeId, coinId) => {
return dataLayer.removeMirror(storeId, coinId);
};

const getValue = async (storeId, key) => {
if (CONFIG().CADT.USE_SIMULATOR) {
return '7b22636f6d6d656e74223a2022227d';
} else {
return dataLayer.getValue(storeId, key);
}
};

export default {
addMirror,
createDataLayerStore,
Expand All @@ -188,4 +196,5 @@ export default {
syncDataLayer,
upsertDataLayer,
removeMirror,
getValue,
};
24 changes: 17 additions & 7 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
formatModelAssociationName,
getDeletedItems,
} from '../../utils/model-utils.js';
import { keyValueToChangeList } from '../../utils/datalayer-utils';
import { encodeHex, keyValueToChangeList } from '../../utils/datalayer-utils';
import dataLayer from '../../datalayer';

class Project extends Model {
Expand Down Expand Up @@ -384,19 +384,29 @@ class Project extends Model {
);

const { registryId } = await Organization.getHomeOrg();
const currentDataLayer = await dataLayer.getCurrentStoreData(registryId);
const currentComment = currentDataLayer.filter(
(kv) => kv.key === 'comment',

const commentValueInStore = await dataLayer.getValue(
registryId,
encodeHex('comment'),
);
const isUpdateComment = currentComment.length > 0;

const isUpdateComment =
!_.isNil(commentValueInStore) && commentValueInStore !== false;

const commentChangeList = keyValueToChangeList(
'comment',
`{"comment": "${comment}"}`,
isUpdateComment,
);

const currentAuthor = currentDataLayer.filter((kv) => kv.key === 'author');
const isUpdateAuthor = currentAuthor.length > 0;
const authorValueInStore = await dataLayer.getValue(
registryId,
encodeHex('author'),
);

const isUpdateAuthor =
!_.isNil(authorValueInStore) && authorValueInStore !== false;

const authorChangeList = keyValueToChangeList(
'author',
`{"author": "${author}"}`,
Expand Down
24 changes: 17 additions & 7 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
createXlsFromSequelizeResults,
transformFullXslsToChangeList,
} from '../../utils/xls';
import { keyValueToChangeList } from '../../utils/datalayer-utils';
import { encodeHex, keyValueToChangeList } from '../../utils/datalayer-utils';
import { unitsUpdateSchema } from '../../validations/index.js';
import { getDeletedItems } from '../../utils/model-utils.js';
import dataLayer from '../../datalayer';
Expand Down Expand Up @@ -436,19 +436,29 @@ class Unit extends Model {
);

const { registryId } = await Organization.getHomeOrg();
const currentDataLayer = await dataLayer.getCurrentStoreData(registryId);
const currentComment = currentDataLayer.filter(
(kv) => kv.key === 'comment',

const commentValueInStore = await dataLayer.getValue(
registryId,
encodeHex('comment'),
);
const isUpdateComment = currentComment.length > 0;

const isUpdateComment =
!_.isNil(commentValueInStore) && commentValueInStore !== false;

const commentChangeList = keyValueToChangeList(
'comment',
`{"comment": "${comment}"}`,
isUpdateComment,
);

const currentAuthor = currentDataLayer.filter((kv) => kv.key === 'author');
const isUpdateAuthor = currentAuthor.length > 0;
const authorValueInStore = await dataLayer.getValue(
registryId,
encodeHex('author'),
);

const isUpdateAuthor =
!_.isNil(authorValueInStore) && authorValueInStore !== false;

const authorChangeList = keyValueToChangeList(
'author',
`{"author": "${author}"}`,
Expand Down
151 changes: 78 additions & 73 deletions src/tasks/sync-registries.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ async function createTransaction(callback, afterCommitCallbacks) {
}
}

const tryParseJSON = (jsonString, defaultValue) => {
try {
return JSON.parse(jsonString);
} catch (error) {
return defaultValue;
}
};

const syncOrganizationAudit = async (organization) => {
try {
let afterCommitCallbacks = [];
Expand Down Expand Up @@ -354,86 +362,83 @@ const syncOrganizationAudit = async (organization) => {
const key = decodeHex(diff.key);
const modelKey = key.split('|')[0];

if (!['comment', 'author'].includes(key)) {
const auditData = {
orgUid: organization.orgUid,
registryId: organization.registryId,
rootHash: root2.root_hash,
type: diff.type,
table: modelKey,
change: decodeHex(diff.value),
onchainConfirmationTimeStamp: root2.timestamp,
comment: _.get(
JSON.parse(
decodeHex(_.get(comment, '[0].value', encodeHex('{}'))),
),
'comment',
'',
const auditData = {
orgUid: organization.orgUid,
registryId: organization.registryId,
rootHash: root2.root_hash,
type: diff.type,
table: modelKey,
change: decodeHex(diff.value),
onchainConfirmationTimeStamp: root2.timestamp,
comment: _.get(
tryParseJSON(
decodeHex(_.get(comment, '[0].value', encodeHex('{}'))),
),
author: _.get(
JSON.parse(
decodeHex(_.get(author, '[0].value', encodeHex('{}'))),
),
'author',
'',
'comment',
'',
),
author: _.get(
tryParseJSON(
decodeHex(_.get(author, '[0].value', encodeHex('{}'))),
),
};

if (modelKey) {
const record = JSON.parse(decodeHex(diff.value));
const primaryKeyValue =
record[ModelKeys[modelKey].primaryKeyAttributes[0]];

if (diff.type === 'INSERT') {
logger.info(`UPSERTING: ${modelKey} - ${primaryKeyValue}`);
await ModelKeys[modelKey].upsert(record, {
transaction,
mirrorTransaction,
});
} else if (diff.type === 'DELETE') {
logger.info(`DELETING: ${modelKey} - ${primaryKeyValue}`);
await ModelKeys[modelKey].destroy({
where: {
[ModelKeys[modelKey].primaryKeyAttributes[0]]:
primaryKeyValue,
},
transaction,
mirrorTransaction,
});
}
'author',
'',
),
};

if (modelKey && Object.keys(ModelKeys).includes(modelKey)) {
const record = JSON.parse(decodeHex(diff.value));
const primaryKeyValue =
record[ModelKeys[modelKey].primaryKeyAttributes[0]];

if (diff.type === 'INSERT') {
logger.info(`UPSERTING: ${modelKey} - ${primaryKeyValue}`);
await ModelKeys[modelKey].upsert(record, {
transaction,
mirrorTransaction,
});
} else if (diff.type === 'DELETE') {
logger.info(`DELETING: ${modelKey} - ${primaryKeyValue}`);
await ModelKeys[modelKey].destroy({
where: {
[ModelKeys[modelKey].primaryKeyAttributes[0]]: primaryKeyValue,
},
transaction,
mirrorTransaction,
});
}

if (organization.orgUid === homeOrg?.orgUid) {
const stagingUuid = [
'unit',
'project',
'units',
'projects',
].includes(modelKey)
? primaryKeyValue
: undefined;

if (stagingUuid) {
afterCommitCallbacks.push(async () => {
logger.info(`DELETING STAGING: ${stagingUuid}`);
await Staging.destroy({
where: { uuid: stagingUuid },
});
if (organization.orgUid === homeOrg?.orgUid) {
const stagingUuid = [
'unit',
'project',
'units',
'projects',
].includes(modelKey)
? primaryKeyValue
: undefined;

if (stagingUuid) {
afterCommitCallbacks.push(async () => {
logger.info(`DELETING STAGING: ${stagingUuid}`);
await Staging.destroy({
where: { uuid: stagingUuid },
});
}
});
}
}

// Create the Audit record
await Audit.create(auditData, { transaction, mirrorTransaction });
await Organization.update(
{ registryHash: root2.root_hash },
{
where: { orgUid: organization.orgUid },
transaction,
mirrorTransaction,
},
);
}

// Create the Audit record
await Audit.create(auditData, { transaction, mirrorTransaction });
await Organization.update(
{ registryHash: root2.root_hash },
{
where: { orgUid: organization.orgUid },
transaction,
mirrorTransaction,
},
);
}
};

Expand Down

0 comments on commit 3729ea9

Please sign in to comment.