diff --git a/app/src/services/sync.js b/app/src/services/sync.js index af12478c..2f664498 100644 --- a/app/src/services/sync.js +++ b/app/src/services/sync.js @@ -328,17 +328,25 @@ const service = { const comsTags = comsTagsForVersion[0]?.tagset ?? []; // S3 Tags const s3Tags = toLowerKeys(s3TagsForVersion?.TagSet ?? []); - - // Ensure `coms-id` tag exists on this version in S3 - if (s3Tags.length < 10 && !s3Tags.find(s3T => s3T.key === 'coms-id')) { + /** + * Add coms-id tag to latest version in S3 if not already present + * NOTE: For a sync job the _deriveObjectId() function will have already added + * the coms-id to latest version. + * TODO: check if this version is still also the latest on corresponding version in S3 + */ + if (comsVersion.isLatest && s3Tags.length < 10 && !s3Tags.find(s3T => s3T.key === 'coms-id')) { + /** + * NOTE: adding tags to a specified version (passing a `VersionId` parameter) will affect `Last Modified` + * attribute of multiple versions on some s3 storage providors including Dell ECS + */ await storageService.putObjectTagging({ filePath: path, tags: (s3TagsForVersion?.TagSet ?? []).concat([{ Key: 'coms-id', Value: comsVersion.objectId }]), - s3VersionId: comsVersion.s3VersionId, bucketId: bucketId, + // s3VersionId: comsVersion.s3VersionId, }); // add to our arrays for comaprison s3Tags.push({ key: 'coms-id', value: comsVersion.objectId }); diff --git a/app/tests/unit/services/sync.spec.js b/app/tests/unit/services/sync.spec.js index a727a3e8..f4d525ce 100644 --- a/app/tests/unit/services/sync.spec.js +++ b/app/tests/unit/services/sync.spec.js @@ -825,7 +825,8 @@ describe('syncTags', () => { const comsVersion = { id: validUuidv4, objectId: validUuidv4, - s3VersionId: validUuidv4 + s3VersionId: validUuidv4, + isLatest: true }; beforeEach(() => { @@ -898,7 +899,6 @@ describe('syncTags', () => { Key: 'coms-id', Value: comsVersion.objectId }]), - s3VersionId: comsVersion.s3VersionId, bucketId: bucketId, })); expect(versionTrx.commit).toHaveBeenCalledTimes(1); @@ -940,7 +940,75 @@ describe('syncTags', () => { Key: 'coms-id', Value: comsVersion.objectId }]), + bucketId: bucketId, + })); + expect(versionTrx.commit).toHaveBeenCalledTimes(1); + }); + + it('should not write coms-id tag when coms version is not latest', async () => { + fetchTagsForVersionSpy.mockResolvedValue([{}]); + getObjectTaggingSpy.mockResolvedValue({}); + putObjectTaggingSpy.mockResolvedValue({}); + + comsVersion.isLatest = false; + const result = await service.syncTags(comsVersion, path, bucketId); + // reset for other tests + comsVersion.isLatest = true; + + expect(result).toBeTruthy(); + expect(Array.isArray(result)).toBeTruthy(); + expect(result).toHaveLength(0); + + expect(Version.startTransaction).toHaveBeenCalledTimes(1); + expect(associateTagsSpy).toHaveBeenCalledTimes(0); + expect(dissociateTagsSpy).toHaveBeenCalledTimes(0); + expect(fetchTagsForVersionSpy).toHaveBeenCalledTimes(1); + expect(fetchTagsForVersionSpy).toHaveBeenCalledWith(expect.objectContaining({ + versionIds: comsVersion.id + }), expect.any(Object)); + expect(getObjectTaggingSpy).toHaveBeenCalledTimes(1); + expect(getObjectTaggingSpy).toHaveBeenCalledWith(expect.objectContaining({ + filePath: path, s3VersionId: comsVersion.s3VersionId, + bucketId: bucketId + })); + expect(getSpy).toHaveBeenCalledTimes(0); + expect(putObjectTaggingSpy).toHaveBeenCalledTimes(0); + expect(versionTrx.commit).toHaveBeenCalledTimes(1); + }); + + it('should write coms-id tag when coms version is latest', async () => { + fetchTagsForVersionSpy.mockResolvedValue([{}]); + getObjectTaggingSpy.mockResolvedValue({}); + putObjectTaggingSpy.mockResolvedValue({}); + + const result = await service.syncTags(comsVersion, path, bucketId); + + expect(result).toBeTruthy(); + expect(Array.isArray(result)).toBeTruthy(); + expect(result).toHaveLength(1); + + expect(Version.startTransaction).toHaveBeenCalledTimes(1); + expect(associateTagsSpy).toHaveBeenCalledTimes(1); + expect(dissociateTagsSpy).toHaveBeenCalledTimes(0); + expect(fetchTagsForVersionSpy).toHaveBeenCalledTimes(1); + expect(fetchTagsForVersionSpy).toHaveBeenCalledWith(expect.objectContaining({ + versionIds: comsVersion.id + }), expect.any(Object)); + expect(getObjectTaggingSpy).toHaveBeenCalledTimes(1); + expect(getObjectTaggingSpy).toHaveBeenCalledWith(expect.objectContaining({ + filePath: path, + s3VersionId: comsVersion.s3VersionId, + bucketId: bucketId + })); + expect(getSpy).toHaveBeenCalledTimes(0); + expect(putObjectTaggingSpy).toHaveBeenCalledTimes(1); + expect(putObjectTaggingSpy).toHaveBeenCalledWith(expect.objectContaining({ + filePath: path, + tags: expect.arrayContaining([{ + Key: 'coms-id', + Value: comsVersion.objectId + }]), bucketId: bucketId, })); expect(versionTrx.commit).toHaveBeenCalledTimes(1);