Skip to content

Commit

Permalink
feat: data model for organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Jan 17, 2024
1 parent 12eddc8 commit 8a413fd
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import Config from '../models/site/config.js';
import { Config } from '../models/site/config.js';
import { createOrganization } from '../models/organization.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/src/dto/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { createSite } from '../models/site.js';
import AuditConfig from '../models/site/audit-config.js';
import Config from '../models/site/config.js';
import { Config } from '../models/site/config.js';

/**
* Data transfer object for Site.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { hasText, isObject } from '@adobe/spacecat-shared-utils';

import { Base } from './base.js';
import Config from './site/config.js';
import { Config, defaultConfig } from './site/config.js';

/**
* Creates a new Organization.
Expand Down Expand Up @@ -88,12 +88,7 @@ export const createOrganization = (data) => {
const newState = { ...data };

if (!isObject(newState.config)) {
newState.config = {
slack: {
},
alerts: {
},
};
newState.config = { ...defaultConfig };
}
if (!hasText(newState.name)) {
throw new Error('Org name must be provided');
Expand Down
9 changes: 2 additions & 7 deletions packages/spacecat-shared-data-access/src/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isObject, isValidUrl } from '@adobe/spacecat-shared-utils';

import { Base } from './base.js';
import AuditConfig from './site/audit-config.js';
import Config from './site/config.js';
import { Config, defaultConfig } from './site/config.js';

export const DELIVERY_TYPES = {
AEM_CS: 'aem_cs',
Expand Down Expand Up @@ -183,12 +183,7 @@ export const createSite = (data) => {
newState.auditConfig = AuditConfig(newState.auditConfig);

if (!isObject(newState.config)) {
newState.config = {
slack: {
},
alerts: [{
}],
};
newState.config = { ...defaultConfig };
}

newState.config = Config(newState.config);
Expand Down
17 changes: 11 additions & 6 deletions packages/spacecat-shared-data-access/src/models/site/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import Joi from 'joi';

const configSchema = Joi.object({
export const configSchema = Joi.object({
slack: Joi.object({
workspace: Joi.string(),
channel: Joi.string(),
Expand All @@ -23,7 +23,14 @@ const configSchema = Joi.object({
mentions: Joi.array().items(Joi.object({ slack: Joi.array().items(Joi.string()) })),
})),
});
const Config = (data = {}) => {

export const defaultConfig = {
slack: {
},
alerts: [],
};

export const Config = (data = {}) => {
const state = {
slack: {
channel: data?.slack?.channel,
Expand All @@ -33,8 +40,8 @@ const Config = (data = {}) => {
};

const self = {
alerts: () => state.alerts,
slack: () => state.slack,
alerts: state.alerts,
slack: state.slack,
};

return Object.freeze(self);
Expand All @@ -60,5 +67,3 @@ Config.toDynamoItem = (config) => {
throw new Error(`Error validating config ${e.message}`);
}
};

export default Config;
15 changes: 3 additions & 12 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isIsoDate } from '@adobe/spacecat-shared-utils';
import { v4 as uuidv4 } from 'uuid';
import { sleep } from '../unit/util.js';
import { createDataAccess } from '../../src/service/index.js';
import { configSchema } from '../../src/models/site/config.js';
import { AUDIT_TYPE_LHS_MOBILE } from '../../src/models/audit.js';

import generateSampleData from './generateSampleData.js';
Expand Down Expand Up @@ -55,17 +56,7 @@ function checkOrganization(organization) {
id: Joi.string(),
name: Joi.string(),
imsOrgId: Joi.string(),
config: Joi.object({
slack: Joi.object({
workspace: Joi.string(),
channel: Joi.string(),
}),
alerts: Joi.array().items(Joi.object({
type: Joi.string(),
byOrg: Joi.boolean(),
mentions: Joi.array().items(Joi.object({ slack: Joi.string() })),
})),
}),
config: configSchema,
});
schema.validate(organization);
}
Expand Down Expand Up @@ -171,7 +162,7 @@ describe('DynamoDB Integration Test', async () => {

expect(newOrg.getName()).to.equal(newOrgData.name);
expect(newOrg.getImsOrgId()).to.equal(newOrgData.imsOrgId);
expect(newOrg.getConfig()).to.be.an('object').that.is.empty;
expect(newOrg.getConfig()).to.be.an('object');
});

it('updates an existing org', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Organization Model Tests', () => {
const config = org.getConfig();

expect(config).to.be.an('object');
expect(config.alerts).to.be.an('object');
expect(config.alerts).to.be.an('array');
expect(config.slack).to.be.an('object');
});

Expand All @@ -50,18 +50,16 @@ describe('Organization Model Tests', () => {
alerts: [{
type: '404',
byOrg: false,
mentions: [{ slack: 'slackId' }],
mentions: [{ slack: ['slackId'] }],
}],
};
const org = createOrganization({ ...validData, conf });
const org = createOrganization({ ...validData, config: conf });
const config = org.getConfig();

expect(config.slack).to.be.an('object');
expect(config.alerts).to.be.an('object');
expect(config.alerts).to.be.an('array');
expect(config.slack.workspace).to.equal('workspace');
expect(config.slack.channel).to.equal('channel');

expect(config.alerts).to.be.an('object');
});
});

Expand Down

0 comments on commit 8a413fd

Please sign in to comment.