import { IUserRepository } from '../../../Domain/IRepository/IUserRepository'; import { IOrganizationRepository } from '../../../Domain/IRepository/IOrganizationRepository'; import { ProcessOrgAuthCallbackCommand } from './ProcessOrgAuthCallbackCommand'; import { logAuth, logDatabase, logError, logWarning } from '../../Services/Logger'; export interface ProcessOrgAuthCallbackResponse { success: boolean; message: string; updatedFields?: string[]; } export class ProcessOrgAuthCallbackCommandHandler { constructor( private readonly userRepo: IUserRepository, private readonly orgRepo: IOrganizationRepository ) {} async execute(cmd: ProcessOrgAuthCallbackCommand): Promise { const startTime = Date.now(); try { logAuth('Processing organization authentication callback', cmd.userId, { organizationId: cmd.organizationId, status: cmd.status, hasAuthToken: !!cmd.authToken }); // Verify organization exists const organization = await this.orgRepo.findById(cmd.organizationId); if (!organization) { logWarning('Organization not found for auth callback', { organizationId: cmd.organizationId, userId: cmd.userId }); return { success: false, message: 'Organization not found' }; } // Verify user exists const user = await this.userRepo.findById(cmd.userId); if (!user) { logWarning('User not found for auth callback', { organizationId: cmd.organizationId, userId: cmd.userId }); return { success: false, message: 'User not found' }; } // Verify user belongs to the organization if (user.orgid !== cmd.organizationId) { logWarning('User does not belong to organization for auth callback', { organizationId: cmd.organizationId, userId: cmd.userId, userOrgId: user.orgid }); return { success: false, message: 'User does not belong to this organization' }; } if (cmd.status === 'not_ok') { logAuth('Organization authentication failed', cmd.userId, { organizationId: cmd.organizationId, organizationName: organization.name }); return { success: false, message: 'Organization authentication failed' }; } // Update user's organization login date const now = new Date(); const updatedUser = await this.userRepo.update(cmd.userId, { Orglogindate: now }); if (!updatedUser) { logError('Failed to update user organization login date', new Error('User update returned null')); return { success: false, message: 'Failed to update user login information' }; } logAuth('Organization authentication successful', cmd.userId, { organizationId: cmd.organizationId, organizationName: organization.name, orgLoginDate: now.toISOString(), executionTime: Date.now() - startTime }); logDatabase('User organization login date updated', `userId: ${cmd.userId}, orgId: ${cmd.organizationId}`, Date.now() - startTime, { userId: cmd.userId, organizationId: cmd.organizationId, newOrgLoginDate: now.toISOString() } ); return { success: true, message: 'Organization authentication successful', updatedFields: ['Orglogindate'] }; } catch (error) { logError('ProcessOrgAuthCallbackCommandHandler error', error as Error); return { success: false, message: 'Internal error processing authentication callback' }; } } }