Skip to content

Commit

Permalink
Further changes to version syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
TimCsaky committed Nov 2, 2023
1 parent b831602 commit af91861
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
6 changes: 4 additions & 2 deletions app/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,12 @@ const utils = {
/**
* @function getUniqueObjects
* @param {object[]} arr array of objects
* @param {string} key key of object property whose value we are comparing
* @returns array of unique objects based on value of a given property
*/
getUniqueObjects(arr, differentiator) {
return [...new Set(arr.map(item => item[differentiator]))];
getUniqueObjects(array, key) {
return [...new Map(array.map(item =>
[item[key], item])).values()];
},

/**
Expand Down
48 changes: 27 additions & 21 deletions app/src/services/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,30 @@ const service = {
.concat(s3VersionsRaw.Versions);

// delete versions from COMS that are not in S3
// get unique coms versions
const uniqueComsVersionIds = getUniqueObjects(comsVersions, 's3VersionId').map(v => v.id);
console.log('uniqueComsVersionIds', uniqueComsVersionIds);

// if comsVersions contains versions (not in s3 OR not in uniqueComsVersionIds)
const comsVersionsToDelete = comsVersions.filter(function(cv) {
const notInS3Versions = !s3Versions.some(s3v => (s3v.VersionId === cv.s3VersionId));
const notInUnique = !uniqueComsVersionIds.some(uv => (uv.id === cv.id));
return notInS3Versions || notInUnique;
})
.map(cv => cv.id);

if(comsVersionsToDelete?.length > 0){
// get list of unique coms versions
const uniqueCVIds = getUniqueObjects(comsVersions, 's3VersionId').map(v => v.id);

// get COMS versions that are not in S3 (matching on s3VersionId) OR not
// in list of unique COMS versions (matching on id)
const cVsToDelete = comsVersions.filter(function(cv) {
const notInS3 = !s3Versions.some(s3v => (s3v.VersionId === cv.s3VersionId));
const isDuplicate = !uniqueCVIds.includes(cv.id);
return notInS3 || isDuplicate;
}).map(cv => cv.id);

if(cVsToDelete?.length > 0){
await Version.query(trx)
.delete()
.where('objectId', comsObject.id)
.whereNotNull('s3VersionId')
.whereIn('id', comsVersionsToDelete);
.whereIn('id', cVsToDelete);
}
// delete versions from comsVersions array for further comparisons
const comsVersionsToKeep = comsVersions.filter(v => !cVsToDelete.includes(v.id));

// Add and Update versions in COMS
const response = await Promise.all(s3Versions.map(async s3Version => {

// S3 Object is in non-versioned bucket
if (s3Version.VersionId === 'null') {
const mimeType = await storageService.headObject({
Expand Down Expand Up @@ -268,12 +270,14 @@ const service = {
}
// S3 Object is in versioned bucket (ie: if VersionId is not 'null')
else {
const comsVersion = comsVersions.find(cv => cv.s3VersionId === s3Version.VersionId);
const comsVersion = comsVersionsToKeep.find(cv => cv.s3VersionId === s3Version.VersionId);
if (comsVersion) { // Version is in COMS
// set isLatest in COMS db
return s3Version.IsLatest ?
{ modified: true, version: await versionService.updateIsLatest(comsObject.id, trx) } :
{ version: comsVersion };
if (s3Version.IsLatest) { // Patch isLatest flags if changed
const updated = await versionService.updateIsLatest(comsObject.id, trx);
return { modified: true, version: updated };
} else { // Version record not modified
return { version: comsVersion };
}
} else { // Version is not in COMS
const mimeType = s3Version.DeleteMarker
? undefined // Will default to 'application/octet-stream'
Expand Down Expand Up @@ -322,7 +326,7 @@ const service = {
trx = etrx ? etrx : await Version.startTransaction();
let response = [];

// Fetch COMS Object record if necessary
// Fetch COMS version record if necessary
const comsVersion = typeof version === 'object' ? version : await versionService.get({ versionId: version }, trx);

// Short circuit if version is a delete marker
Expand Down Expand Up @@ -376,7 +380,9 @@ const service = {
// Associate new S3 Tags
const newTags = [];
for (const s3Tag of s3Tags) {
if (!comsTags.some(comsT => comsT.key === s3Tag.key && comsT.value === s3Tag.value)) {
if (!comsTags.some(comsT => {
return (comsT.key === s3Tag.key && comsT.value === s3Tag.value);
})) {
newTags.push(s3Tag);
} else {
response.push(s3Tag);
Expand Down

0 comments on commit af91861

Please sign in to comment.