@citrineos/data
Version:
The OCPP data module which includes all persistence layer implementation.
97 lines • 4.71 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.SequelizeBootRepository = void 0;
const Boot_1 = require("../model/Boot");
const DeviceModel_1 = require("../model/DeviceModel");
const __1 = require("..");
class SequelizeBootRepository extends __1.SequelizeRepository {
constructor(config, logger, sequelizeInstance, variableAttributes) {
super(config, Boot_1.Boot.MODEL_NAME, logger, sequelizeInstance);
this.variableAttributes = variableAttributes
? variableAttributes
: new __1.SequelizeRepository(config, DeviceModel_1.VariableAttribute.MODEL_NAME, logger, sequelizeInstance);
}
createOrUpdateByKey(tenantId, value, key) {
return __awaiter(this, void 0, void 0, function* () {
let savedBootConfig;
let created;
yield this.s.transaction((sequelizeTransaction) => __awaiter(this, void 0, void 0, function* () {
const [boot, bootCreated] = yield this.readOrCreateByQuery(tenantId, {
where: {
tenantId,
id: key,
},
defaults: Object.assign({}, value),
transaction: sequelizeTransaction,
});
if (!bootCreated) {
savedBootConfig = yield boot.update(Object.assign({}, value), { transaction: sequelizeTransaction });
}
else {
savedBootConfig = boot;
}
created = bootCreated;
}));
if (savedBootConfig) {
if (value.pendingBootSetVariableIds) {
savedBootConfig.pendingBootSetVariables = yield this.manageSetVariables(tenantId, value.pendingBootSetVariableIds, key, savedBootConfig.id);
}
this.emit(created ? 'created' : 'updated', [savedBootConfig]);
}
return savedBootConfig;
});
}
updateStatusByKey(tenantId, status, statusInfo, key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.updateByKey(tenantId, { status, statusInfo }, key);
});
}
updateLastBootTimeByKey(tenantId, lastBootTime, key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.updateByKey(tenantId, { lastBootTime }, key);
});
}
/**
* Private Methods
*/
manageSetVariables(tenantId, setVariableIds, stationId, bootConfigId) {
return __awaiter(this, void 0, void 0, function* () {
const managedSetVariables = [];
// Unassigns variables
yield this.variableAttributes.updateAllByQuery(tenantId, { bootConfigId: null }, {
where: {
stationId,
},
});
// Assigns variables, or throws an error if variable with id does not exist
for (const setVariableId of setVariableIds) {
const setVariable = yield this.variableAttributes.updateByKey(tenantId, { bootConfigId }, setVariableId.toString());
if (!setVariable) {
// When this is called from createOrUpdateByKey, this code should be impossible to reach
// Since the boot object would have already been upserted with the pendingBootSetVariableIds as foreign keys
// And if they were not valid foreign keys, it would have thrown an error
throw new Error('SetVariableId does not exist ' + setVariableId);
}
else {
managedSetVariables.push(setVariable);
}
}
return managedSetVariables;
});
}
}
exports.SequelizeBootRepository = SequelizeBootRepository;
//# sourceMappingURL=Boot.js.map