fix: fix worker options argument passing to worker pool/set
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIHttpServer.ts
index 546c7642de181d33de56e8f4ea9e214515a2220e..159559df1f4cacb6eebaa1f67b8e6cca66ac030a 100644 (file)
@@ -1,7 +1,9 @@
-import type { IncomingMessage, RequestListener, ServerResponse } from 'http';
+import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http';
 
 import { StatusCodes } from 'http-status-codes';
 
+import { AbstractUIServer } from './AbstractUIServer';
+import { UIServerUtils } from './UIServerUtils';
 import { BaseError } from '../../exception';
 import {
   type ProcedureName,
@@ -13,12 +15,17 @@ import {
   ResponseStatus,
   type UIServerConfiguration,
 } from '../../types';
-import { logger } from '../../utils/Logger';
-import { Utils } from '../../utils/Utils';
-import { AbstractUIServer, UIServerUtils } from '../internal';
+import { Constants, Utils, logger } from '../../utils';
 
 const moduleName = 'UIHttpServer';
 
+enum HttpMethods {
+  GET = 'GET',
+  PUT = 'PUT',
+  POST = 'POST',
+  PATCH = 'PATCH',
+}
+
 export class UIHttpServer extends AbstractUIServer {
   public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
     super(uiServerConfiguration);
@@ -37,7 +44,7 @@ export class UIHttpServer extends AbstractUIServer {
   public sendResponse(response: ProtocolResponse): void {
     const [uuid, payload] = response;
     try {
-      if (this.responseHandlers.has(uuid) === true) {
+      if (this.hasResponseHandler(uuid) === true) {
         const res = this.responseHandlers.get(uuid) as ServerResponse;
         res
           .writeHead(this.responseStatusToStatusCode(payload.status), {
@@ -101,7 +108,7 @@ export class UIHttpServer extends AbstractUIServer {
           error
         );
       });
-      if (req.method === 'POST') {
+      if (req.method === HttpMethods.POST) {
         const bodyBuffer = [];
         req
           .on('data', (chunk) => {
@@ -111,10 +118,19 @@ export class UIHttpServer extends AbstractUIServer {
             const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
             this.uiServices
               .get(version)
-              ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
-              .catch(() => {
-                /* Error caught by AbstractUIService */
-              });
+              ?.requestHandler(
+                this.buildProtocolRequest(
+                  uuid,
+                  procedureName,
+                  body ?? Constants.EMPTY_FREEZED_OBJECT
+                )
+              )
+              .then((protocolResponse: ProtocolResponse) => {
+                if (!Utils.isNullOrUndefined(protocolResponse)) {
+                  this.sendResponse(protocolResponse);
+                }
+              })
+              .catch(Constants.EMPTY_FUNCTION);
           });
       } else {
         throw new BaseError(`Unsupported HTTP method: '${req.method}'`);