UNPKG

@citrineos/data

Version:

The OCPP data module which includes all persistence layer implementation.

143 lines 8.58 kB
"use strict"; // Copyright (c) 2023 S44, LLC // Copyright Contributors to the CitrineOS Project // // SPDX-License-Identifier: Apache 2.0 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SequelizeChargingProfileRepository = void 0; const base_1 = require("@citrineos/base"); const Base_1 = require("./Base"); const DeviceModel_1 = require("../model/DeviceModel"); const ChargingProfile_1 = require("../model/ChargingProfile"); const TransactionEvent_1 = require("../model/TransactionEvent"); class SequelizeChargingProfileRepository extends Base_1.SequelizeRepository { constructor(config, logger, sequelizeInstance, chargingNeeds, chargingSchedule, salesTariff, transaction, evse, compositeSchedule) { super(config, ChargingProfile_1.ChargingProfile.MODEL_NAME, logger, sequelizeInstance); this.chargingNeeds = chargingNeeds ? chargingNeeds : new Base_1.SequelizeRepository(config, ChargingProfile_1.ChargingNeeds.MODEL_NAME, logger, sequelizeInstance); this.chargingSchedule = chargingSchedule ? chargingSchedule : new Base_1.SequelizeRepository(config, ChargingProfile_1.ChargingSchedule.MODEL_NAME, logger, sequelizeInstance); this.evse = evse ? evse : new Base_1.SequelizeRepository(config, DeviceModel_1.Evse.MODEL_NAME, logger, sequelizeInstance); this.salesTariff = salesTariff ? salesTariff : new Base_1.SequelizeRepository(config, ChargingProfile_1.SalesTariff.MODEL_NAME, logger, sequelizeInstance); this.transaction = transaction ? transaction : new Base_1.SequelizeRepository(config, TransactionEvent_1.Transaction.MODEL_NAME, logger, sequelizeInstance); this.compositeSchedule = compositeSchedule ? compositeSchedule : new Base_1.SequelizeRepository(config, ChargingProfile_1.CompositeSchedule.MODEL_NAME, logger, sequelizeInstance); } createOrUpdateChargingProfile(chargingProfile, stationId, evseId, chargingLimitSource, isActive) { return __awaiter(this, void 0, void 0, function* () { let transactionDBId; if (chargingProfile.transactionId) { const activeTransaction = yield TransactionEvent_1.Transaction.findOne({ where: { stationId, transactionId: chargingProfile.transactionId, }, }); transactionDBId = activeTransaction === null || activeTransaction === void 0 ? void 0 : activeTransaction.id; } const [savedChargingProfile, profileCreated] = yield this.readOrCreateByQuery({ where: { stationId: stationId, id: chargingProfile.id, }, defaults: Object.assign(Object.assign({}, chargingProfile), { evseId: evseId, transactionDatabaseId: transactionDBId, chargingLimitSource: chargingLimitSource !== null && chargingLimitSource !== void 0 ? chargingLimitSource : base_1.OCPP2_0_1.ChargingLimitSourceEnumType.CSO, isActive: isActive === undefined ? false : isActive }), }); if (!profileCreated) { yield this.updateByKey(Object.assign(Object.assign({}, chargingProfile), { chargingSchedule: chargingProfile.chargingSchedule.map((s) => (Object.assign({}, s))), stationId: stationId, transactionDatabaseId: transactionDBId, evseId: evseId, chargingLimitSource: chargingLimitSource !== null && chargingLimitSource !== void 0 ? chargingLimitSource : base_1.OCPP2_0_1.ChargingLimitSourceEnumType.CSO, isActive: isActive === undefined ? false : isActive }), savedChargingProfile.databaseId.toString()); // delete existed charging schedules and sales tariff const deletedChargingSchedules = yield this.chargingSchedule.deleteAllByQuery({ where: { chargingProfileDatabaseId: savedChargingProfile.databaseId, }, }); for (const deletedSchedule of deletedChargingSchedules) { yield this.salesTariff.deleteAllByQuery({ where: { chargingScheduleDatabaseId: deletedSchedule.databaseId, }, }); } } for (const chargingSchedule of chargingProfile.chargingSchedule) { const savedChargingSchedule = yield this.chargingSchedule.create(ChargingProfile_1.ChargingSchedule.build(Object.assign({ stationId, chargingProfileDatabaseId: savedChargingProfile.databaseId }, chargingSchedule))); if (chargingSchedule.salesTariff) { yield this.salesTariff.create(ChargingProfile_1.SalesTariff.build(Object.assign({ chargingScheduleDatabaseId: savedChargingSchedule.databaseId }, chargingSchedule.salesTariff))); } } return savedChargingProfile; }); } createChargingNeeds(chargingNeedsReq, stationId) { return __awaiter(this, void 0, void 0, function* () { const activeTransaction = yield TransactionEvent_1.Transaction.findOne({ where: { stationId, isActive: true, }, include: [{ model: DeviceModel_1.Evse, where: { id: chargingNeedsReq.evseId }, required: true }], // required: true ensures the inner join }); if (!activeTransaction) { throw new Error(`No active transaction found on station ${stationId} evse ${chargingNeedsReq.evseId}`); } return yield this.chargingNeeds.create(ChargingProfile_1.ChargingNeeds.build(Object.assign(Object.assign({}, chargingNeedsReq.chargingNeeds), { evseDatabaseId: activeTransaction.evseDatabaseId, transactionDatabaseId: activeTransaction.id, maxScheduleTuples: chargingNeedsReq.maxScheduleTuples }))); }); } findChargingNeedsByEvseDBIdAndTransactionDBId(evseDBId, transactionDataBaseId) { return __awaiter(this, void 0, void 0, function* () { const chargingNeedsArray = yield this.chargingNeeds.readAllByQuery({ where: { evseDatabaseId: evseDBId, transactionDatabaseId: transactionDataBaseId, }, order: [['createdAt', 'DESC']], }); return chargingNeedsArray.length > 0 ? chargingNeedsArray[0] : undefined; }); } createCompositeSchedule(compositeSchedule, stationId) { return __awaiter(this, void 0, void 0, function* () { return yield this.compositeSchedule.create(ChargingProfile_1.CompositeSchedule.build(Object.assign(Object.assign({}, compositeSchedule), { stationId }))); }); } getNextChargingScheduleId(stationId) { return __awaiter(this, void 0, void 0, function* () { return yield this.chargingSchedule.readNextValue('id', { where: { stationId } }); }); } getNextChargingProfileId(stationId) { return __awaiter(this, void 0, void 0, function* () { return yield this.readNextValue('id', { where: { stationId } }); }); } getNextStackLevel(stationId, transactionDatabaseId, profilePurpose) { return __awaiter(this, void 0, void 0, function* () { return yield this.readNextValue('stackLevel', { where: { stationId, transactionDatabaseId: transactionDatabaseId, chargingProfilePurpose: profilePurpose, }, }, 0); }); } } exports.SequelizeChargingProfileRepository = SequelizeChargingProfileRepository; //# sourceMappingURL=ChargingProfile.js.map