86211923db
Repository Interface Optimization: - Created IBaseRepository.ts and IPaginatedRepository.ts - Refactored all 7 repository interfaces to extend base interfaces - Eliminated ~200 lines of redundant code (70% reduction) - Improved type safety and maintainability Dependency Injection Improvements: - Added EmailService and GameTokenService to DIContainer - Updated CreateUserCommandHandler constructor for DI - Updated RequestPasswordResetCommandHandler constructor for DI - Enhanced testability and service consistency Environment Configuration: - Created comprehensive .env.example with 40+ variables - Organized into 12 logical sections (Database, Security, Email, etc.) - Added security guidelines and best practices - Documented all backend environment requirements Documentation: - Added comprehensive codebase review - Created refactoring summary report - Added frontend implementation guide Impact: Improved code quality, reduced maintenance overhead, enhanced developer experience
124 lines
3.8 KiB
TypeScript
124 lines
3.8 KiB
TypeScript
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<ProcessOrgAuthCallbackResponse> {
|
|
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'
|
|
};
|
|
}
|
|
}
|
|
}
|