diff --git a/packages/spacecat-shared-data-access/src/models/site.js b/packages/spacecat-shared-data-access/src/models/site.js index 1db378270..25ad4e24f 100644 --- a/packages/spacecat-shared-data-access/src/models/site.js +++ b/packages/spacecat-shared-data-access/src/models/site.js @@ -65,6 +65,27 @@ const Site = (data = {}) => { return self; }; */ + /** + * Updates the GitHub URL belonging to the site. + * @param {string} gitHubURL - The GitHub URL. + * @return {Base} The updated site. + */ + self.updateGitHubURL = (gitHubURL) => { + if (!isValidUrl(gitHubURL)) { + throw new Error('GitHub URL must be a valid URL'); + } + + self.state.gitHubURL = gitHubURL; + self.touch(); + + return self; + }; + + /** + * Updates the IMS Org ID belonging to the site. + * @param {string} imsOrgId - The IMS Org ID. + * @return {Base} The updated site. + */ self.updateImsOrgId = (imsOrgId) => { if (!hasText(imsOrgId)) { throw new Error('IMS Org ID must be provided'); @@ -81,6 +102,10 @@ const Site = (data = {}) => { return self; }; + /** + * Sets whether the site is live. + * @return {Base} The updated site. + */ self.toggleLive = () => { self.state.isLive = !self.state.isLive; return self; diff --git a/packages/spacecat-shared-data-access/test/it/db.test.js b/packages/spacecat-shared-data-access/test/it/db.test.js index 3ba949b68..ed7fdacc1 100644 --- a/packages/spacecat-shared-data-access/test/it/db.test.js +++ b/packages/spacecat-shared-data-access/test/it/db.test.js @@ -172,15 +172,20 @@ describe('DynamoDB Integration Test', async () => { it('updates an existing site', async () => { const siteToUpdate = await dataAccess.getSiteByBaseURL('https://example1.com'); const originalUpdatedAt = siteToUpdate.getUpdatedAt(); + const newGitHubURL = 'https://github.com/newOrg/some-repo'; const newImsOrgId = 'updatedOrg123'; await sleep(10); // Make sure updatedAt is different + siteToUpdate.updateGitHubURL(newGitHubURL); siteToUpdate.updateImsOrgId(newImsOrgId); + siteToUpdate.toggleLive(); const updatedSite = await dataAccess.updateSite(siteToUpdate); + expect(updatedSite.getGitHubURL()).to.equal(newGitHubURL); expect(updatedSite.getImsOrgId()).to.equal(newImsOrgId); + expect(updatedSite.isLive()).to.be.false; expect(updatedSite.getUpdatedAt()).to.not.equal(originalUpdatedAt); }); diff --git a/packages/spacecat-shared-data-access/test/unit/models/site.test.js b/packages/spacecat-shared-data-access/test/unit/models/site.test.js index c9f7636eb..60713ec51 100644 --- a/packages/spacecat-shared-data-access/test/unit/models/site.test.js +++ b/packages/spacecat-shared-data-access/test/unit/models/site.test.js @@ -60,10 +60,20 @@ describe('Site Model Tests', () => { expect(site.getImsOrgId()).to.equal(newImsOrgId); }); + it('updates gitHubURL correctly', () => { + const newGitHubURL = 'https://gibhub.com/example/example'; + site.updateGitHubURL(newGitHubURL); + expect(site.getGitHubURL()).to.equal(newGitHubURL); + }); + it('throws an error when updating with an empty imsOrgId', () => { expect(() => site.updateImsOrgId('')).to.throw('IMS Org ID must be provided'); }); + it('throws an error when updating with an invalid github URL', () => { + expect(() => site.updateGitHubURL('')).to.throw('GitHub URL must be a valid URL'); + }); + it('sets audits correctly', () => { const audits = [{ id: 'audit1' }, { id: 'audit2' }]; site.setAudits(audits); @@ -92,6 +102,16 @@ describe('Site Model Tests', () => { expect(site.getUpdatedAt()).to.not.equal(initialUpdatedAt); }); + it('updates updatedAt when gitHubURL is updated', async () => { + const initialUpdatedAt = site.getUpdatedAt(); + + await sleep(20); + + site.updateGitHubURL('https://gibhub.com/example/example'); + + expect(site.getUpdatedAt()).to.not.equal(initialUpdatedAt); + }); + it('toggles live status', async () => { expect(site.isLive()).to.be.false;