Browse Source

refactor: added user hasPassword and punishment status property in getData and added it as a column

Kristian Vos 3 years ago
parent
commit
292a61aaa3

+ 26 - 1
backend/logic/actions/punishments.js

@@ -47,6 +47,31 @@ export default {
 				// Creates pipeline array
 				next => next(null, []),
 
+				// If a filter or property exists for status, add status property to all documents
+				(pipeline, next) => {
+					// Check if a filter with the status property exists
+					const statusFilterExists = queries.map(query => query.filter.property).indexOf("status") !== -1;
+					// Check if a property with the status property exists
+					const statusPropertyExists = properties.indexOf("status") !== -1;
+					// If no such filter or property exists, skip this function
+					if (!statusFilterExists && !statusPropertyExists) return next(null, pipeline);
+
+					// Adds status field, set to Inactive if active is false, otherwise it sets it to Inactive if expiresAt has already passed, Active if not
+					pipeline.push({
+						$addFields: {
+							status: {
+								$cond: [
+									{ $eq: ["$active", true] },
+									{ $cond: [{ $gt: [new Date(), "$expiresAt"] }, "Inactive", "Active"] },
+									"Inactive"
+								]
+							}
+						}
+					});
+
+					return next(null, pipeline);
+				},
+
 				// If a filter exists for value, add valueUsername property to all documents
 				(pipeline, next) => {
 					// Check if a filter with the value property exists
@@ -258,7 +283,7 @@ export default {
 				(pipeline, next) => {
 					punishmentModel.aggregate(pipeline).exec((err, result) => {
 						// console.dir(err);
-						// console.dir(result, { depth: 6 });
+						console.dir(result, { depth: 6 });
 						if (err) return next(err);
 						if (result[0].count.length === 0) return next(null, 0, []);
 						const { count } = result[0].count[0];

+ 23 - 1
backend/logic/actions/users.js

@@ -188,6 +188,28 @@ export default {
 				// Creates pipeline array
 				next => next(null, []),
 
+				// If a filter or property exists for hasPassword, add hasPassword property to all documents
+				(pipeline, next) => {
+					// Check if a filter with the hasPassword property exists
+					const hasPasswordFilterExists =
+						queries.map(query => query.filter.property).indexOf("hasPassword") !== -1;
+					// Check if a property with the hasPassword property exists
+					const hasPasswordPropertyExists = properties.indexOf("hasPassword") !== -1;
+					// If no such filter or property exists, skip this function
+					if (!hasPasswordFilterExists && !hasPasswordPropertyExists) return next(null, pipeline);
+
+					// Adds hasPassword field, set it to true if services.password.password is a string, false if not
+					pipeline.push({
+						$addFields: {
+							hasPassword: {
+								$cond: [{ $eq: [{ $type: "$services.password.password" }, "string"] }, true, false]
+							}
+						}
+					});
+
+					return next(null, pipeline);
+				},
+
 				// Adds the match stage to aggregation pipeline, which is responsible for filtering
 				(pipeline, next) => {
 					let queryError;
@@ -300,7 +322,7 @@ export default {
 				(pipeline, next) => {
 					userModel.aggregate(pipeline).exec((err, result) => {
 						// console.dir(err);
-						console.dir(result, { depth: 6 });
+						// console.dir(result, { depth: 6 });
 						if (err) return next(err);
 						if (result[0].count.length === 0) return next(null, 0, []);
 						const { count } = result[0].count[0];

+ 10 - 6
frontend/src/components/AdvancedTable.vue

@@ -531,11 +531,10 @@
 												.split('.')
 												.reduce(
 													(previous, current) =>
-														(previous &&
+														(previous !== null &&
 															previous[
 																current
-															]) ||
-														null,
+															]) !== null || null,
 													item
 												)
 										)
@@ -998,7 +997,9 @@ export default {
 							.split(".")
 							.reduce(
 								(previous, current) =>
-									(previous && previous[current]) || null,
+									(previous !== null &&
+										previous[current] !== null) ||
+									null,
 								res.data
 							)
 					);
@@ -1006,7 +1007,8 @@ export default {
 					.split(".")
 					.reduce(
 						(previous, current) =>
-							(previous && previous[current]) || null,
+							(previous !== null && previous[current] !== null) ||
+							null,
 						res.data
 					);
 				this.updateData(index, row);
@@ -1020,7 +1022,9 @@ export default {
 							.split(".")
 							.reduce(
 								(previous, current) =>
-									(previous && previous[current]) || null,
+									(previous !== null &&
+										previous[current] !== null) ||
+									null,
 								res.data
 							)
 					);

+ 2 - 8
frontend/src/pages/Admin/tabs/Punishments.vue

@@ -29,13 +29,7 @@
 					</div>
 				</template>
 				<template #column-status="slotProps">
-					<span>{{
-						slotProps.item.active &&
-						new Date(slotProps.item.expiresAt).getTime() >
-							Date.now()
-							? "Active"
-							: "Inactive"
-					}}</span>
+					<span>{{ slotProps.item.status }}</span>
 				</template>
 				<template #column-type="slotProps">
 					<span
@@ -163,7 +157,7 @@ export default {
 				{
 					name: "status",
 					displayName: "Status",
-					properties: ["active", "expiresAt"],
+					properties: ["status", "active", "expiresAt"],
 					sortable: false,
 					defaultWidth: 150
 				},

+ 8 - 8
frontend/src/pages/Admin/tabs/Users.vue

@@ -123,11 +123,11 @@
 						>{{ slotProps.item.services.github.id }}</span
 					>
 				</template>
-				<!-- <template #column-hasPassword="slotProps">
+				<template #column-hasPassword="slotProps">
 					<span :title="slotProps.item.hasPassword">{{
 						slotProps.item.hasPassword
 					}}</span>
-				</template> -->
+				</template>
 				<template #column-role="slotProps">
 					<span :title="slotProps.item.role">{{
 						slotProps.item.role
@@ -309,12 +309,12 @@ export default {
 						minWidth: 115,
 						defaultWidth: 115
 					},
-					// {
-					// 	name: "hasPassword",
-					// 	displayName: "Has Password",
-					// 	properties: ["hasPassword"],
-					// 	sortProperty: "hasPassword"
-					// }
+					{
+						name: "hasPassword",
+						displayName: "Has Password",
+						properties: ["hasPassword"],
+						sortProperty: "hasPassword"
+					},
 					{
 						name: "role",
 						displayName: "Role",