From 218de161606abd719bc66eea5ad03b75edc4717b Mon Sep 17 00:00:00 2001 From: Ibrahem alnumman Date: Mon, 19 Feb 2024 18:34:53 +0300 Subject: [PATCH] Fix(deleting topics): fix deleting topics in when privicy is both --- widget/app.controller.js | 39 ++++++++++++++++++++++++++++++ widget/app.js | 2 +- widget/index.html | 1 + widget/js/classes/topic.js | 49 ++++++++------------------------------ 4 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 widget/app.controller.js diff --git a/widget/app.controller.js b/widget/app.controller.js new file mode 100644 index 0000000..d1b0ffa --- /dev/null +++ b/widget/app.controller.js @@ -0,0 +1,39 @@ +const appController = { + deleteTopic(topicId, privacy) { + let db = this.getDataSource(privacy); + + if (privacy === 'both') { + db = this.getDataSource('private'); + } else { + db = this.getDataSource(privacy); + } + + return new Promise((resolve, reject) => { + return Topic.delete(db, topicId) + .then((result) => { + resolve(result); + }) + .catch((err) => { + if (err.code === 'NOTFOUND') { + db = this.getDataSource('public'); + return Topic.delete(db, topicId) + .then((result) => { + resolve(result); + }) + .catch((err) => { + console.log(err); + reject(err); + }); + } + }); + }); + }, + + getDataSource(privacy) { + let db = buildfire.publicData; + if (privacy === 'private') { + db = buildfire.userData; + } + return db; + }, +}; diff --git a/widget/app.js b/widget/app.js index cb02d8b..755e4e8 100644 --- a/widget/app.js +++ b/widget/app.js @@ -688,7 +688,7 @@ function openDeleteDialog(topic, targetElement) { event.preventDefault(); dialogDeleteBtn.disabled = true; topic.deletedBy = loggedUser; - topic.delete(config.privacy) + appController.deleteTopic(topic.id, config.privacy) .then(result => { deleteTopicDialog.close(); showMessage(`Successfully deleted ${topic.title} topic`) diff --git a/widget/index.html b/widget/index.html index 7bd1d7c..2beebf7 100755 --- a/widget/index.html +++ b/widget/index.html @@ -25,6 +25,7 @@ + diff --git a/widget/js/classes/topic.js b/widget/js/classes/topic.js index 87068a2..2a0981f 100644 --- a/widget/js/classes/topic.js +++ b/widget/js/classes/topic.js @@ -69,6 +69,7 @@ class Topic { getRowData() { return { + id: this.id, title: this.title, titleIndex: this.titleIndex, type: this.type, @@ -142,53 +143,23 @@ class Topic { } - delete(privacy) { + static delete(db, topicId) { return new Promise((resolve, reject) => { - if (!this.id) { + if (!topicId) { reject({ error: 'Missed Parameters', - message: 'You missed id parameter' + message: 'You missed id parameter', }); return; } - if (privacy === 'public') { - const filter = { - "$json.parentTopicId": this.id, - "_buildfire.index.date1": { - $type: "null" - }, + db.delete(topicId, 'topics', (err, result) => { + if (err) { + reject(err); + } else { + resolve(result); } - Topic.getTopics(privacy, filter, 1) - .then(topics => { - if (topics && topics.length > 0 && this.deletedBy) { - reject({ - error: 'Unauthorized', - message: this.title + ' group is not empty' - }); - return; - } - this.deletedOn = new Date(); - this.update(privacy) - .then(result => { - Helper.trackAction(Helper.EVENTS.TOPIC_DELETED); - resolve(result); - }) - .catch(err => { - reject(err); - }); - }); - } else { - this.deletedOn = new Date(); - this.update(privacy) - .then(result => { - Helper.trackAction(Helper.EVENTS.TOPIC_DELETED); - resolve(result); - }) - .catch(err => { - reject(err); - }) - } + }); }); }