Browse Source

feat(ViewReport): Ability to delete report

Owen Diffey 3 years ago
parent
commit
3e75f6573f

+ 57 - 0
backend/logic/actions/reports.js

@@ -58,6 +58,15 @@ CacheModule.runJob("SUB", {
 	}
 });
 
+CacheModule.runJob("SUB", {
+	channel: "report.remove",
+	cb: reportId =>
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: ["admin.reports", `view-report.${reportId}`],
+			args: ["event:admin.report.removed", { data: { reportId } }]
+		})
+});
+
 export default {
 	/**
 	 * Gets reports, used in the admin reports page by the AdvancedTable component
@@ -540,5 +549,53 @@ export default {
 				});
 			}
 		);
+	}),
+
+	/**
+	 * Removes a report
+	 *
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {object} reportId - the id of the report item we want to remove
+	 * @param {Function} cb - gets called with the result
+	 */
+	remove: isAdminRequired(async function remove(session, reportId, cb) {
+		const reportModel = await DBModule.runJob("GET_MODEL", { modelName: "report" }, this);
+
+		async.waterfall(
+			[
+				next => {
+					if (!reportId) return next("Please provide a report item id to remove.");
+					return next();
+				},
+
+				next => {
+					reportModel.deleteOne({ _id: reportId }, err => next(err));
+				}
+			],
+			async err => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"REPORT_REMOVE",
+						`Removing report "${reportId}" failed for user "${session.userId}". "${err}"`
+					);
+					return cb({ status: "error", message: err });
+				}
+
+				CacheModule.runJob("PUB", { channel: "report.remove", value: reportId });
+
+				this.log(
+					"SUCCESS",
+					"REPORT_REMOVE",
+					`Removing report "${reportId}" successful by user "${session.userId}".`
+				);
+
+				return cb({
+					status: "success",
+					message: "Successfully removed report"
+				});
+			}
+		);
 	})
 };

+ 10 - 0
frontend/src/api/admin/reports.js

@@ -13,5 +13,15 @@ export default {
 				return reject(new Error(res.message));
 			});
 		});
+	},
+	remove(reportId) {
+		return new Promise((resolve, reject) => {
+			ws.socket.dispatch("reports.remove", reportId, res => {
+				new Toast(res.message);
+				if (res.status === "success")
+					return resolve({ status: "success" });
+				return reject(new Error(res.message));
+			});
+		});
 	}
 };

+ 31 - 2
frontend/src/components/modals/ViewReport.vue

@@ -93,6 +93,17 @@
 				</i>
 				Resolve
 			</button>
+			<div class="right">
+				<quick-confirm @confirm="remove()">
+					<button
+						class="button is-danger material-icons icon-with-button"
+						content="Delete Report"
+						v-tippy
+					>
+						delete_forever
+					</button>
+				</quick-confirm>
+			</div>
 		</template>
 	</modal>
 </template>
@@ -105,9 +116,10 @@ import ws from "@/ws";
 import Modal from "@/components/Modal.vue";
 import SongItem from "@/components/SongItem.vue";
 import ReportInfoItem from "@/components/ReportInfoItem.vue";
+import QuickConfirm from "@/components/QuickConfirm.vue";
 
 export default {
-	components: { Modal, SongItem, ReportInfoItem },
+	components: { Modal, SongItem, ReportInfoItem, QuickConfirm },
 	props: {
 		sector: { type: String, default: "admin" }
 	},
@@ -142,6 +154,12 @@ export default {
 			{ modal: "viewReport" }
 		);
 
+		this.socket.on(
+			"event:admin.report.removed",
+			() => this.closeModal("viewReport"),
+			{ modal: "viewReport" }
+		);
+
 		this.socket.on(
 			"event:admin.report.issue.toggled",
 			res => {
@@ -199,6 +217,13 @@ export default {
 				})
 				.catch(err => new Toast(err.message));
 		},
+		remove() {
+			return this.removeReport(this.reportId)
+				.then(res => {
+					if (res.status === "success") this.closeModal("viewReport");
+				})
+				.catch(err => new Toast(err.message));
+		},
 		toggleIssue(issueId) {
 			this.socket.dispatch(
 				"reports.toggleIssue",
@@ -213,7 +238,11 @@ export default {
 			this.editSong({ songId: this.report.song._id });
 			this.openModal("editSong");
 		},
-		...mapActions("admin/reports", ["indexReports", "resolveReport"]),
+		...mapActions("admin/reports", [
+			"indexReports",
+			"resolveReport",
+			"removeReport"
+		]),
 		...mapActions("modals/editSong", ["editSong"]),
 		...mapActions("modalVisibility", ["closeModal", "openModal"])
 	}

+ 1 - 1
frontend/src/pages/Admin/Songs/Reports.vue

@@ -229,7 +229,7 @@ export default {
 			events: {
 				adminRoom: "reports",
 				removed: {
-					event: "admin.report.resolved",
+					event: "admin.report.removed",
 					id: "reportId"
 				}
 			}

+ 8 - 0
frontend/src/store/modules/admin.js

@@ -30,6 +30,14 @@ const modules = {
 						.resolve(reportId)
 						.then(res => resolve(res))
 						.catch(err => reject(new Error(err.message)));
+				}),
+			/* eslint-disable-next-line no-unused-vars */
+			removeReport: ({ commit }, reportId) =>
+				new Promise((resolve, reject) => {
+					admin.reports
+						.remove(reportId)
+						.then(res => resolve(res))
+						.catch(err => reject(new Error(err.message)));
 				})
 		},
 		mutations: {}