diff --git a/app/controllers/publishRealEstate.js b/app/controllers/publishRealEstate.js
index b15bf29..f7bbe4f 100644
--- a/app/controllers/publishRealEstate.js
+++ b/app/controllers/publishRealEstate.js
@@ -1,7 +1,8 @@
const { findRealEstateByAgencyId } = require("../helpers/db/realEstate");
const {
bulkUpsertKiviPhotos,
- findPhotosForKiviAd
+ findPhotosForKiviAd,
+ deleteUrlPhotosAfterUpdate
} = require("../helpers/db/kiviOriginalAdsPhotos");
const { currentKiviRealEstate } = require("../helpers/url");
const {
@@ -202,8 +203,8 @@ const getPublishInputs = async (req, res) => {
additionalInputValues,
validate: validate,
email,
- locationLat, //: locationLat || 0,
- locationLong, //: locationLong || 0,
+ locationLat: locationLat || 0,
+ locationLong: locationLong || 0,
editingRealEstate,
realEstatePhotosUrls
});
@@ -225,7 +226,7 @@ const postPublishInputs = async (req, res) => {
const editingRealEstate = req.body.editingRealEstate === "true";
- console.log("Editing real estate:", editingRealEstate);
+ // console.log("Editing real estate:", editingRealEstate);
const nextStepPage = editingRealEstate
? req.query.nextStep || "/uspjesnaizmjena"
@@ -296,6 +297,10 @@ const postPublishInputs = async (req, res) => {
//Image urls are stored in new table
const imageUrls =
req.body.imageUrls.split("|").filter(url => url !== "") || [];
+ //If we are in editing mode we need to "delete" photos that are not longer associated with real estate ad
+ if (editingRealEstate) {
+ await deleteUrlPhotosAfterUpdate(imageUrls);
+ }
const imageUrlsData = imageUrls.map(url => {
return {
diff --git a/app/helpers/db/kiviOriginalAdsPhotos.js b/app/helpers/db/kiviOriginalAdsPhotos.js
index 07cfc59..ae40471 100644
--- a/app/helpers/db/kiviOriginalAdsPhotos.js
+++ b/app/helpers/db/kiviOriginalAdsPhotos.js
@@ -1,6 +1,8 @@
"use strict";
const db = require("../../models/index");
+
const sequelize = require("sequelize");
+const Op = sequelize.Op;
const bulkUpsertKiviPhotos = async kiviPhotosData => {
try {
@@ -25,7 +27,25 @@ const findPhotosForKiviAd = async id => {
}
};
+const deleteUrlPhotosAfterUpdate = async photoUrlsToKeep => {
+ //We delete all urls that are not in "newly changed - edited" photo urls array
+ const deleteQuery = {
+ photoUrl: {
+ [Op.notIn]: photoUrlsToKeep
+ }
+ };
+ try {
+ return db.KiviOriginalAdsPhotos.destroy({
+ where: deleteQuery
+ });
+ } catch (error) {
+ console.log("kiviOriginalAdsPhotos.js", error);
+ return null;
+ }
+};
+
module.exports = {
bulkUpsertKiviPhotos,
- findPhotosForKiviAd
+ findPhotosForKiviAd,
+ deleteUrlPhotosAfterUpdate
};
diff --git a/app/migrations/20200325120224-add-constraint-kiviOriginalAdsPhotos.js b/app/migrations/20200325120224-add-constraint-kiviOriginalAdsPhotos.js
new file mode 100644
index 0000000..2d1e29f
--- /dev/null
+++ b/app/migrations/20200325120224-add-constraint-kiviOriginalAdsPhotos.js
@@ -0,0 +1,18 @@
+"use strict";
+
+module.exports = {
+ up: (queryInterface, Sequelize) =>
+ queryInterface.addConstraint(
+ "KiviOriginalAdsPhotos",
+ ["kiviAdId", "photoUrl"],
+ {
+ type: "unique",
+ name: "uniqueKiviAdIdPhoto"
+ }
+ ),
+ down: queryInterface =>
+ queryInterface.removeConstraint(
+ "KiviOriginalAdsPhotos",
+ "uniqueKiviAdIdPhoto"
+ )
+};
diff --git a/app/models/kiviOriginalAdsPhotos.js b/app/models/kiviOriginalAdsPhotos.js
index 8321a0e..2c5a685 100644
--- a/app/models/kiviOriginalAdsPhotos.js
+++ b/app/models/kiviOriginalAdsPhotos.js
@@ -13,6 +13,7 @@ module.exports = (sequalize, DataTypes) => {
kiviAdId: {
type: DataTypes.UUID,
allowNull: false,
+ unique: "uniqueKiviAdIdPhoto",
references: {
model: "KiviOriginal",
key: "kiviAdId"
@@ -20,7 +21,8 @@ module.exports = (sequalize, DataTypes) => {
},
photoUrl: {
type: DataTypes.TEXT,
- allowNull: false
+ allowNull: false,
+ unique: "uniqueKiviAdIdPhoto"
}
},
{
diff --git a/app/views/publishLocation.ejs b/app/views/publishLocation.ejs
index fb33014..36d42d1 100644
--- a/app/views/publishLocation.ejs
+++ b/app/views/publishLocation.ejs
@@ -107,7 +107,7 @@
//Move map and marker to already selected position if in editing mode
if( editingRealEstate===true) {
- console.log('Editujem mapu!');
+ //console.log('Editujem mapu!');
setMarkerToLocation(map, editingRealEstate);
}
diff --git a/app/views/publishRealEstate.ejs b/app/views/publishRealEstate.ejs
index 3f2dbf0..f26810c 100644
--- a/app/views/publishRealEstate.ejs
+++ b/app/views/publishRealEstate.ejs
@@ -1,7 +1,4 @@
-<% if(editingRealEstate) { %>
-