Skip to content

Commit

Permalink
fix: filestore in readonly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D authored and TheLastCicada committed Sep 6, 2023
1 parent e6aaac8 commit c88202e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
55 changes: 31 additions & 24 deletions src/models/file-store/file-store.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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'],
Expand Down
7 changes: 6 additions & 1 deletion src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c88202e

Please sign in to comment.