52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
"use strict";
|
|
const db = require("../../models/index");
|
|
|
|
const sequelize = require("sequelize");
|
|
const Op = sequelize.Op;
|
|
|
|
const bulkUpsertKiviPhotos = async kiviPhotosData => {
|
|
try {
|
|
return await db.KiviOriginalAdsPhotos.bulkCreate(kiviPhotosData, {
|
|
ignoreDuplicates: true
|
|
});
|
|
} catch (e) {
|
|
console.log("Error bulk upserting kiviOriginalAdsPhotos : ", e);
|
|
}
|
|
};
|
|
|
|
const findPhotosForKiviAd = async id => {
|
|
try {
|
|
return db.KiviOriginalAdsPhotos.findAll({
|
|
where: {
|
|
kiviAdId: id
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.log("kiviOriginalAdsPhotos.js", error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
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,
|
|
deleteUrlPhotosAfterUpdate
|
|
};
|