Skip to content

Commit

Permalink
fix: datalayer delete endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Aug 4, 2022
1 parent ea1e918 commit e34bf50
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/controllers/fileStore.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export const getFileList = async (req, res) => {

export const deleteFile = async (req, res) => {
try {
await FileStore.deleteFileStorItem(req.body.fileId);
res.status(204).end();
await FileStore.deleteFileStoreItem(req.body.fileId);
res.status(200).json({
message: 'File deleted from filestore',
});
} catch (error) {
res.status(400).json({
message: 'Can not delete file from filestore',
Expand Down Expand Up @@ -64,6 +66,7 @@ export const addFileToFileStore = async (req, res) => {
return res.json({
message:
'File is being added to the file store, please wait for it to confirm.',
fileId: SHA256,
});
} else {
throw new Error('Missing file data, can not upload file.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default {
},
fileName: {
type: Sequelize.STRING,
unique: true,
},
data: Sequelize.STRING,
orgUid: Sequelize.STRING,
Expand Down
3 changes: 0 additions & 3 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ export const getStoreData = async (storeId, rootHash) => {
return {
...record,
key: decodeHex(record.key),
value: /{([^*]*)}/.test(decodeHex(record.value))
? JSON.parse(decodeHex(record.value))
: decodeHex(record.value),
};
}),
null,
Expand Down
6 changes: 1 addition & 5 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const syncDataLayerStoreToClimateWarehouse = async (storeId, rootHash) => {
await Staging.cleanUpCommitedAndInvalidRecords();
}
} catch (error) {
console.trace('ERROR DURING SYNC TRANSACTION', error, storeData);
console.trace('ERROR DURING SYNC TRANSACTION', error);
}
};

Expand Down Expand Up @@ -137,8 +137,6 @@ const dataLayerWasUpdated = async () => {
return [];
}

logger.debug(JSON.stringify(rootResponse));

const updatedStores = rootResponse.root_hashes.filter((rootHash) => {
const org = organizations.find(
(org) => org.registryId == rootHash.id.replace('0x', ''),
Expand Down Expand Up @@ -238,8 +236,6 @@ const getSubscribedStoreData = async (
encodedData = await dataLayer.getStoreData(storeId);
}

logger.debug(`Downloaded from datalayer: ${encodedData?.keys_values}`);

if (_.isEmpty(encodedData?.keys_values)) {
logger.debug(
`Retrying subscribe to ${storeId}, No data detected in store.`,
Expand Down
2 changes: 1 addition & 1 deletion src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const retry = (storeId, changeList, failedCallback, retryAttempts) => {
}, 30000);
};

const pushChangesWhenStoreIsAvailable = async (
export const pushChangesWhenStoreIsAvailable = async (
storeId,
changeList,
failedCallback = _.noop,
Expand Down
38 changes: 25 additions & 13 deletions src/models/file-store/file-store.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class FileStore extends Model {
let fileStoreId = myOrganization.fileStoreId;

if (!fileStoreId) {
fileStoreId = datalayer.createDataLayerStore();
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
Organization.update(
{ fileStoreId },
{ where: { orgUid: myOrganization.orgUid } },
);
datalayer.createDataLayerStore().then((fileStoreId) => {
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
Organization.update(
{ fileStoreId },
{ where: { orgUid: myOrganization.orgUid } },
);
});

throw new Error('New File store being created, please try again later.');
}

Expand Down Expand Up @@ -59,8 +61,13 @@ class FileStore extends Model {
let fileStoreId = myOrganization.fileStoreId;

if (!fileStoreId) {
fileStoreId = await datalayer.createDataLayerStore();
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
datalayer.createDataLayerStore().then((fileStoreId) => {
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
Organization.update(
{ fileStoreId },
{ where: { orgUid: myOrganization.orgUid } },
);
});
throw new Error('New File store being created, please try again later.');
}

Expand Down Expand Up @@ -92,13 +99,18 @@ class FileStore extends Model {
});
}

static async deleteFileStorItem(SHA256) {
static async deleteFileStoreItem(SHA256) {
const myOrganization = await Organization.getHomeOrg();
let fileStoreId = myOrganization.fileStoreId;

if (!fileStoreId) {
fileStoreId = await datalayer.createDataLayerStore();
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
datalayer.createDataLayerStore().then((fileStoreId) => {
datalayer.syncDataLayer(myOrganization.orgUid, { fileStoreId });
Organization.update(
{ fileStoreId },
{ where: { orgUid: myOrganization.orgUid } },
);
});
throw new Error('New File store being created, please try again later.');
}

Expand All @@ -107,9 +119,9 @@ class FileStore extends Model {
key: encodeHex(SHA256),
};

datalayer.pushChangesWhenStoreIsAvailable(fileStoreId, changeList);
datalayer.pushDataLayerChangeList(fileStoreId, changeList);

FileStore.destroy({ where: { SHA256, orgUid: myOrganization.org } });
FileStore.destroy({ where: { SHA256, orgUid: myOrganization.orgUid } });
}

static async getFileStoreItem(SHA256) {
Expand Down
1 change: 0 additions & 1 deletion src/models/file-store/file-store.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
},
fileName: {
type: Sequelize.STRING,
unique: true,
},
data: Sequelize.STRING,
orgUid: Sequelize.STRING,
Expand Down

0 comments on commit e34bf50

Please sign in to comment.