|
| 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