BACKEND DONE FOR ALL APPS

This commit is contained in:
andrei-mihnea-cerbu
2024-10-21 18:34:45 +03:00
parent 4157ca9e7d
commit ab1eaec413
349 changed files with 17199 additions and 14015 deletions
@@ -0,0 +1,136 @@
import { ParsedMessage } from '../message_handler';
import {userDatabase} from '../../db_managers/db';
import { OperationBase } from '../operations_base/operation_base';
import { OperationHandler } from '../operations_base/operation_handler';
export class UserOperations extends OperationBase {
public static readonly operationCodes = {
...OperationBase.operationCodes,
GET_USERS: 'GET_USERS',
CREATE_USER: 'CREATE_USER',
MODIFY_USER: 'MODIFY_USER',
DELETE_USER: 'DELETE_USER',
FIND_BY_ID: 'FIND_BY_ID',
FIND_BY_EMAIL: 'FIND_BY_EMAIL',
};
// Get all users
public static handleGetUsers(): ParsedMessage {
const users = userDatabase.getAllUsers();
console.log(users);
return {
operationCode: UserOperations.operationCodes.OK,
metaInfo: { users },
};
}
public static handleGetUserById(parsedMessage: ParsedMessage): ParsedMessage {
const { id } = parsedMessage.metaInfo || {};
if(!id){
return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: 'Id is required.' },
};
}
const user = userDatabase.findById(id);
return {
operationCode: user === null ? UserOperations.operationCodes.ERR : UserOperations.operationCodes.OK,
metaInfo: user === null ? { message: 'User not found.' } : { message: user }
}
}
public static handleGetUserByEmail(parsedMessage: ParsedMessage) : ParsedMessage{
const { email } = parsedMessage.metaInfo || {};
if(!email){
return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: 'Email is required.' },
};
}
const user = userDatabase.findByEmail(email);
const response = {
operationCode: user === null ? UserOperations.operationCodes.ERR : UserOperations.operationCodes.OK,
metaInfo: user === null ? { message: 'User not found.' } : user
}
console.log(response);
return response;
}
// Modify an existing user
public static handleModifyUser(parsedMessage: ParsedMessage): ParsedMessage {
const { id, name, email, password, departmentId, app_type } = parsedMessage.metaInfo || {};
if (!id || !name || !email || !password || !departmentId || !app_type) {
return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: 'Some information are missing.' },
};
}
const user = userDatabase.findById(id);
if (!user) return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: 'User not found in the system.' },
};
const listOfUsers = userDatabase.getAllUsers()
// Check if another user already exists with the same email or name (excluding the current user)
const existingUserByEmail = listOfUsers.find((existingUser) => existingUser.email === email && existingUser.id !== id);
const existingUserByName = listOfUsers.find((existingUser) => existingUser.name === name && existingUser.id !== id);
if (existingUserByEmail) {
return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: `Email ${email} is already in use by another user.` },
};
}
if (existingUserByName) {
return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: `Name ${name} is already in use by another user.` },
};
}
const isSuccess = userDatabase.modifyUser(id, name, email, password, departmentId, app_type);
return {
operationCode: isSuccess ? UserOperations.operationCodes.OK : UserOperations.operationCodes.ERR,
metaInfo: { message: isSuccess ? 'User modified successfully.' : 'Failed to modify user.' },
};
}
// Delete a user by ID
public static handleDeleteUser(parsedMessage: ParsedMessage): ParsedMessage {
const { id } = parsedMessage.metaInfo || {};
if (!id) {
return {
operationCode: UserOperations.operationCodes.ERR,
metaInfo: { message: 'User ID is required for deletion.' },
};
}
const isSuccess = userDatabase.deleteUser(id);
return {
operationCode: isSuccess ? UserOperations.operationCodes.OK : UserOperations.operationCodes.ERR,
metaInfo: { message: isSuccess ? 'User deleted successfully.' : 'Failed to delete user.' },
};
}
// Register user operations with the OperationHandler
public register(operationHandler: OperationHandler): void {
operationHandler.registerHandler(UserOperations.operationCodes.GET_USERS, UserOperations.handleGetUsers);
operationHandler.registerHandler(UserOperations.operationCodes.MODIFY_USER, UserOperations.handleModifyUser);
operationHandler.registerHandler(UserOperations.operationCodes.DELETE_USER, UserOperations.handleDeleteUser);
operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_ID, UserOperations.handleGetUserById);
operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_EMAIL, UserOperations.handleGetUserByEmail);
// Register common operations inherited from OperationBase
OperationBase.registerCommonOperations(operationHandler);
}
}