Skip to content

Commit 078895a

Browse files
fix api calls
1 parent 65513d7 commit 078895a

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/audit-status-processor/handler.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { Audit } from '@adobe/spacecat-shared-data-access';
1413
import { 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
*/
2423
export 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

src/demo-url-processor/handler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

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

15+
const TASK_TYPE = 'demo-url-processor';
16+
1517
/** Prepare demo url for the site */
1618
function prepareDemoUrl(experienceUrl, organizationId, siteId) {
1719
return `${experienceUrl}?organizationId=${organizationId}#/@aemrefdemoshared/sites-optimizer/sites/${siteId}/home`;
@@ -32,6 +34,7 @@ export async function runDemoUrlProcessor(message, context) {
3234
} = taskContext;
3335

3436
log.info('Processing demo url for site:', {
37+
taskType: TASK_TYPE,
3538
siteId,
3639
siteUrl,
3740
organizationId,

src/disable-import-audit-processor/handler.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import { Site } from '@adobe/spacecat-shared-data-access';
1413
import { say } from '../utils/slack-utils.js';
1514

16-
const AUDIT_TYPE = 'disable-import-audit-processor';
15+
const TASK_TYPE = 'disable-import-audit-processor';
1716

1817
/**
1918
* Runs the disable import and audit processor
@@ -26,13 +25,13 @@ export async function runDisableImportAuditProcessor(message, context) {
2625
const {
2726
siteId, siteUrl, organizationId, taskContext,
2827
} = message;
29-
const { Configuration } = dataAccess;
28+
const { Site, Configuration } = dataAccess;
3029
const {
3130
importTypes = [], auditTypes = [], slackContext,
3231
} = taskContext;
3332

3433
log.info('Processing disable import and audit request:', {
35-
auditType: AUDIT_TYPE,
34+
taskType: TASK_TYPE,
3635
siteId,
3736
organizationId,
3837
importTypes,

0 commit comments

Comments
 (0)