1010 * governing permissions and limitations under the License.
1111 */
1212
13- import { Audit } from '@adobe/spacecat-shared-data-access' ;
1413import { say } from '../utils/slack-utils.js' ;
1514
16- const AUDIT_TYPE = 'audit-status-processor' ;
15+ const TASK_TYPE = 'audit-status-processor' ;
1716
1817/**
1918 * Runs the audit status processor
@@ -22,7 +21,8 @@ const AUDIT_TYPE = 'audit-status-processor';
2221 * @returns {Promise<object> } The audit result
2322 */
2423export async function runAuditStatusProcessor ( message , context ) {
25- const { log, env } = context ;
24+ const { log, env, dataAccess } = context ;
25+ const { Site } = dataAccess ;
2626 log . info ( 'Running audit status processor' ) ;
2727 const { siteId, organizationId, taskContext } = message ;
2828 const {
@@ -32,32 +32,54 @@ export async function runAuditStatusProcessor(message, context) {
3232 log . info ( 'Processing audit status for site:' , {
3333 siteId,
3434 organizationId,
35- auditType : AUDIT_TYPE ,
35+ taskType : TASK_TYPE ,
3636 auditTypes,
3737 } ) ;
3838
3939 await say ( env , log , slackContext , 'Checking audit status' ) ;
4040 try {
41- // Check latest audit status for each audit type in parallel
41+ // Get the site and its opportunities
42+ const site = await Site . findById ( siteId ) ;
43+ if ( ! site ) {
44+ log . error ( `Site not found for siteId: ${ siteId } ` ) ;
45+ await say ( env , log , slackContext , `:x: Site not found for siteId: ${ siteId } ` ) ;
46+ return ;
47+ }
48+
49+ const opportunities = await site . getOpportunities ( ) ;
50+ log . info ( `Found ${ opportunities . length } opportunities for site ${ siteId } ` ) ;
51+
52+ // Check opportunities for each audit type
4253 const auditStatusPromises = auditTypes . map ( async ( auditType ) => {
43- const latestAudit = await Audit . getLatestAuditByAuditType ( auditType ) ;
44- log . info ( `Latest audit for site ${ siteId } and audit type ${ auditType } : ${ JSON . stringify ( latestAudit ) } ` ) ;
45- if ( latestAudit ) {
46- const auditResult = latestAudit . getAuditResult ( ) ;
47- if ( auditResult . success ) {
48- log . info ( `Latest audit for site ${ siteId } was successful for audit type ${ auditType } ` ) ;
49- const slackMessage = `:check_mark: Latest audit for site ${ siteId } was successful for audit type ${ auditType } ` ;
54+ const opportunitiesForType = opportunities . filter ( ( opp ) => opp . getType ( ) === auditType ) ;
55+ log . info ( `Found ${ opportunitiesForType . length } opportunities for audit type ${ auditType } ` ) ;
56+
57+ if ( opportunitiesForType . length > 0 ) {
58+ // Get the latest opportunity for this audit type
59+ const latestOpportunity = opportunitiesForType . sort (
60+ ( a , b ) => new Date ( b . getCreatedAt ( ) ) - new Date ( a . getCreatedAt ( ) ) ,
61+ ) [ 0 ] ;
62+
63+ log . info ( `Latest opportunity for site ${ siteId } and audit type ${ auditType } : ${ JSON . stringify ( latestOpportunity ) } ` ) ;
64+
65+ const opportunityData = latestOpportunity . getData ( ) ;
66+ if ( opportunityData && opportunityData . success ) {
67+ log . info ( `Latest opportunity for site ${ siteId } was successful for audit type ${ auditType } ` ) ;
68+ const slackMessage = `:check_mark: Latest opportunity for site ${ siteId } was successful for audit type ${ auditType } ` ;
5069 return say ( env , log , slackContext , slackMessage ) ;
5170 } else {
52- log . warn ( `Latest audit for site ${ siteId } failed for audit type ${ auditType } : ${ auditResult . error || 'Unknown error' } ` ) ;
53- const slackMessage = `:x: Latest audit for site ${ siteId } failed for audit type ${ auditType } : ${ auditResult . error || 'Unknown error' } ` ;
71+ const error = opportunityData ?. error || 'Unknown error' ;
72+ log . warn ( `Latest opportunity for site ${ siteId } failed for audit type ${ auditType } : ${ error } ` ) ;
73+ const slackMessage = `:x: Latest opportunity for site ${ siteId } failed for audit type ${ auditType } : ${ error } ` ;
5474 return say ( env , log , slackContext , slackMessage ) ;
5575 }
5676 } else {
57- log . info ( `No previous ${ auditType } audit found for site ${ siteId } ` ) ;
58- return null ;
77+ log . info ( `No opportunities found for audit type ${ auditType } for site ${ siteId } ` ) ;
78+ const slackMessage = `:information_source: No opportunities found for audit type ${ auditType } for site ${ siteId } ` ;
79+ return say ( env , log , slackContext , slackMessage ) ;
5980 }
6081 } ) ;
82+
6183 await Promise . all ( auditStatusPromises ) ;
6284 log . info ( 'Audit status checking completed' ) ;
6385 await say ( env , log , slackContext , 'Audit status checking completed' ) ;
@@ -67,6 +89,7 @@ export async function runAuditStatusProcessor(message, context) {
6789 stack : error . stack ,
6890 errorType : error . name ,
6991 } ) ;
92+ await say ( env , log , slackContext , `:x: Error checking site opportunities status: ${ error . message } ` ) ;
7093 }
7194}
7295
0 commit comments