CentralBackend/services/UserStatusService.js
2025-09-07 12:44:01 +02:00

89 lines
3.4 KiB
JavaScript

const UserStatusRepository = require('../repositories/UserStatusRepository');
const { logger } = require('../middleware/logger');
class UserStatusService {
static async initializeUserStatus(userId, userType, unitOfWork, status = 'inactive') {
logger.info('UserStatusService.initializeUserStatus:start', { userId, userType, status });
try {
const repo = new UserStatusRepository(unitOfWork);
await repo.initializeUserStatus(userId, status);
logger.info('UserStatusService.initializeUserStatus:success', { userId });
} catch (error) {
logger.error('UserStatusService.initializeUserStatus:error', { userId, error: error.message });
throw error;
}
}
static async updateEmailVerified(userId, unitOfWork) {
logger.info('UserStatusService.updateEmailVerified:start', { userId });
try {
const repo = new UserStatusRepository(unitOfWork);
await repo.updateEmailVerified(userId);
logger.info('UserStatusService.updateEmailVerified:success', { userId });
} catch (error) {
logger.error('UserStatusService.updateEmailVerified:error', { userId, error: error.message });
throw error;
}
}
static async markRegistrationComplete(userId, unitOfWork) {
logger.info('UserStatusService.markRegistrationComplete:start', { userId });
try {
const repo = new UserStatusRepository(unitOfWork);
await repo.markRegistrationComplete(userId);
logger.info('UserStatusService.markRegistrationComplete:success', { userId });
} catch (error) {
logger.error('UserStatusService.markRegistrationComplete:error', { userId, error: error.message });
throw error;
}
}
static async checkAndSetPendingIfComplete(userId, unitOfWork) {
logger.info('UserStatusService.checkAndSetPendingIfComplete:start', { userId });
try {
const repo = new UserStatusRepository(unitOfWork);
// Only set to 'pending' if all quickactions are complete and status is 'inactive'
await repo.setPendingIfComplete(userId);
logger.info('UserStatusService.checkAndSetPendingIfComplete:success', { userId });
} catch (error) {
logger.error('UserStatusService.checkAndSetPendingIfComplete:error', { userId, error: error.message });
throw error;
}
}
static async getStatusProgress(userId, unitOfWork) {
logger.info('UserStatusService.getStatusProgress:start', { userId });
try {
const repo = new UserStatusRepository(unitOfWork);
const status = await repo.getStatusByUserId(userId);
if (!status) return null;
// Calculate progress steps
const steps = [
{ key: 'email_verified', label: 'Email Verified' },
{ key: 'profile_completed', label: 'Profile Completed' },
{ key: 'documents_uploaded', label: 'Documents Uploaded' },
{ key: 'contract_signed', label: 'Contract Signed' }
];
const completedSteps = steps.filter(s => !!status[s.key]);
const progressPercent = Math.round((completedSteps.length / steps.length) * 100);
return {
status: status.status,
steps: steps.map(s => ({
key: s.key,
label: s.label,
completed: !!status[s.key]
})),
completedSteps: completedSteps.map(s => s.label),
progressPercent
};
} catch (error) {
logger.error('UserStatusService.getStatusProgress:error', { userId, error: error.message });
throw error;
}
}
}
module.exports = UserStatusService;