Factorized CP create/update code
MODIFY
src/assets/server/rest/v1/docs/e-mobility-oas.json
src/assets/server/rest/v1/docs/e-mobility-oas.json
"description": "Charging Profile ID",
"required": true,
"schema": {
- "type": "string"
+ "type": "string",
+ "pattern": "^[a-f0-9]{64}$"
}
},
"chargingStationID": {

MODIFY
src/assets/server/rest/v1/schemas/chargingstation/chargingprofile-delete.json
src/assets/server/rest/v1/schemas/chargingstation/chargingprofile-delete.json
"type": "object",
"properties": {
"ID": {
- "$ref": "common.json#/definitions/id"
+ "$ref": "chargingstation.json#/definitions/chargingProfile/properties/id"
}
},
"required": [

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -621,114 +621,20 @@ export default class ChargingStationService {
public static async handleCreateChargingProfile(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
const filteredRequest = ChargingStationValidator.getInstance().validateChargingProfileCreateReq(req.body);
- // Check existence
- const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, filteredRequest.chargingStationID);
- UtilsService.assertObjectExists(action, chargingStation, `Charging Station ID '${filteredRequest.chargingStationID}' does not exist.`,
- MODULE_NAME, 'handleCreateChargingProfile', req.user);
- // OCPI Charging Station
- if (!chargingStation.issuer) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.GENERAL_ERROR,
- message: `Charging Station '${chargingStation.id}' not issued by the organization`,
- module: MODULE_NAME, method: 'handleCreateChargingProfile',
- user: req.user,
- action: action
- });
- }
- const chargePoint = Utils.getChargePointFromID(chargingStation, filteredRequest.chargePointID);
- UtilsService.assertObjectExists(action, chargePoint, `Charge Point ID '${filteredRequest.chargePointID}' does not exist.`,
- MODULE_NAME, 'handleCreateChargingProfile', req.user);
- // Check Mandatory fields
- UtilsService.checkIfChargingProfileIsValid(chargingStation, chargePoint, filteredRequest, req);
- let siteID = null;
- if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
- // Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
- siteID = siteArea ? siteArea.siteID : null;
- }
- // Check Auth
- if (!await Authorizations.canUpdateChargingStation(req.user, siteID)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: req.user,
- action: Action.UPDATE, entity: Entity.CHARGING_STATION,
- module: MODULE_NAME, method: 'handleCreateChargingProfile',
- value: chargingStation.id
- });
- }
- // Check if Charging Profile is supported
- if (!chargingStation.capabilities?.supportChargingProfiles) {
- throw new AppError({
- source: chargingStation.id,
- action: action,
- errorCode: HTTPError.FEATURE_NOT_SUPPORTED_ERROR,
- user: req.user,
- module: MODULE_NAME, method: 'handleCreateChargingProfile',
- message: `Charging Station '${chargingStation.id}' does not support Charging Profiles`,
- });
- }
- // Apply & Save charging plan
- const chargingProfileID = await OCPPUtils.setAndSaveChargingProfile(req.user.tenantID, filteredRequest, req.user);
- // Ok
- res.status(StatusCodes.CREATED).json(Object.assign({ id: chargingProfileID }, Constants.REST_RESPONSE_SUCCESS));
+ const chargingProfileID = await ChargingStationService.setAndSaveChargingProfile(filteredRequest, action, req);
+ res.status(StatusCodes.CREATED).send(Object.assign({ id: chargingProfileID }, Constants.REST_RESPONSE_SUCCESS));
next();
}
public static async handleUpdateChargingProfile(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
const filteredRequest = ChargingStationValidator.getInstance().validateChargingProfileUpdateReq({ ...req.params, ...req.body });
- // Check existence
- const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, filteredRequest.chargingStationID);
- UtilsService.assertObjectExists(action, chargingStation, `Charging Station ID '${filteredRequest.chargingStationID}' does not exist.`,
- MODULE_NAME, 'handleUpdateChargingProfile', req.user);
- // OCPI Charging Station
- if (!chargingStation.issuer) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.GENERAL_ERROR,
- message: `Charging Station '${chargingStation.id}' not issued by the organization`,
- module: MODULE_NAME, method: 'handleUpdateChargingProfile',
- user: req.user,
- action: action
- });
- }
- const chargePoint = Utils.getChargePointFromID(chargingStation, filteredRequest.chargePointID);
- UtilsService.assertObjectExists(action, chargePoint, `Charge Point ID '${filteredRequest.chargePointID}' does not exist.`,
+ // Check for existing charging profile
+ const chargingProfile = await ChargingStationStorage.getChargingProfile(req.tenant.id, filteredRequest.id);
+ UtilsService.assertObjectExists(action, chargingProfile, `Charging Profile ID '${filteredRequest.id}' does not exist.`,
MODULE_NAME, 'handleUpdateChargingProfile', req.user);
- // Check Mandatory fields
- UtilsService.checkIfChargingProfileIsValid(chargingStation, chargePoint, filteredRequest, req);
- let siteID = null;
- if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
- // Get the Site Area
- const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
- siteID = siteArea ? siteArea.siteID : null;
- }
- // Check Auth
- if (!await Authorizations.canUpdateChargingStation(req.user, siteID)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: req.user,
- action: Action.UPDATE, entity: Entity.CHARGING_STATION,
- module: MODULE_NAME, method: 'handleUpdateChargingProfile',
- value: chargingStation.id
- });
- }
- // Check if Charging Profile is supported
- if (!chargingStation.capabilities?.supportChargingProfiles) {
- throw new AppError({
- source: chargingStation.id,
- action: action,
- errorCode: HTTPError.FEATURE_NOT_SUPPORTED_ERROR,
- user: req.user,
- module: MODULE_NAME, method: 'handleUpdateChargingProfile',
- message: `Charging Station '${chargingStation.id}' does not support Charging Profiles`,
- });
- }
- // Apply & Save charging plan
- const chargingProfileID = await OCPPUtils.setAndSaveChargingProfile(req.user.tenantID, filteredRequest, req.user);
- // Ok
- res.json(Object.assign({ id: chargingProfileID }, Constants.REST_RESPONSE_SUCCESS));
+ const chargingProfileID = await ChargingStationService.setAndSaveChargingProfile(filteredRequest, action, req);
+ res.send(Object.assign({ id: chargingProfileID }, Constants.REST_RESPONSE_SUCCESS));
next();
}
@@ -1872,4 +1778,58 @@ export default class ChargingStationService {
});
}
}
+
+ private static async setAndSaveChargingProfile(filteredRequest: ChargingProfile, action: ServerAction, req: Request): Promise<string> {
+ // Check existence
+ const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, filteredRequest.chargingStationID);
+ UtilsService.assertObjectExists(action, chargingStation, `Charging Station ID '${filteredRequest.chargingStationID}' does not exist.`,
+ MODULE_NAME, 'setAndSaveChargingProfile', req.user);
+ // OCPI Charging Station
+ if (!chargingStation.issuer) {
+ throw new AppError({
+ source: Constants.CENTRAL_SERVER,
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `Charging Station '${chargingStation.id}' not issued by the organization`,
+ module: MODULE_NAME, method: 'setAndSaveChargingProfile',
+ user: req.user,
+ action: action
+ });
+ }
+ const chargePoint = Utils.getChargePointFromID(chargingStation, filteredRequest.chargePointID);
+ UtilsService.assertObjectExists(action, chargePoint, `Charge Point ID '${filteredRequest.chargePointID}' does not exist.`,
+ MODULE_NAME, 'setAndSaveChargingProfile', req.user);
+ // Check Mandatory fields
+ UtilsService.checkIfChargingProfileIsValid(chargingStation, chargePoint, filteredRequest, req);
+ let siteID = null;
+ if (Utils.isComponentActiveFromToken(req.user, TenantComponents.ORGANIZATION)) {
+ // Get the Site Area
+ const siteArea = await SiteAreaStorage.getSiteArea(req.user.tenantID, chargingStation.siteAreaID);
+ siteID = siteArea ? siteArea.siteID : null;
+ }
+ // Check Auth
+ if (!await Authorizations.canUpdateChargingStation(req.user, siteID)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.FORBIDDEN,
+ user: req.user,
+ action: Action.UPDATE, entity: Entity.CHARGING_STATION,
+ module: MODULE_NAME, method: 'setAndSaveChargingProfile',
+ value: chargingStation.id
+ });
+ }
+ // Check if Charging Profile is supported
+ if (!chargingStation.capabilities?.supportChargingProfiles) {
+ throw new AppError({
+ source: chargingStation.id,
+ action: action,
+ errorCode: HTTPError.FEATURE_NOT_SUPPORTED_ERROR,
+ user: req.user,
+ module: MODULE_NAME, method: 'setAndSaveChargingProfile',
+ message: `Charging Station '${chargingStation.id}' does not support Charging Profiles`,
+ });
+ }
+ // Apply & Save charging plan
+ const chargingProfileID = await OCPPUtils.setAndSaveChargingProfile(req.user.tenantID, filteredRequest, req.user);
+ // Ok
+ return chargingProfileID;
+ }
}

Factorized CP create/update code