@golemio/parkings
Version:
Golemio Parkings Module
105 lines • 5.28 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParkingsLocationRepository = void 0;
const SourceEnum_1 = require("../../helpers/constants/SourceEnum");
const _sch_1 = require("../../schema-definitions");
const ParkingLocationsModel_1 = require("../../schema-definitions/models/ParkingLocationsModel");
const models_1 = require("@golemio/core/dist/integration-engine/models");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const sequelize_1 = require("@golemio/core/dist/shared/sequelize");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let ParkingsLocationRepository = exports.ParkingsLocationRepository = class ParkingsLocationRepository extends models_1.PostgresModel {
constructor() {
super(_sch_1.Parkings.location.name + "Model", {
outputSequelizeAttributes: ParkingLocationsModel_1.ParkingLocationsModel.attributeModel,
pgSchema: _sch_1.Parkings.pgSchema,
pgTableName: ParkingLocationsModel_1.ParkingLocationsModel.tableName,
savingType: "insertOrUpdate",
}, new golemio_validator_1.JSONSchemaValidator(_sch_1.Parkings.location.name + "PgModelValidator", _sch_1.Parkings.location.outputJsonSchema));
this.GetOne = async (id) => {
try {
return await this.sequelizeModel.findOne({
where: { id },
raw: true,
nest: true,
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Database error", "ParkingsModel", err, 500);
}
};
this.saveWithoutAddress = async (parkingLocations, source, dataProvider, condition) => {
const transaction = await this.sequelizeModel.sequelize.transaction();
try {
await this.bulkSave(parkingLocations, this.getParkingAttributesToUpdate(), false, false, transaction);
const activeIds = parkingLocations.map((parkingLocations) => parkingLocations.id);
await this.markInactiveParkingsLocations(activeIds, source, transaction, dataProvider, condition);
await transaction.commit();
}
catch (err) {
await transaction.rollback();
throw new golemio_errors_1.GeneralError("Database error", "ParkingsLocationModel", err, 500);
}
};
}
async saveTskParkingLocations(parkingLocations, includeDisabled, dataProvider) {
const condition = includeDisabled
? { special_access: { [sequelize_1.Op.contains]: ["disabled"] } }
: {
[sequelize_1.Op.or]: [{ [sequelize_1.Op.not]: { special_access: { [sequelize_1.Op.contains]: ["disabled"] } } }, { special_access: null }],
};
await this.saveWithoutAddress(parkingLocations, SourceEnum_1.SourceEnum.TSK_V2, dataProvider, condition);
}
async markInactiveParkingsLocations(activeIds, source, transaction, dataProvider, condition) {
await this.update({
active: false,
}, {
where: {
source,
active: true,
id: { [sequelize_1.Op.notIn]: activeIds },
...(dataProvider && { data_provider: dataProvider }),
...condition,
},
transaction,
});
}
async findWithOutdatedAddress(batchSize, updateInterval) {
return this.find({
limit: batchSize,
where: {
address_updated_at: {
[sequelize_1.Op.lte]: sequelize_1.Sequelize.literal(`NOW() - INTERVAL '${updateInterval} days'`),
},
},
});
}
async findWithoutAddress() {
return this.find({
where: {
address: null,
address_updated_at: null,
},
});
}
getParkingAttributesToUpdate() {
return ParkingLocationsModel_1.ParkingLocationsModel.attributeList
.filter((key) => !["address", "address_updated_at"].includes(key))
.concat("updated_at");
}
};
exports.ParkingsLocationRepository = ParkingsLocationRepository = __decorate([
(0, tsyringe_1.injectable)(),
__metadata("design:paramtypes", [])
], ParkingsLocationRepository);
//# sourceMappingURL=ParkingsLocationRepository.js.map