From afd860660be0c6db550cc7aa32040282b095ac0b Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Mon, 9 Oct 2017 13:17:18 +0200
Subject: [PATCH] Adjust CouchDB view documents for v3 entities

---
 src/main/resources/couchdb/Answer.design.js   | 18 ++++++-------
 .../resources/couchdb/Attachment.design.js    |  2 +-
 src/main/resources/couchdb/Comment.design.js  | 10 +++----
 src/main/resources/couchdb/Content.design.js  |  4 +--
 src/main/resources/couchdb/LoggedIn.design.js | 27 -------------------
 src/main/resources/couchdb/Motd.design.js     |  6 ++---
 src/main/resources/couchdb/MotdList.design.js | 13 ---------
 .../{Session.design.js => Room.design.js}     | 12 ++++-----
 ...DbUser.design.js => UserProfile.design.js} |  6 ++---
 .../couchdb/lerning_progress.design.js        |  4 +--
 .../resources/couchdb/statistics.design.js    | 14 +++++-----
 11 files changed, 38 insertions(+), 78 deletions(-)
 delete mode 100644 src/main/resources/couchdb/LoggedIn.design.js
 delete mode 100644 src/main/resources/couchdb/MotdList.design.js
 rename src/main/resources/couchdb/{Session.design.js => Room.design.js} (72%)
 rename src/main/resources/couchdb/{DbUser.design.js => UserProfile.design.js} (61%)

diff --git a/src/main/resources/couchdb/Answer.design.js b/src/main/resources/couchdb/Answer.design.js
index 44d0a92f3..b673dd79e 100644
--- a/src/main/resources/couchdb/Answer.design.js
+++ b/src/main/resources/couchdb/Answer.design.js
@@ -4,14 +4,14 @@ var designDoc = {
 	"views": {
 		"by_questionid": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit(doc.questionId, {_rev: doc._rev});
 				}
 			}
 		},
 		"by_questionid_piround_text_subject": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.questionId, doc.piRound, doc.abstention, doc.answerText, doc.answerSubject, doc.successfulFreeTextAnswer], {_rev: doc._rev});
 				}
 			},
@@ -19,21 +19,21 @@ var designDoc = {
 		},
 		"by_questionid_timestamp": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.questionId, doc.timestamp], {_rev: doc._rev});
 				}
 			}
 		},
 		"by_questionid_user_piround": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.questionId, doc.user, doc.piRound], {_rev: doc._rev});
 				}
 			}
 		},
 		"by_sessionid": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit(doc.sessionId, {_rev: doc._rev});
 				}
 			},
@@ -41,7 +41,7 @@ var designDoc = {
 		},
 		"by_sessionid_variant": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.sessionId, doc.questionVariant], {_rev: doc._rev});
 				}
 			},
@@ -49,21 +49,21 @@ var designDoc = {
 		},
 		"by_user_sessionid": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.user, doc.sessionId], {_rev: doc._rev});
 				}
 			}
 		},
 		"questionid_by_user_sessionid_variant": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.user, doc.sessionId, doc.questionVariant], doc.questionId);
 				}
 			}
 		},
 		"questionid_piround_by_user_sessionid_variant": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit([doc.user, doc.sessionId, doc.questionVariant], [doc.questionId, doc.piRound]);
 				}
 			}
diff --git a/src/main/resources/couchdb/Attachment.design.js b/src/main/resources/couchdb/Attachment.design.js
index e19248f49..38ae80143 100644
--- a/src/main/resources/couchdb/Attachment.design.js
+++ b/src/main/resources/couchdb/Attachment.design.js
@@ -4,7 +4,7 @@ var designDoc = {
 	"views": {
 		"by_creatorid": {
 			"map": function (doc) {
-				if (doc.type === "attachment") {
+				if (doc.type === "Attachment") {
 					emit(doc.creatorId, {_rev: doc._rev});
 				}
 			},
diff --git a/src/main/resources/couchdb/Comment.design.js b/src/main/resources/couchdb/Comment.design.js
index c407a2eb5..5dcde26d1 100644
--- a/src/main/resources/couchdb/Comment.design.js
+++ b/src/main/resources/couchdb/Comment.design.js
@@ -4,7 +4,7 @@ var designDoc = {
 	"views": {
 		"by_sessionid": {
 			"map": function (doc) {
-				if (doc.type === "interposed_question") {
+				if (doc.type === "Comment") {
 					emit(doc.sessionId, {_rev: doc._rev});
 				}
 			},
@@ -12,7 +12,7 @@ var designDoc = {
 		},
 		"by_sessionid_creator_read": {
 			"map": function (doc) {
-				if (doc.type === "interposed_question") {
+				if (doc.type === "Comment") {
 					emit([doc.sessionId, doc.creator, doc.read], {_rev: doc._rev})
 				}
 			},
@@ -20,14 +20,14 @@ var designDoc = {
 		},
 		"by_sessionid_creator_timestamp": {
 			"map": function (doc) {
-				if (doc.type === "interposed_question") {
+				if (doc.type === "Comment") {
 					emit([doc.sessionId, doc.creator, doc.timestamp], {_rev: doc._rev});
 				}
 			}
 		},
 		"by_sessionid_read": {
 			"map": function (doc) {
-				if (doc.type === "interposed_question") {
+				if (doc.type === "Comment") {
 					emit([doc.sessionId, doc.read], {_rev: doc._rev});
 				}
 			},
@@ -35,7 +35,7 @@ var designDoc = {
 		},
 		"by_sessionid_timestamp": {
 			"map": function (doc) {
-				if (doc.type === "interposed_question") {
+				if (doc.type === "Comment") {
 					emit([doc.sessionId, doc.timestamp], {_rev: doc._rev});
 				}
 			}
diff --git a/src/main/resources/couchdb/Content.design.js b/src/main/resources/couchdb/Content.design.js
index cb3f2f982..6a827d2ac 100644
--- a/src/main/resources/couchdb/Content.design.js
+++ b/src/main/resources/couchdb/Content.design.js
@@ -4,7 +4,7 @@ var designDoc = {
 	"views": {
 		"by_sessionid": {
 			"map": function (doc) {
-				if (doc.type === "skill_question") {
+				if (doc.type === "Content") {
 					emit(doc.sessionId, {_rev: doc._rev});
 				}
 			},
@@ -12,7 +12,7 @@ var designDoc = {
 		},
 		"by_sessionid_variant_active": {
 			"map": function (doc) {
-				if (doc.type === "skill_question") {
+				if (doc.type === "Content") {
 					emit([doc.sessionId, doc.questionVariant, doc.active, doc.subject, doc.text.substr(0, 16)], {_rev: doc._rev});
 				}
 			},
diff --git a/src/main/resources/couchdb/LoggedIn.design.js b/src/main/resources/couchdb/LoggedIn.design.js
deleted file mode 100644
index aadec2d1e..000000000
--- a/src/main/resources/couchdb/LoggedIn.design.js
+++ /dev/null
@@ -1,27 +0,0 @@
-var designDoc = {
-	"_id": "_design/LoggedIn",
-	"language": "javascript",
-	"views": {
-		"all": {
-			"map": function (doc) {
-				if (doc.type === "logged_in"){
-					emit(doc.user, doc);
-				}
-			}
-		},
-		"by_last_activity_for_guests": {
-			"map": function (doc) {
-				if (doc.type === "logged_in" && doc.user.indexOf("Guest") === 0) {
-					emit(doc.timestamp || 0, {_rev: doc._rev});
-				}
-			}
-		},
-		"visited_sessions_by_user": {
-			"map": function (doc) {
-				if (doc.type === "logged_in") {
-					emit(doc.user, doc.visitedSessions);
-				}
-			}
-		}
-	}
-};
diff --git a/src/main/resources/couchdb/Motd.design.js b/src/main/resources/couchdb/Motd.design.js
index 8e932510d..f7aa01b86 100644
--- a/src/main/resources/couchdb/Motd.design.js
+++ b/src/main/resources/couchdb/Motd.design.js
@@ -4,21 +4,21 @@ var designDoc = {
 	"views": {
 		"by_audience_for_global": {
 			"map": function (doc) {
-				if (doc.type === "motd" && doc.audience !== "session") {
+				if (doc.type === "Motd" && doc.audience !== "session") {
 					emit(doc.audience, {_rev: doc._rev});
 				}
 			}
 		},
 		"by_motdkey": {
 			"map": function (doc) {
-				if (doc.type === "motd") {
+				if (doc.type === "Motd") {
 					emit(doc.motdkey, {_rev: doc._rev});
 				}
 			}
 		},
 		"by_sessionkey": {
 			"map": function (doc) {
-				if (doc.type === "motd" && doc.audience === "session") {
+				if (doc.type === "Motd" && doc.audience === "session") {
 					emit(doc.sessionkey, {_rev: doc._rev});
 				}
 			}
diff --git a/src/main/resources/couchdb/MotdList.design.js b/src/main/resources/couchdb/MotdList.design.js
deleted file mode 100644
index e7ddbe762..000000000
--- a/src/main/resources/couchdb/MotdList.design.js
+++ /dev/null
@@ -1,13 +0,0 @@
-var designDoc = {
-	"_id": "_design/MotdList",
-	"language": "javascript",
-	"views": {
-		"doc_by_username": {
-			"map": function (doc) {
-				if (doc.type === "motdlist") {
-					emit(doc.username, doc);
-				}
-			}
-		}
-	}
-};
diff --git a/src/main/resources/couchdb/Session.design.js b/src/main/resources/couchdb/Room.design.js
similarity index 72%
rename from src/main/resources/couchdb/Session.design.js
rename to src/main/resources/couchdb/Room.design.js
index 790e3af6a..8f29c8607 100644
--- a/src/main/resources/couchdb/Session.design.js
+++ b/src/main/resources/couchdb/Room.design.js
@@ -1,31 +1,31 @@
 var designDoc = {
-	"_id": "_design/Session",
+	"_id": "_design/Room",
 	"language": "javascript",
 	"views": {
 		"by_courseid": {
 			"map": function (doc) {
-				if (doc.type === "session" && doc.courseId  && doc.sessionType !== "public_pool") {
+				if (doc.type === "Room" && doc.courseId  && doc.sessionType !== "public_pool") {
 					emit(doc.courseId, {_rev: doc._rev});
 				}
 			}
 		},
 		"by_keyword": {
 			"map": function (doc) {
-				if (doc.type === "session") {
+				if (doc.type === "Room") {
 					emit(doc.keyword, {_rev: doc._rev});
 				}
 			}
 		},
 		"by_lastactivity_for_guests": {
 			"map": function (doc) {
-				if (doc.type === "session" && doc.sessionType !== "public_pool" && doc.creator.indexOf("Guest") === 0) {
+				if (doc.type === "Room" && doc.sessionType !== "public_pool" && doc.creator.indexOf("Guest") === 0) {
 					emit(doc.lastOwnerActivity || doc.creationTime, {_rev: doc._rev});
 				}
 			}
 		},
 		"partial_by_sessiontype_creator_name": {
 			"map": function (doc) {
-				if (doc.type === "session") {
+				if (doc.type === "Room") {
 					emit([doc.sessionType, doc.creator, doc.name], {
 						shortName: doc.shortName,
 						keyword: doc.keyword,
@@ -38,7 +38,7 @@ var designDoc = {
 		},
 		"partial_by_subject_name_for_publicpool": {
 			"map": function (doc) {
-				if (doc.type === "session" && doc.sessiontype === "public_pool") {
+				if (doc.type === "Room" && doc.sessiontype === "public_pool") {
 					emit([doc.ppSubject, doc.name], {
 						ppSubject: doc.ppSubject,
 						name: doc.name,
diff --git a/src/main/resources/couchdb/DbUser.design.js b/src/main/resources/couchdb/UserProfile.design.js
similarity index 61%
rename from src/main/resources/couchdb/DbUser.design.js
rename to src/main/resources/couchdb/UserProfile.design.js
index aa63058d1..07f37c3c2 100644
--- a/src/main/resources/couchdb/DbUser.design.js
+++ b/src/main/resources/couchdb/UserProfile.design.js
@@ -1,17 +1,17 @@
 var designDoc = {
-	"_id": "_design/DbUser",
+	"_id": "_design/UserProfile",
 	"language": "javascript",
 	"views": {
 		"by_creation_for_inactive": {
 			"map": function (doc) {
-				if (doc.type === "userdetails" && doc.activationKey) {
+				if (doc.type === "UserProfile" && doc.activationKey) {
 					emit(doc.creation, {_rev: doc._rev});
 				}
 			}
 		},
 		"by_username": {
 			"map": function (doc) {
-				if (doc.type === "userdetails") emit(doc.username, {_rev: doc._rev});
+				if (doc.type === "UserProfile") emit(doc.username, {_rev: doc._rev});
 			}
 		}
 	}
diff --git a/src/main/resources/couchdb/lerning_progress.design.js b/src/main/resources/couchdb/lerning_progress.design.js
index 5ae5ba844..716c6f78d 100644
--- a/src/main/resources/couchdb/lerning_progress.design.js
+++ b/src/main/resources/couchdb/lerning_progress.design.js
@@ -5,7 +5,7 @@ var designDoc = {
 		"question_value_achieved_for_user": {
 			"comment": "This view returns the points users scored for answered questions.",
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer" && !doc.abstention) {
+				if (doc.type === "Answer" && !doc.abstention) {
 					/* The 'questionValue' contains the points scored with this answer,
 					 * and this could be negative if a wrong answer was given.
 					 * However, we do not want negative values, so we set the lower bound to 0.*/
@@ -28,7 +28,7 @@ var designDoc = {
 				 * 1) On any single choice question, the value is the maximum of all possibleAnswer values.
 				 * 2) On a multiple choice question, we add up all positive values. */
 				 var value = 0, answers = [], positiveAnswers = [], score = 0;
-				 if (doc.type === "skill_question" && ["school", "flashcard"].indexOf(doc.questionType) === -1) {
+				 if (doc.type === "Content" && ["school", "flashcard"].indexOf(doc.questionType) === -1) {
 				 	if ("freetext" === doc.questionType && !doc.fixedAnswer) { return; }
 					answers = doc.possibleAnswers.map(function (answer) { return answer.value || 0; });
 					/* find the maximum value */
diff --git a/src/main/resources/couchdb/statistics.design.js b/src/main/resources/couchdb/statistics.design.js
index c9004fd54..0e33e1499 100644
--- a/src/main/resources/couchdb/statistics.design.js
+++ b/src/main/resources/couchdb/statistics.design.js
@@ -4,7 +4,7 @@ var designDoc = {
 	"views": {
 		"active_student_users": {
 			"map": function (doc) {
-				if (doc.type === "skill_question_answer") {
+				if (doc.type === "Answer") {
 					emit(doc.user, 1);
 				}
 			},
@@ -13,14 +13,14 @@ var designDoc = {
 		"statistics": {
 			"map": function (doc) {
 				switch (doc.type) {
-				case "session":
+				case "Room":
 					if (doc.active) {
 						emit("openSessions", 1);
 					} else {
 						emit("closedSessions", 1);
 					}
 					break;
-				case "skill_question":
+				case "Content":
 					if (doc.questionType === "flashcard") {
 						emit("flashcards", 1);
 					} else {
@@ -34,13 +34,13 @@ var designDoc = {
 						}
 					}
 					break;
-				case "skill_question_answer":
+				case "Answer":
 					emit("answers", 1);
 					break;
-				case "interposed_question":
+				case "Comment":
 					emit ("interposedQuestions", 1);
 					break;
-				case "log":
+				case "LogEntry":
 					if (doc.event === "delete") {
 						switch (doc.payload.type) {
 						case "session":
@@ -67,7 +67,7 @@ var designDoc = {
 		},
 		"unique_session_creators": {
 			"map": function (doc) {
-				if (doc.type === "session") {
+				if (doc.type === "Room") {
 					emit(doc.creator, 1);
 				}
 			},
-- 
GitLab