import { Logger } from '@libs/types/'; import { SQSMessageWrapper } from '@p81-common/common-messaging'; import { ComputeSWGProfile, Events } from '@p81-common/compute-dtos'; import { UserSWGProfileService } from '@p81-common/swg-profile-compute'; import ComputeProfileContext from '@functions/swg-lambda-compute-profile/src/ComputeProfileContext'; import { ComputeConfig } from '@p81-common/swg-profile-compute/dist/types/config'; export default class ComputeProfileEventHandler { context: ComputeProfileContext; constructor(context: ComputeProfileContext) { this.context = context; } public async handler( message: SQSMessageWrapper, logger: Logger = this.context.logger, ) { try { if (!message.isValid) { return; } const msg = message.payload()!.body; const { tenantId, users } = msg; const childLogger = logger.child({ module: 'ComputeProfileEventHandler', tenantId, usersLength: users.length, eventName: Events.compute.ComputeSwgProfileEvent.eventName, }); childLogger.debug({ message: 'Compute SWG profile start' }); const isThreatPreventionFeatureFlagEnabled = await this.context.redisManager.hasFeatureFlag( tenantId, this.context.config.get('swgThreatPrevention'), ); const config: ComputeConfig = { ...this.context.config.get('sqsMessaging'), isThreatPreventionFeatureFlagEnabled, }; childLogger.debug({ message: 'Compute package config', config }); const userSWGProfileService = new UserSWGProfileService( this.context.db, this.context.eventsBusSDPProducer, childLogger, config, ); await userSWGProfileService.compute(tenantId, users); childLogger.debug({ message: 'Compute SWG profile done' }); } catch (err) { logger.error({ message: 'Compute SWG profile Error', err }); message.markForRetry(); } } }