@golemio/parkings
Version:
Golemio Parkings Module
345 lines • 18.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParkingsModel = void 0;
const Di_1 = require("../ioc/Di");
const ModuleContainerToken_1 = require("../ioc/ModuleContainerToken");
const DetailAttributesHelper_1 = require("./helpers/DetailAttributesHelper");
const index_1 = require("../../schema-definitions/index");
const output_gateway_1 = require("@golemio/core/dist/output-gateway");
const database_1 = require("@golemio/core/dist/output-gateway/database");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const sequelize_1 = __importStar(require("@golemio/core/dist/shared/sequelize"));
class ParkingsModel extends output_gateway_1.SequelizeModel {
constructor(parkingSourcesRepository) {
super(index_1.Parkings.name, index_1.Parkings.parkingsApiV2.pgTableName, index_1.Parkings.parkingsApiV2.outputSequelizeAttributes, {
schema: index_1.Parkings.pgSchema,
});
this.parkingSourcesRepository = parkingSourcesRepository;
this.Associate = (m) => {
this.sequelizeModel.hasOne(m.ParkingsMeasurementsActualModel.sequelizeModel, {
as: ParkingsModel.MODEL_RELATION_ACTUAL_MEASUREMENTS,
foreignKey: "source_id",
sourceKey: "source_id",
});
this.sequelizeModel.hasMany(m.ParkingsLocationModel.sequelizeModel, {
as: ParkingsModel.MODEL_RELATION_PARKING_LOCATIONS,
foreignKey: "source_id",
sourceKey: "source_id",
});
this.sequelizeModel.hasOne(m.ParkingPaymentsRepository.sequelizeModel, {
as: ParkingsModel.MODEL_RELATION_PARKING_PAYMENTS,
foreignKey: "parking_id",
sourceKey: "id",
});
this.sequelizeModel.hasOne(m.ParkingTariffRelationsRepository.sequelizeModel, {
as: ParkingsModel.MODEL_RELATION_PARKING_TARIFFS_RELATIONS,
foreignKey: "parking_id",
sourceKey: "id",
});
};
this.GetAll = async (options = {}) => {
const { lat, lng, range, source, sourceId, category, limit, offset, updatedSince, minutesBefore } = options;
const order = [];
const areaLimit = sequelize_1.default.fn("ST_BuildArea", sequelize_1.default.fn("ST_GeomFromGeoJSON", JSON.stringify(this.geoConfigHelper.returnGeoCoordinates())));
let where = {};
let attributes = { include: [], exclude: [] };
try {
if (lat && lng) {
const location = sequelize_1.default.fn("ST_SetSRID", sequelize_1.default.fn("ST_MakePoint", lng, lat), 4326);
const distance = sequelize_1.default.fn("ST_Distance", sequelize_1.default.col("sanitized_location"), location, true);
attributes.include.push([distance, "distance"]);
order.push([sequelize_1.default.col("distance"), "asc"]);
if (range) {
where.range = sequelize_1.default.where(sequelize_1.default.fn("ST_DWithin", location, sequelize_1.default.col("sanitized_location"), range, true), "true");
}
}
where.sanitized_location = sequelize_1.default.where(sequelize_1.default.fn("ST_Within", sequelize_1.default.col("sanitized_location"), areaLimit), "true");
if (source) {
where.source = source;
}
else {
where.source = {
[sequelize_1.default.Op.in]: await this.parkingSourcesRepository.getLegacySources({ isRestrictedToOpenData: false }),
};
}
if (sourceId) {
where.source_id_mapped = sourceId;
}
if (category) {
where.category = {
[sequelize_1.default.Op.in]: category,
};
}
if (minutesBefore) {
where.updated_at = {
[sequelize_1.default.Op.gte]: sequelize_1.default.literal(`NOW() - INTERVAL '${minutesBefore} minutes'`),
};
}
if (updatedSince) {
where.updated_at = {
[sequelize_1.default.Op.gte]: updatedSince,
};
}
order.push([sequelize_1.default.col("source"), "asc"]);
order.push([sequelize_1.default.col("source_id_mapped"), "asc"]);
return await this.sequelizeModel.findAll({
attributes,
include: [
{
as: ParkingsModel.MODEL_RELATION_ACTUAL_MEASUREMENTS,
model: database_1.sequelizeConnection.models[index_1.Parkings.measurementsActual.pgTableName],
attributes: [
[sequelize_1.default.col("date_modified"), "available_spots_last_updated"],
[sequelize_1.default.col("available_spot_number"), "available_spots_number"],
],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_PAYMENTS,
model: this.paymentsRepository.sequelizeModel,
attributes: ["payment_web_url", "payment_android_url", "payment_ios_url"],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_TARIFFS_RELATIONS,
model: this.parkingTariffRelationsRepository.sequelizeModel,
attributes: ["tariff_id"],
},
],
limit,
offset,
order,
raw: true,
nest: true,
where,
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Database error", "ParkingsModel", err, 500);
}
};
this.GetOne = async (id) => {
return await this.sequelizeModel.findOne({
include: [
{
as: ParkingsModel.MODEL_RELATION_ACTUAL_MEASUREMENTS,
model: database_1.sequelizeConnection.models[index_1.Parkings.measurementsActual.pgTableName],
attributes: [
[sequelize_1.default.col("date_modified"), "available_spots_last_updated"],
[sequelize_1.default.col("available_spot_number"), "available_spots_number"],
],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_PAYMENTS,
model: this.paymentsRepository.sequelizeModel,
attributes: ["payment_web_url", "payment_android_url", "payment_ios_url"],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_TARIFFS_RELATIONS,
model: this.parkingTariffRelationsRepository.sequelizeModel,
attributes: ["tariff_id"],
},
],
where: {
id,
source: { [sequelize_1.Op.in]: await this.parkingSourcesRepository.getLegacySources({ isRestrictedToOpenData: false }) },
},
raw: true,
nest: true,
});
};
this.GetAllDetail = async (options = {}) => {
const { limit, offset } = options;
const order = [];
const where = {};
const attributes = (0, DetailAttributesHelper_1.getParkingDetailAttributes)();
try {
if (options.lat && options.lng) {
const location = sequelize_1.default.fn("ST_SetSRID", sequelize_1.default.fn("ST_MakePoint", options.lng, options.lat), 4326);
const distance = sequelize_1.default.fn("ST_Distance", sequelize_1.default.literal(`CASE WHEN ${ParkingsModel.MODEL_RELATION_PARKING_LOCATIONS}.sanitized_locations IS NULL
THEN ${index_1.Parkings.parkingsApiV2.pgTableName}.sanitized_location
ELSE ${ParkingsModel.MODEL_RELATION_PARKING_LOCATIONS}.sanitized_locations END`), location, true);
attributes.push([distance, "distance"]);
order.push([sequelize_1.default.col("distance"), "asc"]);
if (options.range) {
where.range = sequelize_1.default.where(sequelize_1.default.fn("ST_DWithin", location, sequelize_1.default.col("v_parkings_api_v2.sanitized_location"), options.range, true), "true");
}
}
if (options.source) {
where.source = options.source;
}
else {
where.source = {
[sequelize_1.Op.in]: await this.parkingSourcesRepository.getLegacySources({ isRestrictedToOpenData: false }),
};
}
if (options.sourceId) {
where.source_id_mapped = options.sourceId;
}
if (options.category) {
where.category = {
[sequelize_1.default.Op.in]: options.category,
};
}
if (options.minutesBefore) {
const now = Date.now();
const beforeTime = now - Number(options.minutesBefore) * 60 * 1000;
where.date_modified = {
[sequelize_1.default.Op.gte]: new Date(beforeTime).toISOString(),
};
}
if (options.updatedSince) {
where.date_modified = {
[sequelize_1.default.Op.gte]: new Date(options.updatedSince).toISOString(),
};
}
if (options.parkingType) {
where.parking_type = {
[sequelize_1.default.Op.in]: options.parkingType,
};
}
if (options.zoneType) {
where.zone_type = {
[sequelize_1.default.Op.in]: options.zoneType,
};
}
order.push([sequelize_1.default.col("source"), "asc"]);
order.push([sequelize_1.default.col("source_id"), "asc"]);
return await this.sequelizeModel.findAll({
attributes,
include: [
{
as: ParkingsModel.MODEL_RELATION_PARKING_LOCATIONS,
model: database_1.sequelizeConnection.models[index_1.Parkings.location.pgTableName],
attributes: [],
on: {
source: {
[sequelize_1.Op.eq]: sequelize_1.default.col(`"${this.sequelizeModel.tableName}".source`),
},
source_id: {
[sequelize_1.Op.eq]: sequelize_1.default.col(`"${this.sequelizeModel.tableName}".source_id`),
},
},
required: false,
where: sequelize_1.default.literal(`"parking_locations".updated_at > ("v_parkings_api_v2".updated_at - interval '2 day') AND ` +
`NOT ("parking_locations".source = 'tsk_v2' AND ` +
`COALESCE('disabled' = ANY("parking_locations"."special_access"), false))`),
},
{
as: ParkingsModel.MODEL_RELATION_ACTUAL_MEASUREMENTS,
model: database_1.sequelizeConnection.models[index_1.Parkings.measurementsActual.pgTableName],
attributes: [
[sequelize_1.default.col("date_modified"), "available_spots_last_updated"],
[sequelize_1.default.col("available_spot_number"), "available_spots_number"],
],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_PAYMENTS,
model: this.paymentsRepository.sequelizeModel,
attributes: ["payment_web_url", "payment_android_url", "payment_ios_url"],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_TARIFFS_RELATIONS,
model: this.parkingTariffRelationsRepository.sequelizeModel,
attributes: ["tariff_id"],
},
],
limit,
offset,
order,
raw: true,
nest: true,
where,
subQuery: false,
});
}
catch (err) {
throw new golemio_errors_1.GeneralError("Database error", "ParkingsLocationModel", err, 500);
}
};
this.GetOneDetail = async (id) => {
const [parkingId, locationId] = id.split("_");
return await this.sequelizeModel.findOne({
attributes: (0, DetailAttributesHelper_1.getParkingDetailAttributes)(),
include: [
{
as: ParkingsModel.MODEL_RELATION_PARKING_LOCATIONS,
model: database_1.sequelizeConnection.models[index_1.Parkings.location.pgTableName],
attributes: [],
on: {
source: {
[sequelize_1.Op.eq]: sequelize_1.default.col(`"${this.sequelizeModel.tableName}".source`),
},
source_id: {
[sequelize_1.Op.eq]: sequelize_1.default.col(`"${this.sequelizeModel.tableName}".source_id`),
},
},
required: !!locationId,
where: {
...(locationId && { id: locationId }),
[sequelize_1.Op.and]: sequelize_1.default.literal(`NOT ("parking_locations".source = 'tsk_v2' AND ` +
`COALESCE('disabled' = ANY("parking_locations"."special_access"), false))`),
},
},
{
as: ParkingsModel.MODEL_RELATION_ACTUAL_MEASUREMENTS,
model: database_1.sequelizeConnection.models[index_1.Parkings.measurementsActual.pgTableName],
attributes: [
[sequelize_1.default.col("date_modified"), "available_spots_last_updated"],
[sequelize_1.default.col("available_spot_number"), "available_spots_number"],
],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_PAYMENTS,
model: this.paymentsRepository.sequelizeModel,
attributes: ["payment_web_url", "payment_android_url", "payment_ios_url"],
},
{
as: ParkingsModel.MODEL_RELATION_PARKING_TARIFFS_RELATIONS,
model: this.parkingTariffRelationsRepository.sequelizeModel,
attributes: ["tariff_id"],
},
],
where: {
id: parkingId,
source: {
[sequelize_1.Op.in]: await this.parkingSourcesRepository.getLegacySources({ isRestrictedToOpenData: false }),
},
},
raw: true,
nest: true,
subQuery: false,
});
};
this.paymentsRepository = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.ParkingPaymentsRepository);
this.parkingTariffRelationsRepository = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.ParkingTariffRelationsRepository);
this.geoConfigHelper = Di_1.ParkingsContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.GeoConfigHelper);
}
}
exports.ParkingsModel = ParkingsModel;
ParkingsModel.MODEL_RELATION_ACTUAL_MEASUREMENTS = "measurements";
ParkingsModel.MODEL_RELATION_PARKING_LOCATIONS = "parking_locations";
ParkingsModel.MODEL_RELATION_PARKING_PAYMENTS = "parking_payments";
ParkingsModel.MODEL_RELATION_PARKING_TARIFFS_RELATIONS = "parking_tariffs_relation";
//# sourceMappingURL=ParkingsModel.js.map