Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 fix organization imports v2 #1242

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
assertIfReadOnlyMode,
assertCanDeleteOrg,
assertNoPendingCommits,
assertOrgDoesNotExist,
} from '../utils/data-assertions';

import { getDataModelVersion } from '../utils/helpers';
Expand Down Expand Up @@ -181,12 +182,13 @@ export const resetHomeOrg = async (req, res) => {
]);

res.json({
message: 'Your home organization was reset, please create a new one.',
message:
'Your home organization was deleted from this instance. (note that it still exists in datalayer)',
success: true,
});
} catch (error) {
res.status(400).json({
message: 'Error resetting your organization',
message: 'Error deleting your organization',
error: error.message,
success: false,
});
Expand All @@ -199,14 +201,15 @@ export const importOrg = async (req, res) => {
await assertWalletIsSynced();

const { orgUid } = req.body;
await assertOrgDoesNotExist(orgUid);

res.json({
await Organization.importOrganization(orgUid);

res.status(200).json({
message:
'Importing and subscribing organization this can take a few mins.',
'Successfully imported organization. CADT will begin syncing data from datalayer shortly',
success: true,
});

return Organization.importOrganization(orgUid);
} catch (error) {
console.trace(error);
res.status(400).json({
Expand All @@ -223,17 +226,19 @@ export const importHomeOrg = async (req, res) => {
await assertWalletIsSynced();

const { orgUid } = req.body;
await assertOrgDoesNotExist(orgUid);

await Organization.importHomeOrg(orgUid);
// asserts home org stores are owned
await Organization.importOrganization(orgUid, true);

res.json({
message: 'Importing home organization.',
res.status(200).json({
message:
'Successfully imported home organization. CADT will begin syncing data from datalayer shortly',
success: true,
});
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Error importing organization',
message: 'Error importing home organization',
error: error.message,
success: false,
});
Expand All @@ -244,7 +249,6 @@ export const subscribeToOrganization = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertWalletIsSynced();
await assertHomeOrgExists();

await Organization.subscribeToOrganization(req.body.orgUid);

Expand Down Expand Up @@ -284,12 +288,12 @@ export const deleteImportedOrg = async (req, res) => {

return res.json({
message:
'UnSubscribed to organization, you will no longer receive updates.',
'Removed all organization records. cadt will not sync the organizations data from datalayer',
success: true,
});
} catch (error) {
res.status(400).json({
message: 'Error unsubscribing to organization',
message: 'Error deleting organization',
error: error.message,
success: false,
});
Expand Down
48 changes: 19 additions & 29 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,21 @@ const getStoreData = async (storeId, rootHash) => {
const data = response.body;

if (data.success) {
if (!_.isEmpty(data.keys_values)) {
logger.info(`Downloaded Data, root hash: ${rootHash || 'latest'}`);
if (_.isEmpty(data.keys_values)) {
logger.warn(
`datalayer get_keys_values returned no data for store ${storeId} at root hash: ${rootHash || 'latest'}`,
);
}
return data;
} else {
throw new Error(JSON.stringify(data));
}

logger.error(
`FAILED GETTING STORE DATA FOR ${storeId}: ${JSON.stringify(data)}`,
);
} catch (error) {
logger.info(
`Unable to find store data for ${storeId} at root ${
logger.error(
`failed to get keys and values from datalayer for store ${storeId} at root ${
rootHash || 'latest'
}`,
}. Error: ${error.message}`,
);
logger.error(error.message);
return false;
}
}
Expand All @@ -396,14 +395,19 @@ const getRoot = async (storeId, ignoreEmptyStore = false) => {
.send({ id: storeId });

const { confirmed, hash } = response.body;
logger.debug(
`the current root data for store ${storeId} is ${JSON.stringify(response.body)}`,
);

if (confirmed && (!ignoreEmptyStore || !hash.includes('0x00000000000'))) {
if (confirmed && (ignoreEmptyStore || !hash.includes('0x00000000000'))) {
return response.body;
}

return false;
} catch (error) {
logger.error(error.message);
logger.error(
`failed to get root for store ${storeId}. error: ${error.message}`,
);
return false;
}
};
Expand Down Expand Up @@ -522,34 +526,20 @@ const createDataLayerStore = async () => {
}
};

const subscribeToStoreOnDataLayer = async (
storeId,
restoreHomeOrgOverride = false,
) => {
const subscribeToStoreOnDataLayer = async (storeId) => {
if (!storeId) {
logger.info(`No storeId found to subscribe to: ${storeId}`);
return false;
}

const homeOrg = await Organization.getHomeOrg();

if (
!restoreHomeOrgOverride &&
homeOrg &&
[(homeOrg.orgUid, homeOrg.registryId)].includes(storeId)
) {
logger.info(`Cant subscribe to self: ${storeId}`);
return { success: true };
}

const { storeIds: subscriptions, success } = await getSubscriptions();
if (!success) {
return false;
}

if (subscriptions.includes(storeId)) {
logger.info(`Already subscribed to: ${storeId}`);
return { success: true };
return true;
}

const url = `${CONFIG.DATALAYER_URL}/subscribe`;
Expand Down Expand Up @@ -577,7 +567,7 @@ const subscribeToStoreOnDataLayer = async (

await addMirror(storeId, mirrorUrl, true);

return data;
return true;
}

return false;
Expand Down
33 changes: 24 additions & 9 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const subscribeToStoreOnDataLayer = async (storeId) => {
}
};

/**
* gets and decodes data from a subscribed store.
* will subscribe to any store id for which there is no subscription.
* @param storeId to retrieve data from
* @returns {Promise<any>}
*/
const getSubscribedStoreData = async (storeId) => {
const { storeIds: subscriptions, success } =
await dataLayer.getSubscriptions();
Expand All @@ -36,23 +42,26 @@ const getSubscribedStoreData = async (storeId) => {

if (!alreadySubscribed) {
logger.info(`No Subscription Found for ${storeId}, Subscribing...`);
const response = await subscribeToStoreOnDataLayer(storeId);
const response = await dataLayer.subscribeToStoreOnDataLayer(storeId);

if (!response || !response.success) {
if (!response) {
throw new Error(`Failed to subscribe to ${storeId}`);
}
}

logger.info(`Subscription Found for ${storeId}.`);
logger.debug(`Subscription Found for ${storeId}.`);

if (!USE_SIMULATOR) {
logger.info(`Getting confirmation for ${storeId}.`);
logger.debug(
`syncService getSubscribedData() checking that data is available for ${storeId}.`,
);
const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId, true);
logger.info(`Store found in DataLayer: ${storeId}.`);
if (!storeExistAndIsConfirmed) {
throw new Error(`Store not found in DataLayer: ${storeId}.`);
} else {
logger.debug(`Store is confirmed, proceeding to get data ${storeId}`);
logger.debug(
`store data is confirmed available, proceeding to get data ${storeId}`,
);
}
}

Expand All @@ -64,7 +73,9 @@ const getSubscribedStoreData = async (storeId) => {
}

if (_.isEmpty(encodedData?.keys_values)) {
throw new Error(`No data found for store ${storeId}`);
throw new Error(
`getSubscribedStoreData() found no data for store ${storeId}`,
);
}

const decodedData = decodeDataLayerResponse(encodedData);
Expand Down Expand Up @@ -147,9 +158,13 @@ const getCurrentStoreData = async (storeId) => {

const encodedData = await dataLayer.getStoreData(storeId);
if (encodedData) {
return decodeDataLayerResponse(encodedData);
const decodedData = decodeDataLayerResponse(encodedData);
return decodedData.reduce((obj, current) => {
obj[current.key] = current.value;
return obj;
}, {});
} else {
return [];
return undefined;
}
};

Expand Down
15 changes: 14 additions & 1 deletion src/models/file-store/file-store.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@ class FileStore extends Model {
}

await datalayer.subscribeToStoreOnDataLayer(organization.fileStoreId);
Organization.update({ fileStoreSubscribed: true });

/* todo: this is code is now valid but it wasnt previously resulting in the records not updating and the filestore always
being marked as not subscribed. at the moment, not sure what the impact of marking them as subscribed would
be so leaving this commented out to revisit at a later date (today is 12/9/24)
await Organization.update(
{ fileStoreSubscribed: true },
{
where: {
orgUid,
},
},
);

*/
}

static async unsubscribeFromFileStore(orgUid) {
Expand Down
Loading
Loading