Patch location
MODIFY
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/emsp/EMSPLocationsEndpoint.ts
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/emsp/EMSPLocationsEndpoint.ts
@@ -11,7 +11,6 @@ import Constants from '../../../../../utils/Constants';
import { HTTPError } from '../../../../../types/HTTPError';
import Logging from '../../../../../utils/Logging';
import LoggingHelper from '../../../../../utils/LoggingHelper';
-import OCPIClientFactory from '../../../../../client/ocpi/OCPIClientFactory';
import { OCPIConnector } from '../../../../../types/ocpi/OCPIConnector';
import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
import { OCPILocation } from '../../../../../types/ocpi/OCPILocation';
@@ -44,6 +43,77 @@ export default class EMSPLocationsEndpoint extends AbstractEndpoint {
}
private async patchLocationRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant, ocpiEndpoint: OCPIEndpoint): Promise<OCPIResponse> {
+ const urlSegment = req.path.substring(1).split('/');
+ urlSegment.shift();
+ const countryCode = urlSegment.shift();
+ const partyID = urlSegment.shift();
+ const locationID = urlSegment.shift();
+ const evseUID = urlSegment.shift();
+ const connectorID = urlSegment.shift();
+ if (!countryCode || !partyID || !locationID) {
+ throw new AppError({
+ action: ServerAction.OCPI_PUT_LOCATION,
+ module: MODULE_NAME, method: 'putLocationRequest',
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: 'Missing request parameters',
+ ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
+ });
+ }
+ const location = req.body as OCPILocation;
+ if (locationID !== location.id) {
+ throw new AppError({
+ action: ServerAction.OCPI_PUT_LOCATION,
+ module: MODULE_NAME, method: 'putLocationRequest',
+ errorCode: StatusCodes.NOT_FOUND,
+ message: `Location ID '${locationID}' mismatch in URL`,
+ ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
+ detailedMessages: { locationId: locationID, location }
+ });
+ }
+ if (evseUID) {
+ const foundEvse = location.evses.find((evse) => evse.uid === evseUID);
+ if (!foundEvse) {
+ throw new AppError({
+ action: ServerAction.OCPI_PUT_LOCATION,
+ module: MODULE_NAME, method: 'putLocationRequest',
+ errorCode: StatusCodes.NOT_FOUND,
+ message: `EVSE UID '${evseUID}' mismatch in URL`,
+ ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
+ detailedMessages: { evseUid: evseUID, location }
+ });
+ }
+ if (connectorID) {
+ const foundEvseConnector = foundEvse.connectors.find(
+ (evseConnector) => evseConnector.id === connectorID);
+ if (!foundEvseConnector) {
+ throw new AppError({
+ action: ServerAction.OCPI_PUT_LOCATION,
+ module: MODULE_NAME, method: 'putLocationRequest',
+ errorCode: StatusCodes.NOT_FOUND,
+ message: `Unknown Connector ID '${connectorID}' in EVSE UID '${evseUID}'`,
+ ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
+ detailedMessages: { connectorId: connectorID, evse: foundEvse, location }
+ });
+ }
+ // Update EVSE Connector
+ await this.updateConnector(tenant, foundEvse, foundEvseConnector, location);
+ } else {
+ // Update EVSE
+ await this.updateEvse(tenant, foundEvse, location);
+ }
+ } else {
+ // Get the Orgs
+ const company = await OCPIUtils.checkAndGetEMSPCompany(tenant, ocpiEndpoint);
+ const siteName = OCPIUtils.buildOperatorName(countryCode, partyID);
+ const sites = await SiteStorage.getSites(tenant, { companyIDs: [company.id], name: siteName }, Constants.DB_PARAMS_SINGLE_RECORD);
+ const foundSite = sites.result.find((existingSite) => existingSite.name === siteName);
+ // Update Location
+ await OCPIUtils.processEMSPLocation(tenant, location, company, foundSite, siteName);
+ }
+ return OCPIUtils.success();
+ }
+
+ private async putLocationRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant, ocpiEndpoint: OCPIEndpoint): Promise<OCPIResponse> {
const urlSegment = req.path.substring(1).split('/');
// Remove action
urlSegment.shift();
@@ -62,8 +132,6 @@ export default class EMSPLocationsEndpoint extends AbstractEndpoint {
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
});
}
- // Get the OCPI client
- const ocpiClient = await OCPIClientFactory.getOcpiClient(tenant, ocpiEndpoint);
// Handle Charging Station / Connector
if (evseUID) {
const chargingStation = await ChargingStationStorage.getChargingStationByOcpiLocationUid(
@@ -116,6 +184,7 @@ export default class EMSPLocationsEndpoint extends AbstractEndpoint {
const foundEvse = chargingStation.ocpiData.evses.find((evse) => evse.uid === evseUID);
if (!foundEvse) {
throw new AppError({
+ ...LoggingHelper.getChargingStationProperties(chargingStation),
action: ServerAction.OCPI_PATCH_LOCATION,
module: MODULE_NAME, method: 'patchLocationRequest',
errorCode: StatusCodes.NOT_FOUND,
@@ -125,99 +194,35 @@ export default class EMSPLocationsEndpoint extends AbstractEndpoint {
});
}
if (connectorID) {
- const evseConnector = req.body as OCPIConnector;
- // const foundEvseConnector = foundEvse.connectors.find((evseConnector) => evseConnector.id === connectorID);
- // if (!foundEvseConnector) {
- // throw new AppError({
- // action: ServerAction.OCPI_PATCH_LOCATION,
- // module: MODULE_NAME, method: 'patchLocationRequest',
- // errorCode: StatusCodes.NOT_FOUND,
- // message: `Unknown Connector ID '${connectorID}' in EVSE UID '${evseUID}'`,
- // ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
- // detailedMessages: { connectorId: connectorID, evse: foundEvse, location }
- // });
- // }
- // // Patch Connector
- // await this.patchEvseConnector(tenant, chargingStation, foundEvseConnector);
- } else {
- // Patch Evse
- const newEvse = req.body as OCPIEvse;
- await this.patchEvse(tenant, chargingStation, foundEvse, newEvse, location, chargingStation.site, chargingStation.siteArea);
- }
- // Handle Location
- } else {
- const location = req.body as OCPILocation;
- }
- return OCPIUtils.success();
- }
-
- private async putLocationRequest(req: Request, res: Response, next: NextFunction, tenant: Tenant, ocpiEndpoint: OCPIEndpoint): Promise<OCPIResponse> {
- const urlSegment = req.path.substring(1).split('/');
- urlSegment.shift();
- const countryCode = urlSegment.shift();
- const partyID = urlSegment.shift();
- const locationID = urlSegment.shift();
- const evseUID = urlSegment.shift();
- const connectorID = urlSegment.shift();
- if (!countryCode || !partyID || !locationID) {
- throw new AppError({
- action: ServerAction.OCPI_PUT_LOCATION,
- module: MODULE_NAME, method: 'putLocationRequest',
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'Missing request parameters',
- ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
- });
- }
- const location = req.body as OCPILocation;
- if (locationID !== location.id) {
- throw new AppError({
- action: ServerAction.OCPI_PUT_LOCATION,
- module: MODULE_NAME, method: 'putLocationRequest',
- errorCode: StatusCodes.NOT_FOUND,
- message: `Location ID '${locationID}' mismatch in URL`,
- ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
- detailedMessages: { locationId: locationID, location }
- });
- }
- if (evseUID) {
- const foundEvse = location.evses.find((evse) => evse.uid === evseUID);
- if (!foundEvse) {
- throw new AppError({
- action: ServerAction.OCPI_PUT_LOCATION,
- module: MODULE_NAME, method: 'putLocationRequest',
- errorCode: StatusCodes.NOT_FOUND,
- message: `EVSE UID '${evseUID}' mismatch in URL`,
- ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
- detailedMessages: { evseUid: evseUID, location }
- });
- }
- if (connectorID) {
- const foundEvseConnector = foundEvse.connectors.find(
- (evseConnector) => evseConnector.id === connectorID);
+ const newEvseConnector = req.body as OCPIConnector;
+ const foundEvseConnector = foundEvse.connectors.find((evseConnector) => evseConnector.id === connectorID);
if (!foundEvseConnector) {
throw new AppError({
- action: ServerAction.OCPI_PUT_LOCATION,
- module: MODULE_NAME, method: 'putLocationRequest',
+ ...LoggingHelper.getChargingStationProperties(chargingStation),
+ action: ServerAction.OCPI_PATCH_LOCATION,
+ module: MODULE_NAME, method: 'patchLocationRequest',
errorCode: StatusCodes.NOT_FOUND,
message: `Unknown Connector ID '${connectorID}' in EVSE UID '${evseUID}'`,
ocpiError: OCPIStatusCode.CODE_2003_UNKNOWN_LOCATION_ERROR,
detailedMessages: { connectorId: connectorID, evse: foundEvse, location }
});
}
- // Update EVSE Connector
- await this.updateConnector(tenant, foundEvse, foundEvseConnector, location);
+ // Patch Connector
+ // await this.patchEvseConnector(tenant, chargingStation, foundEvseConnector);
} else {
- // Update EVSE
- await this.updateEvse(tenant, foundEvse, location);
+ // Patch Evse
+ const newEvse = req.body as OCPIEvse;
+ await this.patchEvse(tenant, chargingStation, foundEvse, newEvse, location, chargingStation.site, chargingStation.siteArea);
}
+ // Handle Location
} else {
// Get the Orgs
const company = await OCPIUtils.checkAndGetEMSPCompany(tenant, ocpiEndpoint);
const siteName = OCPIUtils.buildOperatorName(countryCode, partyID);
const sites = await SiteStorage.getSites(tenant, { companyIDs: [company.id], name: siteName }, Constants.DB_PARAMS_SINGLE_RECORD);
- // Get the Site
const foundSite = sites.result.find((existingSite) => existingSite.name === siteName);
// Update Location
+ const location = req.body as OCPILocation;
await OCPIUtils.processEMSPLocation(tenant, location, company, foundSite, siteName);
}
return OCPIUtils.success();

Patch location