]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(ui-common): use ProcedureName enum instead of string literals in payload...
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 30 Apr 2026 13:43:01 +0000 (15:43 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 30 Apr 2026 13:43:01 +0000 (15:43 +0200)
Replace procedureName string literals with ProcedureName enum values
for type safety and consistency across builders, consumers and tests.

ui/cli/src/commands/transaction.ts
ui/common/src/utils/payloadBuilders.ts
ui/common/tests/payloadBuilders.test.ts
ui/web/src/core/UIClient.ts

index 43624e9f0215d039b6125e539684993925c9be94..d5bbb7f17ee9f5ebf4fdf097aceb6f4f4be73a61 100644 (file)
@@ -64,7 +64,7 @@ export const createTransactionCommands = (program: Command): Command => {
               { evseId: options.evseId, idTag: options.idTag }
             )
             procedureName =
-              proc === 'transactionEvent'
+              proc === ProcedureName.TRANSACTION_EVENT
                 ? ProcedureName.TRANSACTION_EVENT
                 : ProcedureName.START_TRANSACTION
             payload = {
@@ -129,7 +129,7 @@ export const createTransactionCommands = (program: Command): Command => {
               options.connectorId
             )
             procedureName =
-              proc === 'transactionEvent'
+              proc === ProcedureName.TRANSACTION_EVENT
                 ? ProcedureName.TRANSACTION_EVENT
                 : ProcedureName.STOP_TRANSACTION
             payload = {
index fbd43f93dc8385c72356f107142e9e0e717854de..c1c7b74af0421a2f3c9c1cb0f988b8cb8dce1ec1 100644 (file)
@@ -1,11 +1,10 @@
-import type { RequestPayload } from '../types/UIProtocol.js'
-
 import {
   OCPP20IdTokenEnumType,
   type OCPP20IdTokenType,
   OCPP20TransactionEventEnumType,
   OCPPVersion,
 } from '../types/ChargingStationType.js'
+import { ProcedureName, type RequestPayload } from '../types/UIProtocol.js'
 
 /**
  * Builds an Authorize request payload adapted to the station's OCPP version.
@@ -52,7 +51,10 @@ export function buildStartTransactionPayload (
   connectorId: number,
   ocppVersion: OCPPVersion | undefined,
   options?: { evseId?: number; idTag?: string }
-): { payload: RequestPayload; procedureName: 'startTransaction' | 'transactionEvent' } {
+): {
+    payload: RequestPayload
+    procedureName: ProcedureName.START_TRANSACTION | ProcedureName.TRANSACTION_EVENT
+  } {
   if (isOCPP20x(ocppVersion)) {
     return {
       payload: {
@@ -63,13 +65,13 @@ export function buildStartTransactionPayload (
           idToken: { idToken: options.idTag, type: OCPP20IdTokenEnumType.ISO14443 },
         }),
       },
-      procedureName: 'transactionEvent',
+      procedureName: ProcedureName.TRANSACTION_EVENT,
     }
   }
   assertOCPP16OrUndefined(ocppVersion)
   return {
     payload: { connectorId, ...(options?.idTag != null && { idTag: options.idTag }) },
-    procedureName: 'startTransaction',
+    procedureName: ProcedureName.START_TRANSACTION,
   }
 }
 
@@ -84,7 +86,10 @@ export function buildStopTransactionPayload (
   transactionId: number | string,
   ocppVersion: OCPPVersion | undefined,
   connectorId?: number
-): { payload: RequestPayload; procedureName: 'stopTransaction' | 'transactionEvent' } {
+): {
+    payload: RequestPayload
+    procedureName: ProcedureName.STOP_TRANSACTION | ProcedureName.TRANSACTION_EVENT
+  } {
   if (isOCPP20x(ocppVersion)) {
     return {
       payload: {
@@ -92,13 +97,13 @@ export function buildStopTransactionPayload (
         eventType: OCPP20TransactionEventEnumType.ENDED,
         transactionId: transactionId.toString(),
       },
-      procedureName: 'transactionEvent',
+      procedureName: ProcedureName.TRANSACTION_EVENT,
     }
   }
   assertOCPP16OrUndefined(ocppVersion)
   return {
     payload: { transactionId },
-    procedureName: 'stopTransaction',
+    procedureName: ProcedureName.STOP_TRANSACTION,
   }
 }
 
index bfed3a091f918f489b50cf9c6249b66a838487a5..1a36dfba4365373c6d4456e31d40328221c5042e 100644 (file)
@@ -6,6 +6,7 @@ import {
   OCPP20TransactionEventEnumType,
   OCPPVersion,
 } from '../src/types/ChargingStationType.js'
+import { ProcedureName } from '../src/types/UIProtocol.js'
 import {
   buildAuthorizePayload,
   buildIdToken,
@@ -71,7 +72,7 @@ await describe('payloadBuilders', async () => {
       const result = buildStartTransactionPayload(1, OCPPVersion.VERSION_16, { idTag: 'TAG1' })
       assert.deepStrictEqual(result, {
         payload: { connectorId: 1, idTag: 'TAG1' },
-        procedureName: 'startTransaction',
+        procedureName: ProcedureName.START_TRANSACTION,
       })
     })
 
@@ -83,7 +84,7 @@ await describe('payloadBuilders', async () => {
           eventType: OCPP20TransactionEventEnumType.STARTED,
           idToken: { idToken: 'TAG1', type: OCPP20IdTokenEnumType.ISO14443 },
         },
-        procedureName: 'transactionEvent',
+        procedureName: ProcedureName.TRANSACTION_EVENT,
       })
     })
 
@@ -99,7 +100,7 @@ await describe('payloadBuilders', async () => {
           evseId: 2,
           idToken: { idToken: 'TAG1', type: OCPP20IdTokenEnumType.ISO14443 },
         },
-        procedureName: 'transactionEvent',
+        procedureName: ProcedureName.TRANSACTION_EVENT,
       })
     })
 
@@ -110,7 +111,7 @@ await describe('payloadBuilders', async () => {
       })
       assert.deepStrictEqual(result, {
         payload: { connectorId: 1, idTag: 'TAG1' },
-        procedureName: 'startTransaction',
+        procedureName: ProcedureName.START_TRANSACTION,
       })
     })
 
@@ -121,7 +122,7 @@ await describe('payloadBuilders', async () => {
           connectorId: 1,
           eventType: OCPP20TransactionEventEnumType.STARTED,
         },
-        procedureName: 'transactionEvent',
+        procedureName: ProcedureName.TRANSACTION_EVENT,
       })
     })
   })
@@ -131,7 +132,7 @@ await describe('payloadBuilders', async () => {
       const result = buildStopTransactionPayload(12345, OCPPVersion.VERSION_16)
       assert.deepStrictEqual(result, {
         payload: { transactionId: 12345 },
-        procedureName: 'stopTransaction',
+        procedureName: ProcedureName.STOP_TRANSACTION,
       })
     })
 
@@ -143,7 +144,7 @@ await describe('payloadBuilders', async () => {
           eventType: OCPP20TransactionEventEnumType.ENDED,
           transactionId: 'uuid-123',
         },
-        procedureName: 'transactionEvent',
+        procedureName: ProcedureName.TRANSACTION_EVENT,
       })
     })
 
index b95b9ead6aa3c40924412d404bf567ee13909e9c..40b03214c390c47e2a60359639c694799e7442d5 100644 (file)
@@ -187,7 +187,7 @@ export class UIClient {
       options.ocppVersion,
       { evseId: options.evseId, idTag: options.idTag }
     )
-    if (procedureName === 'transactionEvent') {
+    if (procedureName === ProcedureName.TRANSACTION_EVENT) {
       return this.transactionEvent(hashId, payload)
     }
     return this.sendRequest(ProcedureName.START_TRANSACTION, {
@@ -251,7 +251,7 @@ export class UIClient {
       options.transactionId,
       options.ocppVersion
     )
-    if (procedureName === 'transactionEvent') {
+    if (procedureName === ProcedureName.TRANSACTION_EVENT) {
       return this.transactionEvent(hashId, payload)
     }
     return this.sendRequest(ProcedureName.STOP_TRANSACTION, {