Skip to content

Commit 6cff471

Browse files
src/tasks/demo-url-processor/handler.js
1 parent 6208774 commit 6cff471

2 files changed

Lines changed: 78 additions & 53 deletions

File tree

src/tasks/demo-url-processor/handler.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const TASK_TYPE = 'demo-url-processor';
1717

1818
/**
1919
* Gets the IMS tenant ID from the organization
20+
* @param {object} imsOrgId - The IMS organization ID
2021
* @param {object} organization - The organization object
2122
* @param {object} context - The context object
2223
* @param {object} log - The log object
@@ -44,8 +45,8 @@ function getImsTenantId(imsOrgId, organization, context, log) {
4445
}
4546

4647
/**
47-
* Runs the audit status processor
48-
* @param {object} demoUrlMessage - The demoUrlMessage object
48+
* Runs the demo URL processor
49+
* @param {object} message - The message object
4950
* @param {object} context - The context object
5051
*/
5152
export async function runDemoUrlProcessor(message, context) {
@@ -61,22 +62,35 @@ export async function runDemoUrlProcessor(message, context) {
6162
log.info('Processing demo url for site:', {
6263
taskType: TASK_TYPE,
6364
siteId,
65+
siteUrl,
66+
imsOrgId,
6467
experienceUrl,
6568
organizationId,
6669
});
6770

68-
const organization = await Organization.findById(organizationId);
69-
if (!organization) {
70-
log.error(`Organization not found for organizationId: ${organizationId}`);
71-
await say(env, log, slackContext, `:x: Organization not found for organizationId: ${organizationId}`);
72-
return ok({ message: 'Organization not found' });
71+
let imsTenantId = context.env.DEFAULT_TENANT_ID;
72+
try {
73+
const organization = await Organization.findById(organizationId);
74+
if (!organization) {
75+
log.error(`Organization not found for organizationId: ${organizationId}`);
76+
if (slackContext) {
77+
await say(env, log, slackContext, `:x: Organization not found for organizationId: ${organizationId}`);
78+
}
79+
return ok({ message: 'Organization not found' });
80+
}
81+
imsTenantId = getImsTenantId(imsOrgId, organization, context, log);
82+
} catch (error) {
83+
log.error(`Error finding organization for organizationId: ${organizationId}`, error);
7384
}
7485

75-
const imsTenantId = getImsTenantId(imsOrgId, organization, context, log);
7686
const demoUrl = `${experienceUrl}?organizationId=${organizationId}#/@${imsTenantId}/sites-optimizer/sites/${siteId}/home`;
7787
const slackMessage = `:white_check_mark: Setup complete for site ${siteUrl}! Access your environment here: ${demoUrl}`;
78-
await say(env, log, slackContext, slackMessage);
79-
log.info(`Setup complete! Access your demo environment here: ${demoUrl}`);
88+
89+
if (slackContext) {
90+
await say(env, log, slackContext, slackMessage);
91+
}
92+
93+
log.info(`Setup complete for site ${siteUrl}! Access your environment here: ${demoUrl}`);
8094

8195
return ok({ message: 'Demo URL processor completed' });
8296
}

test/tasks/demo-url-processor/demo-url-processor.test.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('Demo URL Processor', () => {
5050
// Mock message
5151
message = {
5252
siteId: 'test-site-id',
53+
siteUrl: 'example.com',
5354
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
5455
organizationId: 'test-org-id',
5556
taskContext: {
@@ -74,36 +75,13 @@ describe('Demo URL Processor', () => {
7475
expect(context.log.info.calledWith('Processing demo url for site:', {
7576
taskType: 'demo-url-processor',
7677
siteId: 'test-site-id',
78+
siteUrl: 'example.com',
79+
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
7780
experienceUrl: 'https://example.com',
7881
organizationId: 'test-org-id',
7982
})).to.be.true;
8083
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aem-sites-engineering/sites-optimizer/sites/test-site-id/home';
81-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
82-
});
83-
84-
it('should handle missing slackContext in taskContext', async () => {
85-
// Set up the IMS_ORG_TENANT_ID_MAPPINGS secret in context
86-
context.env.IMS_ORG_TENANT_ID_MAPPINGS = JSON.stringify({
87-
'8C6043F15F43B6390A49401A@AdobeOrg': 'aem-sites-engineering',
88-
});
89-
90-
delete message.taskContext.slackContext;
91-
await runDemoUrlProcessor(message, context);
92-
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aem-sites-engineering/sites-optimizer/sites/test-site-id/home';
93-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
94-
});
95-
96-
it('should use IMS_ORG_TENANT_ID_MAPPINGS mapping when available', async () => {
97-
// Set up the IMS_ORG_TENANT_ID_MAPPINGS secret in context
98-
context.env.IMS_ORG_TENANT_ID_MAPPINGS = JSON.stringify({
99-
'8C6043F15F43B6390A49401A@AdobeOrg': 'aem-sites-engineering',
100-
});
101-
102-
await runDemoUrlProcessor(message, context);
103-
104-
// Should use the mapped tenant name instead of the fallback
105-
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aem-sites-engineering/sites-optimizer/sites/test-site-id/home';
106-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
84+
expect(context.log.info.calledWith(`Setup complete for site example.com! Access your environment here: ${expectedDemoUrl}`)).to.be.true;
10785
});
10886

10987
it('should fallback to name-based tenant when IMS_ORG_TENANT_ID_MAPPINGS mapping is not available', async () => {
@@ -114,7 +92,7 @@ describe('Demo URL Processor', () => {
11492

11593
// Should use the fallback name-based tenant (lowercase, no spaces)
11694
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home';
117-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
95+
expect(context.log.info.calledWith(`Setup complete for site example.com! Access your environment here: ${expectedDemoUrl}`)).to.be.true;
11896
});
11997

12098
it('should fallback to name-based tenant when IMS_ORG_TENANT_ID_MAPPINGS mapping is invalid JSON', async () => {
@@ -125,20 +103,7 @@ describe('Demo URL Processor', () => {
125103

126104
// Should use the fallback name-based tenant
127105
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home';
128-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
129-
});
130-
131-
it('should fallback to name-based tenant when IMS_ORG_TENANT_ID_MAPPINGS mapping does not contain the imsOrgId', async () => {
132-
// Set IMS_ORG_TENANT_ID_MAPPINGS secret with different mapping
133-
context.env.IMS_ORG_TENANT_ID_MAPPINGS = JSON.stringify({
134-
'DIFFERENT_ORG_ID@AdobeOrg': 'different-team',
135-
});
136-
137-
await runDemoUrlProcessor(message, context);
138-
139-
// Should use the fallback name-based tenant since the imsOrgId is not in the mapping
140-
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home';
141-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
106+
expect(context.log.info.calledWith(`Setup complete for site example.com! Access your environment here: ${expectedDemoUrl}`)).to.be.true;
142107
});
143108

144109
it('should handle organization not found error', async () => {
@@ -150,7 +115,7 @@ describe('Demo URL Processor', () => {
150115
// Should log error and return early
151116
expect(context.log.error.calledWith('Organization not found for organizationId: test-org-id')).to.be.true;
152117
// Should not log the success message
153-
expect(context.log.info.calledWithMatch(sinon.match('Setup complete!'))).to.be.false;
118+
expect(context.log.info.calledWithMatch(sinon.match('Setup complete for site example.com!'))).to.be.false;
154119
});
155120

156121
it('should handle organization with missing name property', async () => {
@@ -168,7 +133,53 @@ describe('Demo URL Processor', () => {
168133
// Should log error about missing name and use fallback
169134
expect(context.log.error.calledWith('Organization name is missing, using default tenant ID')).to.be.true;
170135
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@default-tenant/sites-optimizer/sites/test-site-id/home';
171-
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
136+
expect(context.log.info.calledWith(`Setup complete for site example.com! Access your environment here: ${expectedDemoUrl}`)).to.be.true;
137+
});
138+
139+
it('should return success message when processing completes', async () => {
140+
// Set up the IMS_ORG_TENANT_ID_MAPPINGS secret in context
141+
context.env.IMS_ORG_TENANT_ID_MAPPINGS = JSON.stringify({
142+
'8C6043F15F43B6390A49401A@AdobeOrg': 'aem-sites-engineering',
143+
});
144+
145+
// The function should complete without throwing an error
146+
await runDemoUrlProcessor(message, context);
147+
148+
// Verify that the success message was logged
149+
expect(context.log.info.calledWithMatch(sinon.match('Setup complete for site example.com!'))).to.be.true;
150+
});
151+
152+
it('should handle error when Organization.findById throws an exception', async () => {
153+
// Set up the IMS_ORG_TENANT_ID_MAPPINGS secret in context
154+
context.env.IMS_ORG_TENANT_ID_MAPPINGS = JSON.stringify({
155+
'8C6043F15F43B6390A49401A@AdobeOrg': 'aem-sites-engineering',
156+
});
157+
158+
// Mock Organization.findById to throw an error
159+
context.dataAccess.Organization.findById.rejects(new Error('Database connection failed'));
160+
161+
// The function should handle the error gracefully without throwing
162+
await runDemoUrlProcessor(message, context);
163+
164+
// Verify that the error was logged
165+
expect(context.log.error.calledWith('Error finding organization for organizationId: test-org-id', sinon.match.any)).to.be.true;
166+
167+
// Note: The current implementation continues execution even after errors,
168+
// so the success message will still be logged. This test verifies that
169+
// the error handling works and the function completes successfully.
170+
171+
// Verify that the success message was still logged (since the function continues)
172+
expect(context.log.info.calledWithMatch(sinon.match('Setup complete for site example.com!'))).to.be.true;
173+
174+
// Verify that the processing log was recorded
175+
expect(context.log.info.calledWith('Processing demo url for site:', {
176+
taskType: 'demo-url-processor',
177+
siteId: 'test-site-id',
178+
siteUrl: 'example.com',
179+
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
180+
experienceUrl: 'https://example.com',
181+
organizationId: 'test-org-id',
182+
})).to.be.true;
172183
});
173184
});
174185
});

0 commit comments

Comments
 (0)