Skip to content

Commit

Permalink
fix: invalid generation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Dec 7, 2023
1 parent c49bb88 commit c395bdf
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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 @@ -295,9 +303,7 @@ const syncOrganizationAudit = async (organization) => {
logger.info(' ');
logger.info(`Syncing Registry: ${_.get(organization, 'name')}`);
logger.info(
`${organization.name} is ${
syncRemaining + 1
} DataLayer generations away from being fully synced.`,
`${organization.name} is ${syncRemaining} DataLayer generations away from being fully synced.`,
);

if (!CONFIG.USE_SIMULATOR) {
Expand Down Expand Up @@ -357,31 +363,32 @@ 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('{}'))),
),
};
'author',
'',
),
};

console.log(modelKey);
if (Object.keys(ModelKeys).includes(modelKey)) {
if (modelKey) {
const record = JSON.parse(decodeHex(diff.value));
const primaryKeyValue =
Expand Down Expand Up @@ -425,18 +432,18 @@ const syncOrganizationAudit = async (organization) => {
}
}
}

// 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 c395bdf

Please sign in to comment.