101 lines
3.5 KiB
JavaScript
101 lines
3.5 KiB
JavaScript
const { logger } = require('../middleware/logger');
|
|
|
|
class UserStatusRepository {
|
|
constructor(unitOfWork) {
|
|
this.unitOfWork = unitOfWork;
|
|
}
|
|
|
|
async getStatusByUserId(userId) {
|
|
logger.info('UserStatusRepository.getStatusByUserId:start', { userId });
|
|
try {
|
|
const conn = this.unitOfWork.connection;
|
|
const [rows] = await conn.query('SELECT * FROM user_status WHERE user_id = ?', [userId]);
|
|
logger.info('UserStatusRepository.getStatusByUserId:success', { userId, found: rows.length > 0 });
|
|
return rows.length > 0 ? rows[0] : null;
|
|
} catch (error) {
|
|
logger.error('UserStatusRepository.getStatusByUserId:error', { userId, error: error.message });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async initializeUserStatus(userId, status = 'inactive') {
|
|
logger.info('UserStatusRepository.initializeUserStatus:start', { userId, status });
|
|
try {
|
|
const conn = this.unitOfWork.connection;
|
|
await conn.query(
|
|
`INSERT INTO user_status (user_id, status) VALUES (?, ?)
|
|
ON DUPLICATE KEY UPDATE status = VALUES(status)`,
|
|
[userId, status]
|
|
);
|
|
logger.info('UserStatusRepository.initializeUserStatus:success', { userId });
|
|
} catch (error) {
|
|
logger.error('UserStatusRepository.initializeUserStatus:error', { userId, error: error.message });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async updateEmailVerified(userId) {
|
|
logger.info('UserStatusRepository.updateEmailVerified:start', { userId });
|
|
try {
|
|
const conn = this.unitOfWork.connection;
|
|
await conn.query(
|
|
`UPDATE user_status
|
|
SET email_verified = TRUE, email_verified_at = CURRENT_TIMESTAMP
|
|
WHERE user_id = ?`,
|
|
[userId]
|
|
);
|
|
logger.info('UserStatusRepository.updateEmailVerified:success', { userId });
|
|
} catch (error) {
|
|
logger.error('UserStatusRepository.updateEmailVerified:error', { userId, error: error.message });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async markRegistrationComplete(userId) {
|
|
logger.info('UserStatusRepository.markRegistrationComplete:start', { userId });
|
|
try {
|
|
const conn = this.unitOfWork.connection;
|
|
await conn.query(
|
|
`UPDATE user_status
|
|
SET registration_completed = TRUE, status = 'active'
|
|
WHERE user_id = ?`,
|
|
[userId]
|
|
);
|
|
logger.info('UserStatusRepository.markRegistrationComplete:success', { userId });
|
|
} catch (error) {
|
|
logger.error('UserStatusRepository.markRegistrationComplete:error', { userId, error: error.message });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async setPendingIfComplete(userId) {
|
|
logger.info('UserStatusRepository.setPendingIfComplete:start', { userId });
|
|
try {
|
|
const status = await this.getStatusByUserId(userId);
|
|
if (
|
|
status &&
|
|
status.email_verified &&
|
|
status.profile_completed &&
|
|
status.documents_uploaded &&
|
|
status.contract_signed &&
|
|
status.status === 'inactive'
|
|
) {
|
|
const conn = this.unitOfWork.connection;
|
|
await conn.query(
|
|
`UPDATE user_status SET status = 'pending' WHERE user_id = ?`,
|
|
[userId]
|
|
);
|
|
logger.info('UserStatusRepository.setPendingIfComplete:status_set_pending', { userId });
|
|
}
|
|
logger.info('UserStatusRepository.setPendingIfComplete:success', { userId });
|
|
} catch (error) {
|
|
logger.error('UserStatusRepository.setPendingIfComplete:error', { userId, error: error.message });
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
// No changes required for progress calculation, as getStatusByUserId returns all needed fields.
|
|
|
|
module.exports = UserStatusRepository;
|