Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/api/src/task-management/task-management.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ export class TaskManagementService {
`Created task item: ${taskItem.id} for organization ${organizationId} by ${member.id}`,
);

// Log task creation in audit log
void this.auditService.logTaskItemCreated({
// Log task creation in audit log first (await to ensure it's created before assignment log)
await this.auditService.logTaskItemCreated({
taskItemId: taskItem.id,
organizationId,
userId: authContext.userId,
Expand All @@ -325,9 +325,9 @@ export class TaskManagementService {
assignedByUserId: authContext.userId,
});

// Log initial assignment in audit log
// Log initial assignment in audit log (after creation log to ensure correct order)
if (taskItem.assignee) {
void this.auditService.logTaskItemAssigned({
await this.auditService.logTaskItemAssigned({
taskItemId: taskItem.id,
organizationId,
userId: authContext.userId,
Expand Down
113 changes: 0 additions & 113 deletions apps/api/src/trigger/vendor/backfill-vendor-risk-assessment-tasks.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { db } from '@db';
import { logger, schedules } from '@trigger.dev/sdk';
import { vendorRiskAssessmentTask } from './vendor-risk-assessment-task';

/**
* Monthly scheduled task that refreshes risk assessments for all vendors.
* Runs on the 1st of each month at 2:00 AM UTC.
*/
export const vendorRiskAssessmentMonthlySchedule = schedules.task({
id: 'vendor-risk-assessment-monthly-schedule',
cron: '0 2 1 * *', // 1st of each month at 2:00 AM UTC
maxDuration: 1000 * 60 * 60, // 1 hour (for batch processing)
run: async (payload) => {
logger.info('Monthly vendor risk assessment refresh started', {
scheduledAt: payload.timestamp,
lastRun: payload.lastTimestamp,
});

// Find all vendors across all organizations that have websites
const vendors = await db.vendor.findMany({
where: {
website: {
not: null,
},
},
select: {
id: true,
name: true,
website: true,
organizationId: true,
},
});

logger.info(`Found ${vendors.length} unique vendors with websites`);

if (vendors.length === 0) {
return {
success: true,
totalVendors: 0,
triggered: 0,
message: 'No vendors with websites found',
};
}

// Process ALL vendors - monthly refresh for everyone
// This ensures all vendors get updated risk assessments monthly
logger.info(`Processing all ${vendors.length} vendors for monthly refresh`);

// Batch trigger risk assessment tasks with research enabled for ALL vendors
// This will:
// - Create new assessments for vendors without data (v1)
// - Refresh existing assessments and increment version (v1 -> v2, v2 -> v3, etc.)
const batch = vendors.map((vendor) => ({
payload: {
vendorId: vendor.id,
vendorName: vendor.name,
vendorWebsite: vendor.website!,
organizationId: vendor.organizationId,
createdByUserId: null, // System-initiated
withResearch: true, // Always do research for monthly refresh
},
}));

try {
await vendorRiskAssessmentTask.batchTrigger(batch);
logger.info(`Triggered ${batch.length} vendor risk assessment tasks`, {
totalVendors: vendors.length,
triggered: batch.length,
});

return {
success: true,
totalVendors: vendors.length,
triggered: batch.length,
message: `Triggered monthly refresh for ${batch.length} vendors`,
};
} catch (error) {
logger.error('Failed to trigger batch risk assessment tasks', {
error: error instanceof Error ? error.message : String(error),
batchSize: batch.length,
});

return {
success: false,
totalVendors: vendors.length,
triggered: 0,
error: error instanceof Error ? error.message : String(error),
};
}
},
});

Loading