@golemio/parkings
Version:
Golemio Parkings Module
123 lines • 6.02 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.ParkingMachinesRepository = void 0;
const SourceEnum_1 = require("../../helpers/constants/SourceEnum");
const Di_1 = require("../ioc/Di");
const ModuleContainerToken_1 = require("../ioc/ModuleContainerToken");
const SequelizeModel_1 = require("@golemio/core/dist/output-gateway/models/SequelizeModel");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const sequelize_1 = require("@golemio/core/dist/shared/sequelize");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let ParkingMachinesRepository = exports.ParkingMachinesRepository = class ParkingMachinesRepository extends SequelizeModel_1.SequelizeModel {
constructor(setup) {
super(setup.name, setup.tableName, setup.attributes, setup.options);
this.shouldHideSecondarySourcesFromPrimaryData = true;
this.associate = () => {
this.sequelizeModel.hasOne(this.parkingTariffRelationsRepository.sequelizeModel, {
as: "parking_tariffs_relation",
foreignKey: "parking_id",
sourceKey: "id",
});
};
this.geoConfigHelper = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.GeoConfigHelper);
this.parkingTariffRelationsRepository = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.ParkingTariffRelationsRepository);
this.associate();
}
async GetAll(params) {
try {
let where = {};
let primarySources = [];
const areaLimit = sequelize_1.Sequelize.fn("ST_BuildArea", sequelize_1.Sequelize.fn("ST_GeomFromGeoJSON", JSON.stringify(this.geoConfigHelper.returnGeoCoordinates())));
if (params.primarySource) {
primarySources = params.primarySource;
}
else {
throw new golemio_errors_1.GeneralError("List of sources must be provided! ", this.name, undefined, 500);
}
// DO NOT SHOW OSM PARKING MACHINES IF THERE ARE OTHER SOURCES
if (this.shouldHideSecondarySourcesFromPrimaryData && primarySources.length > 1) {
primarySources = primarySources.filter((source) => source !== SourceEnum_1.SourceEnum.OSM);
}
where.source = { [sequelize_1.Op.in]: primarySources };
where.location = sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("ST_Within", sequelize_1.Sequelize.col("location"), areaLimit), "true");
if (params.validFrom) {
where.validFrom = { [sequelize_1.Op.gte]: params.validFrom };
}
if (params.type) {
where.type = { [sequelize_1.Op.in]: params.type };
}
if (params.codeMask) {
where.code = { [sequelize_1.Op.like]: params.codeMask };
}
if (params.activeOnly) {
where.active = true;
}
if (params.boundingBox) {
where = {
...where,
[sequelize_1.Op.and]: (0, sequelize_1.literal)(`ST_Contains(ST_MakeEnvelope($lonMin, $latMin, $lonMax, $latMax, 4326), location)`),
};
}
return await this.sequelizeModel.findAll({
raw: true,
nest: true,
include: [
{
model: this.parkingTariffRelationsRepository.sequelizeModel,
as: "parking_tariffs_relation",
attributes: ["tariff_id"],
required: false,
subQuery: false,
},
],
where,
limit: params.limit,
offset: params.offset,
bind: params.boundingBox && {
lonMin: params.boundingBox[1],
latMin: params.boundingBox[2],
lonMax: params.boundingBox[3],
latMax: params.boundingBox[0],
},
order: [["id", "ASC"]],
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Database error ~ GetAll", this.name, err, 500);
}
}
async GetOne(id) {
try {
return await this.sequelizeModel.findByPk(id, {
raw: true,
nest: true,
include: [
{
model: this.parkingTariffRelationsRepository.sequelizeModel,
as: "parking_tariffs_relation",
attributes: ["tariff_id"],
required: false,
subQuery: false,
},
],
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Database error ~ GetOne", this.name, err, 500);
}
}
};
exports.ParkingMachinesRepository = ParkingMachinesRepository = __decorate([
(0, tsyringe_1.injectable)(),
__metadata("design:paramtypes", [Object])
], ParkingMachinesRepository);
//# sourceMappingURL=ParkingMachinesRepository.js.map