Skip to content

Commit edfaeb8

Browse files
address review comments
1 parent cb05f2e commit edfaeb8

3 files changed

Lines changed: 19 additions & 58 deletions

File tree

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"@adobe/helix-status": "10.1.5",
7777
"@adobe/helix-universal": "5.4.0",
7878
"@adobe/helix-universal-logger": "3.0.28",
79-
"@adobe/spacecat-shared-data-access": "https://gist.github.com/tkotthakota-adobe/c18a23bd0566c296ad75701b84ffbef5/raw/dd0062c172263f60f98c8dee6f7a608c7dffabf0/adobe-spacecat-shared-data-access-3.32.1.tgz",
79+
"@adobe/spacecat-shared-data-access": "https://gist.github.com/tkotthakota-adobe/ab119c43b3a874bcda91240f5a458179/raw/eddd18886cbf1d4285418e1f60d987ebd29dc333/adobe-spacecat-shared-data-access-3.32.1.tgz",
8080
"@adobe/spacecat-shared-data-access-v2": "npm:@adobe/spacecat-shared-data-access@2.109.0",
8181
"@adobe/spacecat-shared-google-client": "1.5.8",
8282
"@adobe/spacecat-shared-gpt-client": "1.6.20",
@@ -85,7 +85,7 @@
8585
"@adobe/spacecat-shared-rum-api-client": "2.40.10",
8686
"@adobe/spacecat-shared-scrape-client": "2.5.4",
8787
"@adobe/spacecat-shared-slack-client": "1.6.4",
88-
"@adobe/spacecat-shared-utils": "https://gist.github.com/tkotthakota-adobe/3e5c80c528d7b3d17d1e3d08d708573b/raw/33821e8f69e83f13d40f39f192d383e32276f58f/adobe-spacecat-shared-utils-1.106.1.tgz",
88+
"@adobe/spacecat-shared-utils": "https://gist.github.com/tkotthakota-adobe/24594fa3013e22c6183dacebe2293b42/raw/114b0fba15511512ecfa441dfd91a4a163d029ab/adobe-spacecat-shared-utils-1.106.1.tgz",
8989
"@aws-sdk/client-lambda": "3.1019.0",
9090
"@aws-sdk/client-sqs": "3.1019.0",
9191
"@aws-sdk/client-s3": "3.1019.0",

src/tasks/opportunity-status-processor/handler.js

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getOpportunityTitle,
2121
OPPORTUNITY_DEPENDENCY_MAP,
2222
getOpportunitiesForAudit,
23+
computeAuditCompletion,
2324
} from '@adobe/spacecat-shared-utils';
2425
import { getAuditStatus } from '../../utils/cloudwatch-utils.js';
2526
import { checkAndAlertBotProtection } from '../../utils/bot-detection.js';
@@ -217,54 +218,6 @@ async function isScrapingAvailable(baseUrl, context, onboardStartTime) {
217218
}
218219
}
219220

220-
/**
221-
* Checks which audit types have completed since onboardStartTime by querying the database.
222-
* An audit is considered completed if a record exists with auditedAt >= onboardStartTime.
223-
* Falls back conservatively (all pending) if the DB query fails.
224-
*
225-
* @param {string} siteId - The site ID
226-
* @param {Array<string>} auditTypes - Audit types expected for this onboard session
227-
* @param {number} onboardStartTime - Onboarding start timestamp in ms
228-
* @param {object} dataAccess - Data access object
229-
* @param {object} log - Logger
230-
* @returns {Promise<{pendingAuditTypes: Array<string>, completedAuditTypes: Array<string>}>}
231-
*/
232-
async function checkAuditCompletionFromDB(siteId, auditTypes, onboardStartTime, dataAccess, log) {
233-
const pendingAuditTypes = [];
234-
const completedAuditTypes = [];
235-
try {
236-
const { Audit } = dataAccess;
237-
const latestAudits = await Audit.allLatestForSite(siteId);
238-
const auditsByType = {};
239-
if (latestAudits) {
240-
for (const audit of latestAudits) {
241-
auditsByType[audit.getAuditType()] = audit;
242-
}
243-
}
244-
for (const auditType of auditTypes) {
245-
const audit = auditsByType[auditType];
246-
if (!audit) {
247-
pendingAuditTypes.push(auditType);
248-
} else {
249-
const auditedAt = new Date(audit.getAuditedAt()).getTime();
250-
if (Number.isNaN(auditedAt) || auditedAt < onboardStartTime) {
251-
// Unparseable timestamp or audit predates this onboard session — treat as pending
252-
pendingAuditTypes.push(auditType);
253-
} else {
254-
completedAuditTypes.push(auditType);
255-
}
256-
}
257-
}
258-
} catch (error) {
259-
log.warn(`Could not check audit completion from DB for site ${siteId}: ${error.message}`);
260-
// Conservative fallback: mark all as pending so disclaimer is always shown on error
261-
// Reset first to avoid duplicates if some types were already pushed before the error
262-
pendingAuditTypes.length = 0;
263-
pendingAuditTypes.push(...auditTypes.filter((t) => !completedAuditTypes.includes(t)));
264-
}
265-
return { pendingAuditTypes, completedAuditTypes };
266-
}
267-
268221
/**
269222
* Analyzes missing opportunities and determines the root cause
270223
* @param {Array<string>} missingOpportunities - Array of missing opportunity types
@@ -582,8 +535,16 @@ export async function runOpportunityStatusProcessor(message, context) {
582535
// Only meaningful when we have an onboardStartTime anchor to compare against.
583536
let pendingAuditTypes = [];
584537
if (auditTypes && auditTypes.length > 0 && onboardStartTime) {
585-
// eslint-disable-next-line max-len
586-
({ pendingAuditTypes } = await checkAuditCompletionFromDB(siteId, auditTypes, onboardStartTime, dataAccess, log));
538+
try {
539+
const { Audit } = dataAccess;
540+
const latestAudits = await Audit.allLatestForSite(siteId);
541+
const completion = computeAuditCompletion(auditTypes, onboardStartTime, latestAudits);
542+
pendingAuditTypes = completion.pendingAuditTypes;
543+
} catch (auditErr) {
544+
log.warn(`Could not check audit completion from DB for site ${siteId}: ${auditErr.message}`);
545+
// Conservative fallback: mark all as pending so disclaimer is always shown on error
546+
pendingAuditTypes = [...auditTypes];
547+
}
587548
}
588549

589550
// Process opportunities by type to avoid duplicates

0 commit comments

Comments
 (0)