Skip to content

Commit 67d3ba6

Browse files
refactor + add opportunity status task processor
1 parent f1fca92 commit 67d3ba6

File tree

5 files changed

+115
-104
lines changed

5 files changed

+115
-104
lines changed

src/audit-status-processor/handler.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import dataAccess from '@adobe/spacecat-shared-data-access';
1616
import { sqsEventAdapter } from '@adobe/spacecat-shared-utils';
1717
import { internalServerError, notFound, ok } from '@adobe/spacecat-shared-http-utils';
1818

19-
import { runAuditStatusProcessor as auditStatusProcessor } from './audit-status-processor/handler.js';
20-
import { runDisableImportAuditProcessor as disableImportAuditProcessor } from './disable-import-audit-processor/handler.js';
21-
import { runDemoUrlProcessor as demoUrlProcessor } from './demo-url-processor/handler.js';
19+
import { runOpportunityStatusProcessor as opportunityStatusProcessor } from './tasks/opportunity-status-processor/handler.js';
20+
import { runDisableImportAuditProcessor as disableImportAuditProcessor } from './tasks/disable-import-audit-processor/handler.js';
21+
import { runDemoUrlProcessor as demoUrlProcessor } from './tasks/demo-url-processor/handler.js';
2222

2323
const HANDLERS = {
24-
'audit-status-processor': auditStatusProcessor,
24+
'opportunity-status-processor': opportunityStatusProcessor,
2525
'disable-import-audit-processor': disableImportAuditProcessor,
2626
'demo-url-processor': demoUrlProcessor,
2727
dummy: (message) => ok(message),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { say } from '../utils/slack-utils.js';
13+
import { say } from '../../utils/slack-utils.js';
1414

1515
const TASK_TYPE = 'demo-url-processor';
1616

@@ -23,7 +23,6 @@ function prepareDemoUrl(experienceUrl, organizationId, siteId) {
2323
* Runs the audit status processor
2424
* @param {object} demoUrlMessage - The demoUrlMessage object
2525
* @param {object} context - The context object
26-
* @returns {Promise<object>} The audit result
2726
*/
2827
export async function runDemoUrlProcessor(message, context) {
2928
const { log, env } = context;

src/disable-import-audit-processor/handler.js renamed to src/tasks/disable-import-audit-processor/handler.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { say } from '../utils/slack-utils.js';
13+
import { say } from '../../utils/slack-utils.js';
1414

1515
const TASK_TYPE = 'disable-import-audit-processor';
1616

1717
/**
1818
* Runs the disable import and audit processor
1919
* @param {object} message - The message object containing siteId and auditContext
2020
* @param {object} context - The context object
21-
* @returns {Promise<object>} The result
2221
*/
2322
export async function runDisableImportAuditProcessor(message, context) {
2423
const { log, env, dataAccess } = context;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2025 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import { say } from '../../utils/slack-utils.js';
14+
15+
const TASK_TYPE = 'opportunity-status-processor';
16+
17+
/**
18+
* Gets the opportunity title from the opportunity type
19+
* @param {string} opportunityType - The opportunity type
20+
* @returns {string} The opportunity title
21+
*/
22+
function getOpportunityTitle(opportunityType) {
23+
switch (opportunityType) {
24+
case 'cwv':
25+
return 'Core Web Vitals';
26+
case 'meta-tags':
27+
return 'SEO Meta Tags';
28+
case 'broken-back-links':
29+
return 'Broken Back Links';
30+
case 'broken-links':
31+
return 'Broken Links';
32+
default:
33+
// Convert kebab-case to Title Case (e.g., "first-second" -> "First Second")
34+
return opportunityType
35+
.split('-')
36+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
37+
.join(' ');
38+
}
39+
}
40+
41+
/**
42+
* Runs the opportunity status processor
43+
* @param {object} message - The message object
44+
* @param {object} context - The context object
45+
*/
46+
export async function runOpportunityStatusProcessor(message, context) {
47+
const { log, env, dataAccess } = context;
48+
const { Site } = dataAccess;
49+
log.info('Running opportunity status processor');
50+
const { siteId, organizationId, taskContext } = message;
51+
const {
52+
auditTypes, slackContext,
53+
} = taskContext;
54+
55+
log.info('Processing opportunity status for site:', {
56+
siteId,
57+
organizationId,
58+
taskType: TASK_TYPE,
59+
auditTypes,
60+
});
61+
62+
await say(env, log, slackContext, 'Checking opportunity status');
63+
try {
64+
// Get the site and its opportunities
65+
const site = await Site.findById(siteId);
66+
if (!site) {
67+
log.error(`Site not found for siteId: ${siteId}`);
68+
await say(env, log, slackContext, `:x: Site not found for siteId: ${siteId}`);
69+
return;
70+
}
71+
72+
const opportunities = await site.getOpportunities();
73+
log.info(`Found ${opportunities.length} opportunities for site ${siteId}`);
74+
75+
// Process each opportunity
76+
for (const opportunity of opportunities) {
77+
const opportunityType = opportunity.getType();
78+
const opportunityId = opportunity.getId();
79+
80+
// Get suggestions for this opportunity
81+
// eslint-disable-next-line no-await-in-loop
82+
const suggestions = await site.getSuggestions(opportunityId);
83+
84+
// Get the opportunity title
85+
const opportunityTitle = getOpportunityTitle(opportunityType);
86+
87+
// Determine status based on suggestions length
88+
const hasSuggestions = suggestions && suggestions.length > 0;
89+
const status = hasSuggestions ? ':white_check_mark:' : ':cross_x:';
90+
91+
// Send Slack message
92+
const slackMessage = `${opportunityTitle} ${status}`;
93+
// eslint-disable-next-line no-await-in-loop
94+
await say(env, log, slackContext, slackMessage);
95+
}
96+
97+
log.info('Opportunity status checking completed');
98+
await say(env, log, slackContext, 'Opportunity status checking completed');
99+
} catch (error) {
100+
log.error('Error in opportunity status checking:', {
101+
error: error.message,
102+
stack: error.stack,
103+
errorType: error.name,
104+
});
105+
await say(env, log, slackContext, `:x: Error checking site opportunities status: ${error.message}`);
106+
}
107+
}
108+
109+
export default runOpportunityStatusProcessor;

0 commit comments

Comments
 (0)