Skip to content

Commit

Permalink
Fix(deleting topics): fix deleting topics in when privicy is both
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahemomari committed Feb 19, 2024
1 parent 7d90d63 commit 218de16
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
39 changes: 39 additions & 0 deletions widget/app.controller.js
Original file line number Diff line number Diff line change
@@ -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;
},
};
2 changes: 1 addition & 1 deletion widget/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
1 change: 1 addition & 0 deletions widget/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script src="./js/authManager.js"></script>
<script src="js/shared/strings.js"></script>
<script src="js/shared/stringsConfig.js"></script>
<script src="./app.controller.js"></script>

<!-- CSS -->

Expand Down
49 changes: 10 additions & 39 deletions widget/js/classes/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Topic {

getRowData() {
return {
id: this.id,
title: this.title,
titleIndex: this.titleIndex,
type: this.type,
Expand Down Expand Up @@ -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);
})
}
});
});
}

Expand Down

0 comments on commit 218de16

Please sign in to comment.