diff --git a/package.json b/package.json index 68366283..681cb503 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cadt", - "version": "1.6.6", + "version": "1.6.7", "_comment": "DONT CHANGE MAJOR UNLESS DATAMODEL CHANGES: The major version corresponds to the datamodel version your using, so 2.0.0 means it'll use datamodel v2", "private": true, "bin": "build/server.js", diff --git a/src/models/file-store/file-store.model.js b/src/models/file-store/file-store.model.js index 0f3c2beb..353cda7d 100644 --- a/src/models/file-store/file-store.model.js +++ b/src/models/file-store/file-store.model.js @@ -47,9 +47,14 @@ class FileStore extends Model { static async addFileToFileStore(SHA256, fileName, base64File) { const myOrganization = await Organization.getHomeOrg(); + + if (myOrganization) { + throw new Error('No homeorg detected'); + } + let fileStoreId = myOrganization.fileStoreId; - if (!fileStoreId) { + if (myOrganization && !fileStoreId) { datalayer.createDataLayerStore().then((fileStoreId) => { datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId }); Organization.update( @@ -87,9 +92,9 @@ class FileStore extends Model { static async getFileStoreList() { const myOrganization = await Organization.getHomeOrg(); - let fileStoreId = myOrganization.fileStoreId; + let fileStoreId = myOrganization?.fileStoreId; - if (!fileStoreId) { + if (myOrganization && !fileStoreId) { datalayer.createDataLayerStore().then((fileStoreId) => { datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId }); Organization.update( @@ -100,27 +105,29 @@ class FileStore extends Model { throw new Error('New File store being created, please try again later.'); } - new Promise((resolve, reject) => { - datalayer.getStoreData( - myOrganization.fileStoreId, - (data) => { - resolve(data); - }, - reject, - ); - }).then((fileStore) => { - // Just caching this so dont await it, we dont care when it finishes - return Promise.all( - Object.keys(fileStore).map((key) => { - FileStore.upsert({ - SHA256: fileStore[key].SHA256, - fileName: key, - data: fileStore[key].data, - orgUid: myOrganization.orgUid, - }); - }), - ); - }); + if (fileStoreId) { + new Promise((resolve, reject) => { + datalayer.getStoreData( + myOrganization.fileStoreId, + (data) => { + resolve(data); + }, + reject, + ); + }).then((fileStore) => { + // Just caching this so dont await it, we dont care when it finishes + return Promise.all( + Object.keys(fileStore).map((key) => { + FileStore.upsert({ + SHA256: fileStore[key].SHA256, + fileName: key, + data: fileStore[key].data, + orgUid: myOrganization.orgUid, + }); + }), + ); + }); + } return FileStore.findAll({ attributes: ['SHA256', 'fileName', 'orgUid'], diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 85a782b1..e537be3b 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -9,13 +9,14 @@ import { sequelize } from '../../database'; import datalayer from '../../datalayer'; import { logger } from '../../config/logger.cjs'; +import { FileStore } from '../'; import { getDefaultOrganizationList } from '../../utils/data-loaders'; import { getDataModelVersion } from '../../utils/helpers'; import { getConfig } from '../../utils/config-loader'; -const { USE_SIMULATOR } = getConfig().APP; +const { USE_SIMULATOR, AUTO_SUBSCRIBE_FILESTORE } = getConfig().APP; logger.info('CADT:organizations'); @@ -287,6 +288,10 @@ class Organization extends Model { subscribed: true, isHome: false, }); + + if (AUTO_SUBSCRIBE_FILESTORE) { + await FileStore.subscribeToFileStore(orgUid); + } } catch (error) { logger.info(error.message); } diff --git a/src/utils/defaultConfig.js b/src/utils/defaultConfig.js index b99719b8..594d5bb0 100644 --- a/src/utils/defaultConfig.js +++ b/src/utils/defaultConfig.js @@ -20,6 +20,7 @@ export const defaultConfig = { DEFAULT_COIN_AMOUNT: 300000000, CERTIFICATE_FOLDER_PATH: null, DATALAYER_FILE_SERVER_URL: null, + AUTO_SUBSCRIBE_FILESTORE: false, TASKS: { AUDIT_SYNC_TASK_INTERVAL: 30, DATAMODEL_SYNC_TASK_INTERVAL: 60,