From 116676db77df79d1a29718e9179633d47302b84e Mon Sep 17 00:00:00 2001 From: andrei-mihnea-cerbu Date: Wed, 13 Nov 2024 13:50:18 +0200 Subject: [PATCH] network chunk v13 --- .../operations_custom/general_operations.ts | 1 - .../operations_custom/key_operations.ts | 18 +++++++ .../operations_custom/user_operations.ts | 50 ++++++++++++++++--- UC/src/udp_server.ts | 2 +- .../operations_custom/general_operations.ts | 1 - 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/UC/src/network/operations_custom/general_operations.ts b/UC/src/network/operations_custom/general_operations.ts index fd1e5f7..4bdee66 100644 --- a/UC/src/network/operations_custom/general_operations.ts +++ b/UC/src/network/operations_custom/general_operations.ts @@ -7,7 +7,6 @@ export class GeneralOperations implements OperationPlugin { public static readonly operationCodes = { OK: 'OK', ERR: 'ERR', - END: 'END', HEARTBEAT: 'HEARTBEAT', ALIVE: 'ALIVE', SET_PUBLIC_KEY: 'SET_PUBLIC_KEY', diff --git a/UC/src/network/operations_custom/key_operations.ts b/UC/src/network/operations_custom/key_operations.ts index 42a4725..4f4fcb3 100644 --- a/UC/src/network/operations_custom/key_operations.ts +++ b/UC/src/network/operations_custom/key_operations.ts @@ -31,9 +31,27 @@ export class KeyOperations implements OperationPlugin { : { operationCode: operationCodes.ERR, metaInfo: { message: 'Key not found.' } }; } + public static async handleDeleteKey(parsedMessage: ParsedMessage): Promise { + const { userId } = parsedMessage.metaInfo || {}; + + if (!userId) { + return { + operationCode: operationCodes.ERR, + metaInfo: { message: 'User ID is required for deletion.' }, + }; + } + + const isSuccess = keyDatabase.deleteKeysByUserId(userId); + return { + operationCode: isSuccess ? operationCodes.OK : operationCodes.ERR, + metaInfo: { message: isSuccess ? 'Key deleted successfully.' : 'Failed to delete key.' }, + }; + } + public register(operationHandler: OperationHandler): void { operationHandler.registerHandler(KeyOperations.operationCodes.GET_KEYS, KeyOperations.handleGetKeys); operationHandler.registerHandler(KeyOperations.operationCodes.CREATE_KEY, KeyOperations.handleCreateKey); operationHandler.registerHandler(KeyOperations.operationCodes.FIND_KEY_BY_USER_ID, KeyOperations.handleFindKeyByUserId); + operationHandler.registerHandler(KeyOperations.operationCodes.DELETE_KEY, KeyOperations.handleDeleteKey); } } diff --git a/UC/src/network/operations_custom/user_operations.ts b/UC/src/network/operations_custom/user_operations.ts index 331883d..a08fbd9 100644 --- a/UC/src/network/operations_custom/user_operations.ts +++ b/UC/src/network/operations_custom/user_operations.ts @@ -1,15 +1,15 @@ -import { ParsedMessage } from '../message_handler'; -import { userDatabase } from '../../db_managers/db'; -import { OperationHandler } from '../operations_base/operation_handler'; -import { OperationPlugin } from '../operations_base/operation_plugin'; -import { operationCodes} from "../operation_codes"; +import {ParsedMessage} from '../message_handler'; +import {userDatabase} from '../../db_managers/db'; +import {OperationHandler} from '../operations_base/operation_handler'; +import {OperationPlugin} from '../operations_base/operation_plugin'; +import {operationCodes} from "../operation_codes"; export class UserOperations implements OperationPlugin { public static readonly operationCodes = { - GET_USERS: 'GET_USERS', CREATE_USER: 'CREATE_USER', MODIFY_USER: 'MODIFY_USER', DELETE_USER: 'DELETE_USER', + GET_USERS: 'GET_USERS', FIND_BY_ID: 'FIND_BY_ID', FIND_BY_EMAIL: 'FIND_BY_EMAIL', }; @@ -35,9 +35,45 @@ export class UserOperations implements OperationPlugin { : { operationCode: operationCodes.ERR, metaInfo: { message: 'Failed to modify user.' } }; } + // Delete a user by ID + public static async handleDeleteUser(parsedMessage: ParsedMessage): Promise { + const { id } = parsedMessage.metaInfo || {}; + + if (!id) { + return { + operationCode: operationCodes.ERR, + metaInfo: { message: 'User ID is required for deletion.' }, + }; + } + + const isSuccess = userDatabase.deleteUser(id); + return { + operationCode: isSuccess ? operationCodes.OK : operationCodes.ERR, + metaInfo: { message: isSuccess ? 'User deleted successfully.' : 'Failed to delete user.' }, + }; + } + + public static async handleGetUserByEmail(parsedMessage: ParsedMessage) : Promise{ + const { email } = parsedMessage.metaInfo || {}; + if(!email){ + return { + operationCode: operationCodes.ERR, + metaInfo: { message: 'Email is required.' }, + }; + } + + const user = userDatabase.findByEmail(email); + return { + operationCode: user === null ? operationCodes.ERR : operationCodes.OK, + metaInfo: user === null ? {message: 'User not found.'} : user + }; + } + 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.GET_USERS, UserOperations.handleGetUsers); operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_ID, UserOperations.handleGetUserById); + operationHandler.registerHandler(UserOperations.operationCodes.FIND_BY_EMAIL, UserOperations.handleGetUserByEmail); } } diff --git a/UC/src/udp_server.ts b/UC/src/udp_server.ts index 8ea867b..a7f19cb 100644 --- a/UC/src/udp_server.ts +++ b/UC/src/udp_server.ts @@ -50,7 +50,7 @@ export class UdpServer { const communicator = new UdpSocketCommunicator(this.udpServer, ip, port, this.operationHandler); - communicator.handleIncomingMessage(msg.toString()); + await communicator.handleIncomingMessage(msg.toString()); const communicatorResult = communicator.getHandlerResult(); if (communicatorResult) { diff --git a/User/src/network/operations_custom/general_operations.ts b/User/src/network/operations_custom/general_operations.ts index fd1e5f7..4bdee66 100644 --- a/User/src/network/operations_custom/general_operations.ts +++ b/User/src/network/operations_custom/general_operations.ts @@ -7,7 +7,6 @@ export class GeneralOperations implements OperationPlugin { public static readonly operationCodes = { OK: 'OK', ERR: 'ERR', - END: 'END', HEARTBEAT: 'HEARTBEAT', ALIVE: 'ALIVE', SET_PUBLIC_KEY: 'SET_PUBLIC_KEY',