Skip to content

Commit

Permalink
feat: update audit config (#63)
Browse files Browse the repository at this point in the history
* feat: add update audit config

* fix: it test
  • Loading branch information
solaris007 authored Dec 16, 2023
1 parent ce46d7d commit 00a468e
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 29 deletions.
135 changes: 127 additions & 8 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,185 @@

// TODO: introduce AuditType interface or Scores interface

/**
* Represents an individual audit of a site.
*/
export interface Audit {
/**
* Retrieves the site ID associated with this audit.
* @returns {string} The site ID.
*/
getSiteId: () => string;

/**
* Retrieves the timestamp when the audit was performed.
* @returns {string} The audit timestamp.
*/
getAuditedAt: () => string;

/**
* Retrieves the result of the audit.
* @returns {object} The audit result.
*/
getAuditResult: () => object;

/**
* Retrieves the type of the audit.
* @returns {object} The audit type.
*/
getAuditType: () => object;

/**
* Retrieves the expiration date of the audit.
* @returns {Date} The expiration date.
*/
getExpiresAt: () => Date;

/**
* Retrieves a reference to the full audit.
* @returns {string} The full audit reference.
*/
getFullAuditRef: () => string;

/**
* Indicates whether the audit is live.
* @returns {boolean} True if the audit is live, false otherwise.
*/
isLive: () => boolean;

/**
* Indicates whether there was an error in the audit.
* @returns {boolean} True if there was an error, false otherwise.
*/
isError: () => boolean;

/**
* Retrieves the scores from the audit.
* @returns {object} The audit scores.
*/
getScores: () => object;
}

/**
* AuditConfigType defines the structure for specific audit type configurations
* AuditConfigType defines the structure for specific audit type configurations.
*/
export interface AuditConfigType {
/**
* Returns true if the audit type is disabled for the site. If an audit type is disabled, no
* audits of that type will be scheduled for the site.
* @returns True if the audit type is disabled for the site
* @returns {boolean} True if the audit type is disabled for the site.
*/
disabled(): boolean;
disabled: () => boolean;
}

/**
* AuditConfig defines the structure for the overall audit configuration of a site
* AuditConfig defines the structure for the overall audit configuration of a site.
*/
export interface AuditConfig {
/**
* Returns true if audits are disabled for the site. If audits are disabled, no audits will be
* scheduled for the site. Overrides any audit type specific configurations.
* @returns True if audits are disabled for the site
* @returns {boolean} True if audits are disabled for the site.
*/
auditsDisabled: () => boolean;

/**
* Returns the audit config for a specific audit type. The audit type is the key.
* @param auditType The audit type to get the config for
* @returns The audit config for the audit type
* @param {string} auditType The audit type to get the config for.
* @returns {AuditConfigType} The audit config for the audit type.
*/
getAuditTypeConfig: (auditType: string) => AuditConfigType;

/**
* Returns the audit configs for all audit types. The keys are the audit types.
* @returns The audit configs for all audit types
* @returns {object} The audit configs for all audit types.
*/
getAuditTypeConfigs: () => object;
}

/**
* Represents a site with associated audit and configuration data.
*/
export interface Site {
/**
* Retrieves the ID of the site.
* @returns {string} The site ID.
*/
getId: () => string;

/**
* Retrieves the base URL of the site.
* @returns {string} The base URL.
*/
getBaseURL: () => string;

/**
* Retrieves the GitHub URL associated with the site.
* @returns {string} The GitHub URL.
*/
getGitHubURL: () => string;

/**
* Retrieves the IMS Organization ID associated with the site.
* @returns {string} The IMS Org ID.
*/
getImsOrgId: () => string;

/**
* Retrieves the creation timestamp of the site.
* @returns {string} The creation timestamp.
*/
getCreatedAt: () => string;

/**
* Retrieves the last update timestamp of the site.
* @returns {string} The last update timestamp.
*/
getUpdatedAt: () => string;

/**
* Retrieves the current audit configuration for the site.
* @returns {AuditConfig} The current audit configuration.
*/
getAuditConfig: () => AuditConfig;

/**
* Retrieves the audits associated with the site.
* @returns {Audit[]} The list of audits.
*/
getAudits: () => Audit[];

/**
* Indicates whether the site is live.
* @returns {boolean} True if the site is live, false otherwise.
*/
isLive: () => boolean;

/**
* Updates the list of audits for the site.
* @param {Audit[]} audits The new list of audits.
* @returns {Site} The updated site instance.
*/
setAudits: (audits: Audit[]) => Site;

/**
* Toggles the live status of the site.
* @returns {Site} The updated site instance with the toggled live status.
*/
toggleLive: () => Site;

/**
* Updates the GitHub URL of the site.
* @param {string} gitHubURL The new GitHub URL.
* @returns {Site} The updated site instance.
*/
updateGitHubURL: (gitHubURL: string) => Site;

/**
* Updates the IMS Org ID of the site.
* @param {string} imsOrgId The new IMS Org ID.
* @returns {Site} The updated site instance.
*/
updateImsOrgId: (imsOrgId: string) => Site;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/spacecat-shared-data-access/src/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ const Site = (data = {}) => {
return self;
}; */

self.setAllAuditsDisabled = (disabled) => {
self.state.auditConfig.updateAuditsDisabled(disabled);
self.touch();
return self;
};

self.updateAuditTypeConfig = (type, config) => {
self.state.auditConfig.updateAuditTypeConfig(type, config);
self.touch();
return self;
};

/**
* Updates the GitHub URL belonging to the site.
* @param {string} gitHubURL - The GitHub URL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@
* governing permissions and limitations under the License.
*/

const AuditConfigType = (data = {}, allAuditsDisabled = false) => ({
disabled: () => allAuditsDisabled || data.disabled || false,
});
const AuditConfigType = (data = {}) => {
const state = {
disabled: data.disabled || false,
};

const self = {
disabled: () => state.disabled,

updateDisabled: (newValue) => {
state.disabled = newValue;
},
};

return Object.freeze(self);
};

AuditConfigType.fromDynamoItem = (dynamoItem) => {
const auditConfigTypeData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import AuditConfigType from './audit-config-type.js';

/**
* Initializes the audit type configs. If auditsDisabled is true, all audit types will be disabled.
* @param {object} auditTypeConfigs - Object containing audit type configs.
* @param {boolean} auditsDisabled - Flag indicating if all audits are disabled.
* @return {object} Object containing audit type configs.
*/
function getAuditTypeConfigs(auditTypeConfigs, auditsDisabled) {
return Object.entries(auditTypeConfigs || {}).reduce((acc, [key, value]) => {
acc[key] = AuditConfigType(value, auditsDisabled);
Expand All @@ -25,12 +20,24 @@ function getAuditTypeConfigs(auditTypeConfigs, auditsDisabled) {
}

const AuditConfig = (data = {}) => {
const auditTypeConfigs = getAuditTypeConfigs(data.auditTypeConfigs, data.auditsDisabled);
return {
auditsDisabled: () => data.auditsDisabled || false,
getAuditTypeConfigs: () => auditTypeConfigs,
getAuditTypeConfig: (type) => auditTypeConfigs[type],
const state = {
auditsDisabled: data.auditsDisabled || false,
auditTypeConfigs: getAuditTypeConfigs(data.auditTypeConfigs, data.auditsDisabled),
};

const self = {
auditsDisabled: () => state.auditsDisabled,
getAuditTypeConfigs: () => state.auditTypeConfigs,
getAuditTypeConfig: (type) => state.auditTypeConfigs[type],
updateAuditsDisabled: (newValue) => {
state.auditsDisabled = newValue;
},
updateAuditTypeConfig: (type, config) => {
state.auditTypeConfigs[type] = AuditConfigType(config);
},
};

return Object.freeze(self);
};

AuditConfig.fromDynamoItem = (dynamoItem) => AuditConfig(dynamoItem);
Expand Down
18 changes: 18 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 @@ -401,4 +401,22 @@ describe('DynamoDB Integration Test', async () => {
);
expect(latestAuditAfterRemoval).to.be.null;
});

it('updates audit configurations for a site', async () => {
const siteToUpdate = await dataAccess.getSiteByBaseURL('https://example2.com');

// Update all audits to be disabled
siteToUpdate.setAllAuditsDisabled(true);
await dataAccess.updateSite(siteToUpdate);

let updatedSite = await dataAccess.getSiteByID(siteToUpdate.getId());
expect(updatedSite.getAuditConfig().auditsDisabled()).to.be.true;

// Update a specific audit type configuration
siteToUpdate.updateAuditTypeConfig('type1', { disabled: false });
await dataAccess.updateSite(siteToUpdate);

updatedSite = await dataAccess.getSiteByID(siteToUpdate.getId());
expect(updatedSite.getAuditConfig().getAuditTypeConfig('type1').disabled()).to.be.false;
});
});
27 changes: 25 additions & 2 deletions packages/spacecat-shared-data-access/test/unit/models/site.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe('Site Model Tests', () => {
expect(auditConfig).to.be.an('object');
expect(auditConfig.auditsDisabled()).to.be.true;
expect(auditConfig.getAuditTypeConfig('type1')).to.be.an('object');
expect(auditConfig.getAuditTypeConfig('type1').disabled()).to.be.true;
expect(auditConfig.getAuditTypeConfig('type1').disabled()).to.be.false;
expect(auditConfig.getAuditTypeConfig('type2')).to.be.an('object');
expect(auditConfig.getAuditTypeConfig('type2').disabled()).to.be.true;
expect(auditConfig.getAuditTypeConfig('type2').disabled()).to.be.false;
});
});

Expand Down Expand Up @@ -153,6 +153,14 @@ describe('Site Model Tests', () => {

expect(site.isLive()).to.be.true;
});
});

describe('AuditConfig Integration', () => {
let site;

beforeEach(() => {
site = createSite(validData);
});

it('handles AuditConfig and AuditConfigType correctly', () => {
const auditConfigData = {
Expand All @@ -170,5 +178,20 @@ describe('Site Model Tests', () => {
expect(auditConfig.getAuditTypeConfig('type1')).to.be.an('object');
expect(auditConfig.getAuditTypeConfig('type1').disabled()).to.be.false;
});

it('sets all audits disabled correctly', () => {
site.setAllAuditsDisabled(true);
expect(site.getAuditConfig().auditsDisabled()).to.be.true;
});

it('updates a specific audit type configuration', () => {
site.updateAuditTypeConfig('type1', { disabled: true });
expect(site.getAuditConfig().getAuditTypeConfig('type1').disabled()).to.be.true;
});

it('adds a new audit type configuration if it does not exist', () => {
site.updateAuditTypeConfig('type3', { disabled: true });
expect(site.getAuditConfig().getAuditTypeConfig('type3').disabled()).to.be.true;
});
});
});
Loading

0 comments on commit 00a468e

Please sign in to comment.