Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
20 changes: 20 additions & 0 deletions packages/spacecat-shared-data-access/test/unit/models/site.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down