Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow update config for site #121

Merged
merged 8 commits into from
Jan 30, 2024
16 changes: 16 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ const Site = (data = {}) => {
return self;
};

/**
* Updates the site config.
* @param {string} config - The Site config.
* @return {Base} The updated site.
*/
self.updateConfig = (config) => {
if (!isObject(config)) {
throw new Error('Config must be provided');
}

self.state.config = Config.toDynamoItem(config);
self.touch();

return self;
};

self.updateDeliveryType = (deliveryType) => {
if (!Object.values(DELIVERY_TYPES).includes(deliveryType)) {
throw new Error(`Invalid delivery type: ${deliveryType}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('Organization Model Tests', () => {

await sleep(20);

organization.updateName('Name123');
organization.updateConfig({});

expect(organization.getUpdatedAt()).to.not.equal(initialUpdatedAt);
});
Expand Down
35 changes: 35 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 @@ -111,6 +111,27 @@ describe('Site Model Tests', () => {
expect(site.getOrganizationId()).to.equal(organizationId);
});

it('updates config correctly', () => {
const conf = {
slack: {
workspace: 'workspace',
channel: 'channel',
},
alerts: [{
type: '404',
byOrg: false,
mentions: [{ slack: ['slackId'] }],
}],
};
site.updateConfig(conf);
const updatedConf = site.getConfig();
expect(updatedConf.slack).to.be.an('object');
expect(updatedConf.alerts).to.be.an('array');
expect(updatedConf.slack.workspace).to.equal('workspace');
expect(updatedConf.slack.channel).to.equal('channel');
expect(updatedConf.alerts[0].mentions[0].slack[0]).to.equal('slackId');
});

it('updates gitHubURL correctly', () => {
const newGitHubURL = 'https://gibhub.com/example/example';
site.updateGitHubURL(newGitHubURL);
Expand All @@ -125,6 +146,10 @@ describe('Site Model Tests', () => {
expect(() => site.updateGitHubURL('')).to.throw('GitHub URL must be a valid URL');
});

it('throws an error when updating with an invalid config', () => {
expect(() => site.updateConfig('abcd')).to.throw('Config must be provided');
});

it('sets audits correctly', () => {
const audits = [{ id: 'audit1' }, { id: 'audit2' }];
site.setAudits(audits);
Expand Down Expand Up @@ -153,6 +178,16 @@ describe('Site Model Tests', () => {
expect(site.getUpdatedAt()).to.not.equal(initialUpdatedAt);
});

it('updates updatedAt when config is updated', async () => {
const initialUpdatedAt = site.getUpdatedAt();

await sleep(20);

site.updateConfig({});

expect(site.getUpdatedAt()).to.not.equal(initialUpdatedAt);
});

it('updates updatedAt when gitHubURL is updated', async () => {
const initialUpdatedAt = site.getUpdatedAt();

Expand Down
Loading