@citrineos/data
Version:
The OCPP data module which includes all persistence layer implementation.
235 lines • 12 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequelizeAuthorizationRepository = void 0;
const sequelize_1 = require("sequelize");
const Authorization_1 = require("../model/Authorization");
const Base_1 = require("./Base");
const IdTokenAdditionalInfo_1 = require("../model/Authorization/IdTokenAdditionalInfo");
class SequelizeAuthorizationRepository extends Base_1.SequelizeRepository {
constructor(config, logger, sequelizeInstance, idToken, idTokenInfo, additionalInfo, idTokenAdditionalInfo) {
super(config, Authorization_1.Authorization.MODEL_NAME, logger, sequelizeInstance);
this.idToken = idToken
? idToken
: new Base_1.SequelizeRepository(config, Authorization_1.IdToken.MODEL_NAME, logger, sequelizeInstance);
this.idTokenInfo = idTokenInfo
? idTokenInfo
: new Base_1.SequelizeRepository(config, Authorization_1.IdTokenInfo.MODEL_NAME, logger, sequelizeInstance);
this.additionalInfo = additionalInfo
? additionalInfo
: new Base_1.SequelizeRepository(config, Authorization_1.AdditionalInfo.MODEL_NAME, logger, sequelizeInstance);
this.idTokenAdditionalInfo = idTokenAdditionalInfo
? idTokenAdditionalInfo
: new Base_1.SequelizeRepository(config, IdTokenAdditionalInfo_1.IdTokenAdditionalInfo.MODEL_NAME, logger, sequelizeInstance);
}
createOrUpdateByQuerystring(value, query, transaction) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (value.idToken.idToken !== query.idToken || value.idToken.type !== query.type) {
throw new Error('Authorization idToken does not match query');
}
const savedAuthorizationModel = yield Authorization_1.Authorization.findOne({
include: [
{
model: Authorization_1.IdToken,
where: {
idToken: query.idToken,
type: query.type,
},
},
],
transaction,
});
const authorizationModel = savedAuthorizationModel !== null && savedAuthorizationModel !== void 0 ? savedAuthorizationModel : Authorization_1.Authorization.build({}, this._createInclude(value));
const updatedIdToken = yield this._updateIdToken(value.idToken, transaction);
authorizationModel.idTokenId = updatedIdToken.id;
if (value.idTokenInfo) {
const valueIdTokenInfo = Authorization_1.IdTokenInfo.build(Object.assign({}, value.idTokenInfo));
// Explicitly overwrite id with undefined to avoid mapping null onto the existing id in the for loop
valueIdTokenInfo.id = (_a = valueIdTokenInfo.id) !== null && _a !== void 0 ? _a : undefined;
if (authorizationModel.idTokenInfoId) {
const savedIdTokenInfo = yield Authorization_1.IdTokenInfo.findOne({
where: { id: authorizationModel.idTokenInfoId },
include: [{ model: Authorization_1.IdToken, include: [Authorization_1.AdditionalInfo] }],
transaction,
});
if (!savedIdTokenInfo) {
throw new Error(`IdTokenInfo for foreign key ${authorizationModel.idTokenInfoId} not found`);
}
Object.keys(valueIdTokenInfo.dataValues).forEach((k) => {
const updatedValue = valueIdTokenInfo.getDataValue(k);
if (updatedValue !== undefined) {
// Null can still be used to remove data
savedIdTokenInfo.setDataValue(k, updatedValue);
}
});
if (value.idTokenInfo.groupIdToken) {
const savedGroupIdToken = yield this._updateIdToken(value.idTokenInfo.groupIdToken, transaction);
if (!savedIdTokenInfo.groupIdTokenId) {
savedIdTokenInfo.groupIdTokenId = savedGroupIdToken.id;
}
}
else if (savedIdTokenInfo.groupIdTokenId) {
savedIdTokenInfo.groupIdTokenId = undefined;
savedIdTokenInfo.groupIdToken = undefined;
}
yield savedIdTokenInfo.save({ transaction });
}
else {
if (value.idTokenInfo.groupIdToken) {
const savedGroupIdToken = yield this._updateIdToken(value.idTokenInfo.groupIdToken, transaction);
valueIdTokenInfo.groupIdTokenId = savedGroupIdToken.id;
}
const createdIdTokenInfo = yield valueIdTokenInfo.save({ transaction });
authorizationModel.idTokenInfoId = createdIdTokenInfo.id;
}
}
else if (authorizationModel.idTokenInfoId) {
// Remove idTokenInfo
authorizationModel.idTokenInfoId = undefined;
authorizationModel.idTokenInfo = undefined;
}
return yield authorizationModel.save({ transaction });
});
}
updateRestrictionsByQuerystring(value, query) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.updateAllByQuery(value, query);
});
}
readAllByQuerystring(query) {
const _super = Object.create(null, {
readAllByQuery: { get: () => super.readAllByQuery }
});
return __awaiter(this, void 0, void 0, function* () {
return yield _super.readAllByQuery.call(this, this._constructQuery(query));
});
}
readOnlyOneByQuerystring(query) {
const _super = Object.create(null, {
readOnlyOneByQuery: { get: () => super.readOnlyOneByQuery }
});
return __awaiter(this, void 0, void 0, function* () {
return yield _super.readOnlyOneByQuery.call(this, this._constructQuery(query));
});
}
existByQuerystring(query) {
const _super = Object.create(null, {
existByQuery: { get: () => super.existByQuery }
});
return __awaiter(this, void 0, void 0, function* () {
return yield _super.existByQuery.call(this, this._constructQuery(query));
});
}
deleteAllByQuerystring(query) {
const _super = Object.create(null, {
deleteAllByQuery: { get: () => super.deleteAllByQuery }
});
return __awaiter(this, void 0, void 0, function* () {
return yield _super.deleteAllByQuery.call(this, this._constructQuery(query), Authorization_1.Authorization.MODEL_NAME);
});
}
/**
* Private Methods
*/
_updateIdToken(value, transaction) {
return __awaiter(this, void 0, void 0, function* () {
const [savedIdTokenModel] = yield Authorization_1.IdToken.findOrCreate({
where: { idToken: value.idToken, type: value.type },
transaction,
});
const additionalInfoIds = [];
// Create any additionalInfos that don't exist,
// and any relations between them and the IdToken that don't exist
if (value.additionalInfo) {
for (const valueAdditionalInfo of value.additionalInfo) {
const [savedAdditionalInfo] = yield Authorization_1.AdditionalInfo.findOrCreate({
where: {
additionalIdToken: valueAdditionalInfo.additionalIdToken,
type: valueAdditionalInfo.type,
},
transaction,
});
yield IdTokenAdditionalInfo_1.IdTokenAdditionalInfo.findOrCreate({
where: {
idTokenId: savedIdTokenModel.id,
additionalInfoId: savedAdditionalInfo.id,
},
transaction,
});
additionalInfoIds.push(savedAdditionalInfo.id);
}
}
// Remove all associations between idToken and additionalInfo that no longer exist
yield IdTokenAdditionalInfo_1.IdTokenAdditionalInfo.destroy({
where: {
idTokenId: savedIdTokenModel.id,
additionalInfoId: { [sequelize_1.Op.notIn]: additionalInfoIds },
},
transaction,
});
return savedIdTokenModel.reload({ include: [Authorization_1.AdditionalInfo], transaction });
});
}
_constructQuery(queryParams) {
// 1.6 doesn't have the concept of token type. But we need to support token type for 2.0.1 messages.
// We ignore token type if it's explicitly set to null, as it's coming from a 1.6 message
const idTokenWhere = {};
if (queryParams.idToken) {
// exact match
idTokenWhere.idToken = queryParams.idToken;
// or partial match:
// idTokenWhere.idToken = { [Op.like]: `%${queryParams.idToken}%` };
}
// only include type if it's provided
if (queryParams.type) {
idTokenWhere.type = queryParams.type;
}
return {
where: {},
include: [
{
model: Authorization_1.IdToken,
where: idTokenWhere,
required: true, // This ensures the inner join, so only Authorizations with the matching IdToken are returned
},
{ model: Authorization_1.IdTokenInfo, include: [{ model: Authorization_1.IdToken, include: [Authorization_1.AdditionalInfo] }] },
],
};
}
_createInclude(value) {
var _a;
const include = [];
if (value.idTokenInfo) {
const idTokenInfoInclude = [];
if (value.idTokenInfo.groupIdToken) {
const idTokenInfoGroupIdTokenInclude = [];
if ((_a = value.idTokenInfo) === null || _a === void 0 ? void 0 : _a.groupIdToken.additionalInfo) {
idTokenInfoGroupIdTokenInclude.push(Authorization_1.AdditionalInfo);
}
idTokenInfoInclude.push({ model: Authorization_1.IdToken, include: idTokenInfoGroupIdTokenInclude });
}
include.push({ model: Authorization_1.IdTokenInfo, include: idTokenInfoInclude });
}
const idTokenInclude = [];
if (value.idToken.additionalInfo) {
idTokenInclude.push(Authorization_1.AdditionalInfo);
}
include.push({ model: Authorization_1.IdToken, include: idTokenInclude });
return { include };
}
}
exports.SequelizeAuthorizationRepository = SequelizeAuthorizationRepository;
//# sourceMappingURL=Authorization.js.map