@citrineos/data
Version:
The OCPP data module which includes all persistence layer implementation.
229 lines • 13.9 kB
JavaScript
;
// 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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequelizeLocalAuthListRepository = void 0;
const base_1 = require("@citrineos/base");
const Authorization_1 = require("../model/Authorization");
const Base_1 = require("./Base");
const LocalListVersionAuthorization_1 = require("../model/Authorization/LocalListVersionAuthorization");
const SendLocalListAuthorization_1 = require("../model/Authorization/SendLocalListAuthorization");
const Authorization_2 = require("./Authorization");
class SequelizeLocalAuthListRepository extends Base_1.SequelizeRepository {
constructor(config, logger, sequelizeInstance, authorization, localListAuthorization, sendLocalList) {
super(config, Authorization_1.Authorization.MODEL_NAME, logger, sequelizeInstance);
this.authorization = authorization
? authorization
: new Authorization_2.SequelizeAuthorizationRepository(config, logger);
this.localListAuthorization = localListAuthorization
? localListAuthorization
: new Base_1.SequelizeRepository(config, Authorization_1.LocalListAuthorization.MODEL_NAME, logger, sequelizeInstance);
this.sendLocalList = sendLocalList
? sendLocalList
: new Base_1.SequelizeRepository(config, Authorization_1.SendLocalList.MODEL_NAME, logger, sequelizeInstance);
}
createSendLocalListFromRequestData(tenantId, stationId, correlationId, updateType, versionNumber, localAuthorizationList) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const sendLocalList = yield this.sendLocalList.create(tenantId, Authorization_1.SendLocalList.build({
tenantId,
stationId,
correlationId,
versionNumber,
updateType,
}));
for (const authData of localAuthorizationList !== null && localAuthorizationList !== void 0 ? localAuthorizationList : []) {
const auth = yield Authorization_1.Authorization.findOne({
include: [
{
model: Authorization_1.IdToken,
where: {
idToken: authData.idToken.idToken,
type: authData.idToken.type,
},
},
{
model: Authorization_1.IdTokenInfo,
include: [Authorization_1.IdToken],
},
],
});
if (!auth) {
throw new Error(`Authorization not found for ${JSON.stringify(authData)}, invalid SendLocalListRequest (create necessary Authorizations first)`);
}
if (!(0, base_1.deepDirectionalEqual)(authData.idToken, auth.idToken)) {
throw new Error(`Authorization idToken in SendLocalListRequest ${JSON.stringify(authData.idToken)} does not match idToken in database ${JSON.stringify(auth.idToken)} (update the idToken first)`);
}
if (((_a = authData.idTokenInfo) === null || _a === void 0 ? void 0 : _a.groupIdToken) &&
(!((_b = auth.idTokenInfo) === null || _b === void 0 ? void 0 : _b.groupIdToken) ||
!(0, base_1.deepDirectionalEqual)(authData.idTokenInfo.groupIdToken, auth.idTokenInfo.groupIdToken))) {
throw new Error(`Authorization group idToken in SendLocalListRequest ${JSON.stringify(authData.idTokenInfo.groupIdToken)} does not match group idToken in database ${JSON.stringify((_c = auth.idTokenInfo) === null || _c === void 0 ? void 0 : _c.groupIdToken)} (update the group idToken first)`);
}
// While new IdTokens will NOT be created or newly associated via message api, idTokenInfo can be allowed to be unique for the local auth list
const localListAuthIdTokenInfo = yield Authorization_1.IdTokenInfo.create(Object.assign(Object.assign({ tenantId }, authData.idTokenInfo), { groupIdTokenId: (_d = auth.idTokenInfo) === null || _d === void 0 ? void 0 : _d.groupIdTokenId }));
const { id, idTokenInfoId: _idTokenInfoId, idTokenInfo: _idTokenInfo } = auth, authorizationFields = __rest(auth, ["id", "idTokenInfoId", "idTokenInfo"]);
const localListAuthorization = yield this.localListAuthorization.create(tenantId, Authorization_1.LocalListAuthorization.build(Object.assign(Object.assign({}, authorizationFields), { idTokenInfoId: localListAuthIdTokenInfo.id, authorizationId: id })));
yield SendLocalListAuthorization_1.SendLocalListAuthorization.create({
tenantId,
sendLocalListId: sendLocalList.id,
authorizationId: localListAuthorization.id,
});
}
yield sendLocalList.reload({ include: [Authorization_1.LocalListAuthorization] });
this.sendLocalList.emit('created', [sendLocalList]);
return sendLocalList;
});
}
validateOrReplaceLocalListVersionForStation(tenantId, versionNumber, stationId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
const localListVersion = yield Authorization_1.LocalListVersion.findOne({
where: { stationId },
transaction,
});
if (localListVersion && localListVersion.versionNumber === versionNumber) {
return;
}
if (localListVersion && localListVersion.versionNumber !== versionNumber) {
// Remove associations
yield LocalListVersionAuthorization_1.LocalListVersionAuthorization.destroy({
where: { localListVersionId: localListVersion.id },
transaction,
});
}
if (!localListVersion) {
const newLocalListVersion = yield Authorization_1.LocalListVersion.create({ tenantId, stationId, versionNumber }, { transaction });
this.emit('created', [newLocalListVersion]);
}
else {
yield localListVersion.update({ versionNumber }, { transaction });
this.emit('updated', [localListVersion]);
}
}));
});
}
getSendLocalListRequestByStationIdAndCorrelationId(tenantId, stationId, correlationId) {
return __awaiter(this, void 0, void 0, function* () {
return this.sendLocalList.readOnlyOneByQuery(tenantId, { where: { stationId, correlationId } });
});
}
createOrUpdateLocalListVersionFromStationIdAndSendLocalList(tenantId, stationId, sendLocalList) {
return __awaiter(this, void 0, void 0, function* () {
switch (sendLocalList.updateType) {
case base_1.OCPP2_0_1.UpdateEnumType.Full:
return this.replaceLocalListVersionFromStationIdAndSendLocalList(tenantId, stationId, sendLocalList);
case base_1.OCPP2_0_1.UpdateEnumType.Differential:
return this.updateLocalListVersionFromStationIdAndSendLocalListRequest(tenantId, stationId, sendLocalList);
}
});
}
replaceLocalListVersionFromStationIdAndSendLocalList(tenantId, stationId, sendLocalList) {
return __awaiter(this, void 0, void 0, function* () {
const localListVersion = yield this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
const oldLocalListVersion = yield Authorization_1.LocalListVersion.findOne({
where: { stationId },
include: [Authorization_1.LocalListAuthorization],
transaction,
});
if (oldLocalListVersion) {
// Remove associations
yield LocalListVersionAuthorization_1.LocalListVersionAuthorization.destroy({
where: { localListVersionId: oldLocalListVersion.id },
transaction,
});
// Destroy old version
yield Authorization_1.LocalListVersion.destroy({ where: { stationId }, transaction });
}
const localListVersion = yield Authorization_1.LocalListVersion.create({
tenantId,
stationId,
versionNumber: sendLocalList.versionNumber,
}, { transaction });
if (!sendLocalList.localAuthorizationList) {
return localListVersion;
}
for (const auth of sendLocalList.localAuthorizationList) {
yield LocalListVersionAuthorization_1.LocalListVersionAuthorization.create({
tenantId,
localListVersionId: localListVersion.id,
authorizationId: auth.id,
}, { transaction });
}
return localListVersion.reload({ include: [Authorization_1.LocalListAuthorization], transaction });
}));
this.emit('created', [localListVersion]);
return localListVersion;
});
}
updateLocalListVersionFromStationIdAndSendLocalListRequest(tenantId, stationId, sendLocalList) {
return __awaiter(this, void 0, void 0, function* () {
const localListVersion = yield this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
var _a;
if (!sendLocalList.localAuthorizationList) {
// See D01.FR.05
const localListVersion = yield this._updateAllByQuery(tenantId, { versionNumber: sendLocalList.versionNumber }, { where: { stationId }, transaction });
if (localListVersion.length !== 1) {
throw new Error(`LocalListVersion not found for ${stationId} during differential version update: ${JSON.stringify(localListVersion)}`);
}
else {
return localListVersion[0];
}
}
const localListVersion = yield Authorization_1.LocalListVersion.findOne({
where: { stationId },
include: [Authorization_1.LocalListAuthorization],
transaction,
});
if (!localListVersion) {
throw new Error(`LocalListVersion not found for ${stationId} during differential update`);
}
for (const sendAuth of sendLocalList.localAuthorizationList) {
// If there is already an association with the same authorizationId, remove it
const oldAuth = (_a = localListVersion.localAuthorizationList) === null || _a === void 0 ? void 0 : _a.find((localAuth) => localAuth.authorizationId === sendAuth.authorizationId);
if (oldAuth) {
yield LocalListVersionAuthorization_1.LocalListVersionAuthorization.destroy({
where: {
localListVersionId: localListVersion.id,
authorizationId: oldAuth.authorizationId,
},
transaction,
});
}
yield LocalListVersionAuthorization_1.LocalListVersionAuthorization.create({
tenantId,
localListVersionId: localListVersion.id,
authorizationId: sendAuth.id,
}, { transaction });
}
yield localListVersion.update({ versionNumber: sendLocalList.versionNumber }, { transaction });
return localListVersion.reload({ include: [Authorization_1.LocalListAuthorization], transaction });
}));
this.emit('updated', [localListVersion]);
return localListVersion;
});
}
}
exports.SequelizeLocalAuthListRepository = SequelizeLocalAuthListRepository;
//# sourceMappingURL=LocalAuthList.js.map