"activate user admin"
This commit is contained in:
@@ -141,32 +141,32 @@ router.get('/users/:userId',
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Search users including soft-deleted ones
|
// Search users including soft-deleted ones
|
||||||
// router.get('/users/search/:searchTerm',
|
router.get('/users/search/:searchTerm',
|
||||||
// adminRequired,
|
adminRequired,
|
||||||
// ValidationMiddleware.validateStringLength({ searchTerm: { min: 2, max: 100 } }),
|
ValidationMiddleware.validateStringLength({ searchTerm: { min: 2, max: 100 } }),
|
||||||
// async (req: Request, res: Response) => {
|
async (req: Request, res: Response) => {
|
||||||
// try {
|
try {
|
||||||
// const { searchTerm } = req.params;
|
const { searchTerm } = req.params;
|
||||||
// const includeDeleted = req.query.includeDeleted === 'true';
|
const includeDeleted = req.query.includeDeleted === 'true';
|
||||||
|
|
||||||
// logRequest('Admin search users endpoint accessed', req, res, { searchTerm, includeDeleted });
|
logRequest('Admin search users endpoint accessed', req, res, { searchTerm, includeDeleted });
|
||||||
|
|
||||||
// const users = includeDeleted
|
const users = includeDeleted
|
||||||
// ? await container.userRepository.searchIncludingDeleted(searchTerm)
|
? await container.userRepository.searchIncludingDeleted(searchTerm)
|
||||||
// : await container.userRepository.search(searchTerm);
|
: await container.userRepository.search(searchTerm);
|
||||||
|
|
||||||
// logRequest('Admin user search completed', req, res, {
|
logRequest('Admin user search completed', req, res, {
|
||||||
// searchTerm,
|
searchTerm,
|
||||||
// resultCount: Array.isArray(users) ? users.length : (users.totalCount || 0),
|
resultCount: Array.isArray(users) ? users.length : (users.totalCount || 0),
|
||||||
// includeDeleted
|
includeDeleted
|
||||||
// });
|
});
|
||||||
|
|
||||||
// res.json(users);
|
res.json(users);
|
||||||
// } catch (error) {
|
} catch (error) {
|
||||||
// logError('Admin search users endpoint error', error as Error, req, res);
|
logError('Admin search users endpoint error', error as Error, req, res);
|
||||||
// res.status(500).json({ error: 'Internal server error' });
|
res.status(500).json({ error: 'Internal server error' });
|
||||||
// }
|
}
|
||||||
// });
|
});
|
||||||
|
|
||||||
// Update any user (admin only)
|
// Update any user (admin only)
|
||||||
router.patch('/users/:userId',
|
router.patch('/users/:userId',
|
||||||
@@ -213,6 +213,32 @@ router.patch('/users/:userId',
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Activate user (admin only)
|
||||||
|
router.post('/users/:userId/activate',
|
||||||
|
adminRequired,
|
||||||
|
ValidationMiddleware.validateUUIDFormat(['userId']),
|
||||||
|
async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const targetUserId = req.params.userId;
|
||||||
|
const adminUserId = (req as any).user.userId;
|
||||||
|
|
||||||
|
logRequest('Admin activate user endpoint accessed', req, res, { adminUserId, targetUserId });
|
||||||
|
|
||||||
|
const result = await container.activateUserCommandHandler.execute({ id: targetUserId });
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: 'User not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
logAuth('User activated by admin', targetUserId, { adminUserId }, req, res);
|
||||||
|
res.json({ message: 'User activated successfully', user: result });
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logError('Admin activate user endpoint error', error as Error, req, res);
|
||||||
|
res.status(500).json({ error: 'Internal server error' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Deactivate user (admin only)
|
// Deactivate user (admin only)
|
||||||
router.post('/users/:userId/deactivate',
|
router.post('/users/:userId/deactivate',
|
||||||
adminRequired,
|
adminRequired,
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import { ProcessOrgAuthCallbackCommandHandler } from '../Organization/commands/P
|
|||||||
import { CreateContactCommandHandler } from '../Contact/commands/CreateContactCommandHandler';
|
import { CreateContactCommandHandler } from '../Contact/commands/CreateContactCommandHandler';
|
||||||
import { UpdateContactCommandHandler } from '../Contact/commands/UpdateContactCommandHandler';
|
import { UpdateContactCommandHandler } from '../Contact/commands/UpdateContactCommandHandler';
|
||||||
import { DeleteContactCommandHandler } from '../Contact/commands/DeleteContactCommandHandler';
|
import { DeleteContactCommandHandler } from '../Contact/commands/DeleteContactCommandHandler';
|
||||||
|
import { ActivateUserCommandHandler } from '../User/commands/ActivateUserCommandHandler';
|
||||||
|
|
||||||
// Query Handlers
|
// Query Handlers
|
||||||
import { GetUserByIdQueryHandler } from '../User/queries/GetUserByIdQueryHandler';
|
import { GetUserByIdQueryHandler } from '../User/queries/GetUserByIdQueryHandler';
|
||||||
@@ -121,6 +122,7 @@ export class DIContainer {
|
|||||||
private _updateContactCommandHandler: UpdateContactCommandHandler | null = null;
|
private _updateContactCommandHandler: UpdateContactCommandHandler | null = null;
|
||||||
private _deleteContactCommandHandler: DeleteContactCommandHandler | null = null;
|
private _deleteContactCommandHandler: DeleteContactCommandHandler | null = null;
|
||||||
private _generateBoardCommandHandler: GenerateBoardCommandHandler | null = null;
|
private _generateBoardCommandHandler: GenerateBoardCommandHandler | null = null;
|
||||||
|
private _activateUserCommandHandler: ActivateUserCommandHandler | null = null;
|
||||||
|
|
||||||
// Query Handlers
|
// Query Handlers
|
||||||
private _getUserByIdQueryHandler: GetUserByIdQueryHandler | null = null;
|
private _getUserByIdQueryHandler: GetUserByIdQueryHandler | null = null;
|
||||||
@@ -306,6 +308,13 @@ export class DIContainer {
|
|||||||
return this._deactivateUserCommandHandler;
|
return this._deactivateUserCommandHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get activateUserCommandHandler(): ActivateUserCommandHandler {
|
||||||
|
if (!this._activateUserCommandHandler) {
|
||||||
|
this._activateUserCommandHandler = new ActivateUserCommandHandler(this.userRepository);
|
||||||
|
}
|
||||||
|
return this._activateUserCommandHandler;
|
||||||
|
}
|
||||||
|
|
||||||
public get deleteUserCommandHandler(): DeleteUserCommandHandler {
|
public get deleteUserCommandHandler(): DeleteUserCommandHandler {
|
||||||
if (!this._deleteUserCommandHandler) {
|
if (!this._deleteUserCommandHandler) {
|
||||||
this._deleteUserCommandHandler = new DeleteUserCommandHandler(this.userRepository);
|
this._deleteUserCommandHandler = new DeleteUserCommandHandler(this.userRepository);
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export interface ActivateUserCommand {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { IUserRepository } from '../../../Domain/IRepository/IUserRepository';
|
||||||
|
import { ActivateUserCommand } from './ActivateUserCommand';
|
||||||
|
|
||||||
|
|
||||||
|
export class ActivateUserCommandHandler {
|
||||||
|
constructor(private readonly userRepo: IUserRepository) {}
|
||||||
|
|
||||||
|
async execute(cmd: ActivateUserCommand): Promise<boolean> {
|
||||||
|
await this.userRepo.activate(cmd.id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,4 +7,5 @@ export interface IUserRepository extends IPaginatedRepository<UserAggregate, { u
|
|||||||
findByEmail(email: string): Promise<UserAggregate | null>;
|
findByEmail(email: string): Promise<UserAggregate | null>;
|
||||||
findByToken(token: string): Promise<UserAggregate | null>;
|
findByToken(token: string): Promise<UserAggregate | null>;
|
||||||
deactivate(id: string): Promise<UserAggregate | null>;
|
deactivate(id: string): Promise<UserAggregate | null>;
|
||||||
|
activate(id: string): Promise<UserAggregate | null>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -345,5 +345,25 @@ export class UserRepository implements IUserRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async activate(id: string) {
|
||||||
|
const startTime = Date.now();
|
||||||
|
try {
|
||||||
|
await this.repo.update(id, { state: UserState.VERIFIED_REGULAR });
|
||||||
|
const result = await this.findById(id);
|
||||||
|
logDatabase('User activated successfully', `update(${id}, { state: VERIFIED_REGULAR })`, Date.now() - startTime, {
|
||||||
|
userId: id,
|
||||||
|
success: !!result
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
logError('UserRepository.activate error', error as Error);
|
||||||
|
// Handle invalid UUID format
|
||||||
|
if (error instanceof Error && error.message.includes('invalid input syntax for type uuid')) {
|
||||||
|
throw new Error('Invalid user ID format');
|
||||||
|
}
|
||||||
|
throw new Error('Failed to activate user in database');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user