| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 | 
							- import async from "async";
 
- import { isAdminRequired } from "./hooks";
 
- import moduleManager from "../../index";
 
- const DBModule = moduleManager.modules.db;
 
- const UtilsModule = moduleManager.modules.utils;
 
- const WSModule = moduleManager.modules.ws;
 
- const CacheModule = moduleManager.modules.cache;
 
- CacheModule.runJob("SUB", {
 
- 	channel: "dataRequest.resolve",
 
- 	cb: dataRequestId => {
 
- 		WSModule.runJob("EMIT_TO_ROOM", {
 
- 			room: "admin.users",
 
- 			args: ["event:admin.dataRequests.resolved", { data: { dataRequestId } }]
 
- 		});
 
- 	}
 
- });
 
- export default {
 
- 	/**
 
- 	 * Gets data requests, used in the admin users page by the AdvancedTable component
 
- 	 *
 
- 	 * @param {object} session - the session object automatically added by the websocket
 
- 	 * @param page - the page
 
- 	 * @param pageSize - the size per page
 
- 	 * @param properties - the properties to return for each data request
 
- 	 * @param sort - the sort object
 
- 	 * @param queries - the queries array
 
- 	 * @param operator - the operator for queries
 
- 	 * @param cb
 
- 	 */
 
- 	getData: isAdminRequired(async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
 
- 		const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
 
- 		async.waterfall(
 
- 			[
 
- 				// Creates pipeline array
 
- 				next => next(null, []),
 
- 				// Adds the match stage to aggregation pipeline, which is responsible for filtering
 
- 				(pipeline, next) => {
 
- 					let queryError;
 
- 					const newQueries = queries.flatMap(query => {
 
- 						const { data, filter, filterType } = query;
 
- 						const newQuery = {};
 
- 						if (filterType === "regex") {
 
- 							newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
 
- 						} else if (filterType === "contains") {
 
- 							newQuery[filter.property] = new RegExp(
 
- 								`${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`,
 
- 								"i"
 
- 							);
 
- 						} else if (filterType === "exact") {
 
- 							newQuery[filter.property] = data.toString();
 
- 						} else if (filterType === "datetimeBefore") {
 
- 							newQuery[filter.property] = { $lte: new Date(data) };
 
- 						} else if (filterType === "datetimeAfter") {
 
- 							newQuery[filter.property] = { $gte: new Date(data) };
 
- 						} else if (filterType === "numberLesserEqual") {
 
- 							newQuery[filter.property] = { $lte: Number(data) };
 
- 						} else if (filterType === "numberLesser") {
 
- 							newQuery[filter.property] = { $lt: Number(data) };
 
- 						} else if (filterType === "numberGreater") {
 
- 							newQuery[filter.property] = { $gt: Number(data) };
 
- 						} else if (filterType === "numberGreaterEqual") {
 
- 							newQuery[filter.property] = { $gte: Number(data) };
 
- 						} else if (filterType === "numberEquals") {
 
- 							newQuery[filter.property] = { $eq: Number(data) };
 
- 						} else if (filterType === "boolean") {
 
- 							newQuery[filter.property] = { $eq: !!data };
 
- 						}
 
- 						return newQuery;
 
- 					});
 
- 					if (queryError) next(queryError);
 
- 					const queryObject = {};
 
- 					if (newQueries.length > 0) {
 
- 						if (operator === "and") queryObject.$and = newQueries;
 
- 						else if (operator === "or") queryObject.$or = newQueries;
 
- 						else if (operator === "nor") queryObject.$nor = newQueries;
 
- 					}
 
- 					pipeline.push({ $match: queryObject });
 
- 					next(null, pipeline);
 
- 				},
 
- 				// Adds sort stage to aggregation pipeline if there is at least one column being sorted, responsible for sorting data
 
- 				(pipeline, next) => {
 
- 					const newSort = Object.fromEntries(
 
- 						Object.entries(sort).map(([property, direction]) => [
 
- 							property,
 
- 							direction === "ascending" ? 1 : -1
 
- 						])
 
- 					);
 
- 					if (Object.keys(newSort).length > 0) pipeline.push({ $sort: newSort });
 
- 					next(null, pipeline);
 
- 				},
 
- 				// Adds first project stage to aggregation pipeline, responsible for including only the requested properties
 
- 				(pipeline, next) => {
 
- 					pipeline.push({ $project: Object.fromEntries(properties.map(property => [property, 1])) });
 
- 					next(null, pipeline);
 
- 				},
 
- 				// Adds the facet stage to aggregation pipeline, responsible for returning a total document count, skipping and limitting the documents that will be returned
 
- 				(pipeline, next) => {
 
- 					pipeline.push({
 
- 						$facet: {
 
- 							count: [{ $count: "count" }],
 
- 							documents: [{ $skip: pageSize * (page - 1) }, { $limit: pageSize }]
 
- 						}
 
- 					});
 
- 					// console.dir(pipeline, { depth: 6 });
 
- 					next(null, pipeline);
 
- 				},
 
- 				// Executes the aggregation pipeline
 
- 				(pipeline, next) => {
 
- 					dataRequestModel.aggregate(pipeline).exec((err, result) => {
 
- 						// console.dir(err);
 
- 						// 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];
 
- 						const { documents } = result[0];
 
- 						// console.log(111, err, result, count, documents[0]);
 
- 						return next(null, count, documents);
 
- 					});
 
- 				}
 
- 			],
 
- 			async (err, count, dataRequests) => {
 
- 				if (err && err !== true) {
 
- 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 
- 					this.log("ERROR", "DATA_REQUESTS_GET_DATA", `Failed to get data from data requests. "${err}"`);
 
- 					return cb({ status: "error", message: err });
 
- 				}
 
- 				this.log("SUCCESS", "DATA_REQUESTS_GET_DATA", `Got data from data requests successfully.`);
 
- 				return cb({
 
- 					status: "success",
 
- 					message: "Successfully got data from data requests.",
 
- 					data: { data: dataRequests, count }
 
- 				});
 
- 			}
 
- 		);
 
- 	}),
 
- 	/**
 
- 	 * Resolves a data request
 
- 	 *
 
- 	 * @param {object} session - the session object automatically added by the websocket
 
- 	 * @param {object} dataRequestId - the id of the data request to resolve
 
- 	 * @param {Function} cb - gets called with the result
 
- 	 */
 
- 	resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
 
- 		const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
 
- 		async.waterfall(
 
- 			[
 
- 				next => {
 
- 					if (!dataRequestId || typeof dataRequestId !== "string")
 
- 						return next("Please provide a data request id.");
 
- 					return next();
 
- 				},
 
- 				next => {
 
- 					dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
 
- 						next(err)
 
- 					);
 
- 				}
 
- 			],
 
- 			async err => {
 
- 				if (err) {
 
- 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 
- 					this.log(
 
- 						"ERROR",
 
- 						"DATA_REQUESTS_RESOLVE",
 
- 						`Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
 
- 					);
 
- 					return cb({ status: "error", message: err });
 
- 				}
 
- 				CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
 
- 				this.log(
 
- 					"SUCCESS",
 
- 					"DATA_REQUESTS_RESOLVE",
 
- 					`Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
 
- 				);
 
- 				return cb({
 
- 					status: "success",
 
- 					message: "Successfully resolved data request."
 
- 				});
 
- 			}
 
- 		);
 
- 	})
 
- };
 
 
  |