Skip to content

feat: Add tenant id to spacecat organization data #1108

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@adobe/helix-universal-logger": "3.0.27",
"@adobe/spacecat-shared-athena-client": "1.2.4",
"@adobe/spacecat-shared-brand-client": "1.1.19",
"@adobe/spacecat-shared-data-access": "2.43.2",
"@adobe/spacecat-shared-data-access": "https://gitpkg.now.sh/adobe/spacecat-shared/packages/spacecat-shared-data-access?f9cdcb7",
"@adobe/spacecat-shared-gpt-client": "1.5.19",
"@adobe/spacecat-shared-http-utils": "1.15.2",
"@adobe/spacecat-shared-ims-client": "1.8.7",
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ function OrganizationsController(ctx, env) {
}

try {
const organization = await Organization.create(context.data);
const { imsClient, data, log } = context;
const { imsOrgId } = data;
log.info(`Getting IMS org details for ${imsOrgId}`);
const imsOrg = await imsClient.getImsOrganizationDetails(imsOrgId);
log.info(`IMS org details: ${JSON.stringify(imsOrg)}`);
const { tenantId, orgName } = imsOrg;
const newOrgData = {
name: orgName,
imsOrgId,
tenantId,
};
const organization = await Organization.create(newOrgData);
log.info(`Organization created: ${JSON.stringify(organization)}`);
return createResponse(OrganizationDto.toJSON(organization), 201);
} catch (e) {
return badRequest(e.message);
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async function run(request, context) {
const routeMatch = matchPath(method, suffix, routeHandlers);

if (routeMatch) {
log.info(`Route match: ${JSON.stringify(routeMatch)}`);
const { handler, params } = routeMatch;

if (params.siteId && !isValidUUIDV4(params.siteId)) {
Expand All @@ -172,8 +173,9 @@ async function run(request, context) {
&& (!isValidUUIDV4(params.organizationId) && params.organizationId !== 'default')) {
return badRequest('Organization Id is invalid. Please provide a valid UUID.');
}
log.info(`Params: ${JSON.stringify(params)}`);
context.params = params;

log.info(`Calling handler: ${handler.name}`);
return await handler(context);
} else {
const notFoundMessage = `no such route /${route}`;
Expand Down
1 change: 1 addition & 0 deletions src/support/slack/commands/set-ims-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function SetSiteOrganizationCommand(context) {
spaceCatOrg = await Organization.create({
name: imsOrgDetails.orgName,
imsOrgId: userImsOrgId,
tenantId: imsOrgDetails.tenantId,
});
await spaceCatOrg.save();

Expand Down
95 changes: 80 additions & 15 deletions test/controllers/organizations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,46 +204,111 @@ describe('Organizations Controller', () => {
expect(() => OrganizationsController({ dataAccess: mockDataAccess })).to.throw('Environment object required');
});

it('creates an organization', async () => {
it('creates an organization for non admin users', async () => {
context.attributes.authInfo.withProfile({ is_admin: false });
mockDataAccess.Organization.create.resolves(organizations[0]);
const response = await organizationsController.createOrganization({
data: { name: 'Org 1', tenantId: 'test-tenant' },
...context,
});
expect(response.status).to.equal(403);

const error = await response.json();
expect(error).to.have.property('message', 'Only admins can create new Organizations');
});

it('creates an organization with IMS integration', async () => {
const mockImsOrg = {
tenantId: 'test-tenant-id',
orgName: 'Test Organization',
};

const mockImsClient = {
getImsOrganizationDetails: sinon.stub().resolves(mockImsOrg),
};

const mockLog = {
info: sinon.stub(),
};

mockDataAccess.Organization.create.resolves(organizations[0]);

const response = await organizationsController.createOrganization({
data: { name: 'Org 1' },
data: { imsOrgId: 'test-ims-org-id' },
imsClient: mockImsClient,
log: mockLog,
...context,
});

expect(mockDataAccess.Organization.create).to.have.been.calledOnce;
expect(mockImsClient.getImsOrganizationDetails).to.have.been.calledOnceWith('test-ims-org-id');
expect(mockDataAccess.Organization.create).to.have.been.calledOnceWith({
name: 'Test Organization',
imsOrgId: 'test-ims-org-id',
tenantId: 'test-tenant-id',
});
expect(response.status).to.equal(201);

const organization = await response.json();
expect(organization).to.have.property('id', '9033554c-de8a-44ac-a356-09b51af8cc28');
expect(organization).to.have.property('name', 'Org 1');
});

it('creates an organization for non admin users', async () => {
context.attributes.authInfo.withProfile({ is_admin: false });
mockDataAccess.Organization.create.resolves(organizations[0]);
it('handles IMS client error during organization creation', async () => {
const mockImsClient = {
getImsOrganizationDetails: sinon.stub().rejects(new Error('IMS API Error')),
};

const mockLog = {
info: sinon.stub(),
};

const response = await organizationsController.createOrganization({
data: { name: 'Org 1' },
data: { imsOrgId: 'test-ims-org-id' },
imsClient: mockImsClient,
log: mockLog,
...context,
});
expect(response.status).to.equal(403);

expect(mockImsClient.getImsOrganizationDetails).to.have.been.calledOnceWith('test-ims-org-id');
expect(mockDataAccess.Organization.create).to.not.have.been.called;
expect(response.status).to.equal(400);

const error = await response.json();
expect(error).to.have.property('message', 'Only admins can create new Organizations');
expect(error).to.have.property('message', 'IMS API Error');
});

it('returns bad request when creating an organization fails', async () => {
mockDataAccess.Organization.create.rejects(new Error('Failed to create organization'));
it('handles organization creation error after IMS lookup', async () => {
const mockImsOrg = {
tenantId: 'test-tenant-id',
orgName: 'Test Organization',
};

const mockImsClient = {
getImsOrganizationDetails: sinon.stub().resolves(mockImsOrg),
};

const mockLog = {
info: sinon.stub(),
};

mockDataAccess.Organization.create.rejects(new Error('Database Error'));

const response = await organizationsController.createOrganization({
data: { name: 'Org 1' },
data: { imsOrgId: 'test-ims-org-id' },
imsClient: mockImsClient,
log: mockLog,
...context,
});

expect(mockDataAccess.Organization.create).to.have.been.calledOnce;
expect(mockImsClient.getImsOrganizationDetails).to.have.been.calledOnceWith('test-ims-org-id');
expect(mockDataAccess.Organization.create).to.have.been.calledOnceWith({
name: 'Test Organization',
imsOrgId: 'test-ims-org-id',
tenantId: 'test-tenant-id',
});
expect(response.status).to.equal(400);

const error = await response.json();
expect(error).to.have.property('message', 'Failed to create organization');
expect(error).to.have.property('message', 'Database Error');
});

it('updates an organization', async () => {
Expand Down
6 changes: 5 additions & 1 deletion test/support/slack/commands/set-ims-org.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ describe('SetSiteOrganizationCommand', () => {
};
siteStub.findByBaseURL.resolves(mockSite);
organizationStub.findByImsOrgId.resolves(null);
imsClientStub.getImsOrganizationDetails.resolves({ orgName: 'Mock IMS Org' });
imsClientStub.getImsOrganizationDetails.resolves({
orgName: 'Mock IMS Org',
tenantId: 'test-tenant-id',
});

const mockNewOrg = {
getId: () => 'newOrgId',
Expand All @@ -141,6 +144,7 @@ describe('SetSiteOrganizationCommand', () => {
expect(organizationStub.create.calledWith({
name: 'Mock IMS Org',
imsOrgId: 'newImsOrgId',
tenantId: 'test-tenant-id',
})).to.be.true;
expect(mockNewOrg.save.calledOnce).to.be.true;
expect(mockSite.setOrganizationId.calledWith('newOrgId')).to.be.true;
Expand Down