Files
old-web/app/helpers/db/dbHelper.js
2019-09-10 10:56:16 +02:00

107 lines
2.1 KiB
JavaScript

const db = require("../../models/index");
/**
* Find all subscribed RealEstateRequests
*/
const allRERequest = async () => {
return await db.RealEstateRequest.findAll({
where: {
subscribed: true
}
});
};
/**
* Find all subscribed RealEstateRequests by UUID
*/
const allRERequestByUiid = async requestArray => {
const Op = db.Sequelize.Op;
return await db.RealEstateRequest.findAll({
where: {
subscribed: true,
[Op.or]: requestArray
}
});
};
/**
* Find all , or all depending on notified bolean marketalerts, that the hasLocation is true, and order them by email
*
* @param fechAll bolean
* @param notified bolean
*
* @returns array of MarketAlerts
*/
const allMarketAlerts = async (fetchAll, notified) => {
let queryObject = {
order: [["email", "DESC"]]
};
if (!fetchAll) {
queryObject.where = {
notified: notified,
hasLocation: true
};
}
return await db.MarketAlert.findAll(queryObject);
};
/**
* Find all , MarketAlerts depending on request
*
* @param request string
*
* @returns array of MarketAlerts
*/
const allMarketAlertsByRequest = async request => {
let queryObject = {
where: {
request: request
}
};
return await db.MarketAlert.findAll(queryObject);
};
/**
* Find MarketAlerts by id
*
* @param id number
*
* @returns single MarketAlert
*/
const getMarketAlertById = async id => {
let queryObject = {
where: {
id: id
}
};
return await db.MarketAlert.findOne(queryObject);
};
/**
* Find all unnotified marketalerts
* @param latLng array
* @param email string
*
* @returns array of MarketAlerts
*/
const findPointInsideBoundingBox = async (latLng, email, uniqueId) => {
return await db.sequelize.query(
`SELECT * FROM "RealEstateRequests" WHERE email = '${email}' AND "uniqueId" = '${uniqueId}' AND subscribed = true AND ST_Contains("RealEstateRequests"."boundingBox", ST_GEOMFROMTEXT('POINT (${
latLng[0]
} ${latLng[1]})'))`
);
};
module.exports = {
allRERequest,
allMarketAlerts,
allRERequestByUiid,
findPointInsideBoundingBox,
allMarketAlertsByRequest,
getMarketAlertById
};