Files
Tempest/imports/api/data-collection.js

164 lines
5.1 KiB
JavaScript
Raw Normal View History

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import { MongoClient } from 'mongodb';
import {Assets} from "/imports/api/assets";
import {Students} from "/imports/api/students";
import {Staff} from "/imports/api/staff";
import {AssetTypes} from "/imports/api/asset-types";
//import {Roles} from 'alanning/roles';
// console.log("Setting Up Data Collection...")
//export const Records = new Mongo.Collection('records');
let client;
let database;
let dataCollection;
if (Meteor.isServer) {
// let uri = process.env.MONGO_URL2;
// uri = "mongodb://localhost:27017";
//
// //client = new MongoClient(uri);
// MongoClient.connect(uri, (err, c) => {
// client = c;
// database = client.db("avusd-data-collection");
// dataCollection = database.collection("records");
// dataCollection.find({deviceId: "1e3e99ef-adf4-4aa2-8784-205bc60f0ce3"}).toArray((e, a) => {
// if(e) console.log(e);
// else console.log("Found " + a.length + " records.");
// })
// });
// let results = Meteor.Records.find({deviceId: "1e3e99ef-adf4-4aa2-8784-205bc60f0ce3"}).fetch();
// console.log(results);
}
if (Meteor.isServer) {
Meteor.methods({
/**
* Collects Chromebook history given one of the possible parameters.
* @param params An object with a single attribute. The attribute must be one of: deviceId, serial, email. It will find all Chromebook data that starts with the given attribute value.
* @returns {any} Array of Chromebook data objects.
*/
'DataCollection.chromebookData'(params) {
if(Roles.userIsInRole(Meteor.userId(), "laptop-management", {anyScope:true})) {
let query = {};
if(params.studentId) check(params.studentId, String)
if(params.staffId) check(params.staffId, String)
// For asset ID's, we need to get the serial from the asset collection first.
if(params.assetId) {
let asset = Assets.findOne({assetId : params.assetId});
if(asset) {
params.serial = asset.serial;
params.regex = false;
}
}
// console.log('chromebook data')
// console.log(params)
if (params.deviceId) query.deviceId = params.regex ? {
$regex: params.deviceId,
$options: "i"
} : params.deviceId;
else if (params.serial) query.serial = params.regex ? {
$regex: params.serial,
$options: "i"
} : params.serial;
else if(params.studentId) {
const student = Students.findOne({_id: params.studentId})
// console.log(student)
if(student) query.email = student.email;
else query = undefined
}
else if(params.staffId) {
const staff = Staff.findOne({_id: params.staffId})
if(staff) query.email = staff.email;
else query = undefined
}
else if (params.email) query.email = params.regex ? {
$regex: params.email,
$options: "i"
} : params.email;
else if (params.date) { //Assume that date is a number. Finds all Chromebook Data with the last check in time greater than or equal to the given date.
query.endTime = {'$gte': params.date}
}
else {
query = undefined;
}
// console.log("query")
// console.log(query)
if(query) {
// console.log("Collecting Chromebook Data: ");
// console.log(query);
//Sort by the last time the record was updated from most to least recent.
let result = Meteor.Records.find(query, {sort: {endTime: -1}}).fetch();
// console.log("Found: ");
// console.log(result);
//Add some additional data to the records.
for (let next of result) {
if (next.serial) {
next.asset = Assets.findOne({serial: next.serial});
}
if (next.email) {
next.person = Students.findOne({email: next.email});
if (!next.person) next.person = Staff.findOne({email: next.email});
}
if (next.asset) {
2022-09-15 09:13:30 -07:00
next.assetType = AssetTypes.findOne({_id: next.asset.assetTypeId})
if (next.asset.assigneeId) {
next.assignedTo = next.asset.assigneeType === "Student" ? Students.findOne({_id: next.asset.assigneeId}) : Staff.findOne({_id: next.asset.assigneeId})
}
}
}
// console.log('returning')
// console.log(result)
return result;
} else return null;
}
else return null;
}
// 'tasks.setChecked'(taskId, setChecked) {
// check(taskId, String);
// check(setChecked, Boolean);
//
// const task = Tasks.findOne(taskId);
// if (task.private && task.owner !== this.userId) {
// // If the task is private, make sure only the owner can check it off
// throw new Meteor.Error('not-authorized');
// }
//
// Tasks.update(taskId, { $set: { checked: setChecked } });
// },
// 'tasks.setPrivate'(taskId, setToPrivate) {
// check(taskId, String);
// check(setToPrivate, Boolean);
//
// const task = Tasks.findOne(taskId);
//
// // Make sure only the task owner can make a task private
// if (task.owner !== this.userId) {
// throw new Meteor.Error('not-authorized');
// }
//
// Tasks.update(taskId, { $set: { private: setToPrivate } });
// },
});
}
// console.log("Data Collection setup.")