2022-09-07 08:58:00 -07:00
|
|
|
import {Mongo} from "meteor/mongo";
|
2022-09-09 08:10:19 -07:00
|
|
|
import {Meteor} from "meteor/meteor";
|
|
|
|
|
import {check} from "meteor/check";
|
|
|
|
|
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";
|
2022-09-07 08:58:00 -07:00
|
|
|
|
|
|
|
|
export const AssetAssignmentHistory = new Mongo.Collection('assetAssignmentHistory');
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Maintains a historical record of asset assignments.
|
|
|
|
|
assetKey: The MongoID of the asset. AssetID's could be reused, so this prevents that eventuality from messing up historical records.
|
|
|
|
|
assetId: The asset's assigned ID (not a MongoID).
|
|
|
|
|
serial: The asset's serial number (part of the device, or defined by the manufacturer). In some cases this may be a partial serial. It is not a unique identifier, but should be mostly unique. This might be undefined if a serial was never provided for the original asset.
|
|
|
|
|
assetTypeName: The name of the asset type, or "UNK" if one could not be found (shouldn't happen). This is stored because these records could be kept longer than the assets in the system.
|
|
|
|
|
assigneeType: One of 'Student' or 'Staff'.
|
|
|
|
|
assigneeId: The MongoID of the student or staff the asset was assigned to.
|
|
|
|
|
startDate: The date/time of the assignment.
|
|
|
|
|
endDate: The date/time of the unassignment.
|
|
|
|
|
comment: A text block detailing the reason for the unassignment of the device. Eg: 'Broke Screen' or 'End of year' or 'Not charging' or 'Needs replacement'.
|
|
|
|
|
startCondition: One of the condition options: [New, Like New, Good, Okay, Damaged] (see assets.unassign for details).
|
|
|
|
|
endCondition: One of the condition options: [New, Like New, Good, Okay, Damaged] (see assets.unassign for details).
|
|
|
|
|
startConditionDetails: An optional text block for details on the condition.
|
|
|
|
|
endConditionDetails: An optional text block for details on the condition.
|
2022-09-09 08:10:19 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
|
Meteor.methods({
|
|
|
|
|
/**
|
|
|
|
|
* Collects historical data on asset assignments.
|
2022-09-12 18:19:57 -07:00
|
|
|
* @param params An object with a single attribute. The attribute must be one of: email, deviceId, assetId, serial, staffId, or studentId. It will find all Assignment data for the given attribute value.
|
2022-09-09 08:10:19 -07:00
|
|
|
* @returns {any} Array of Asset Assignment History objects.
|
|
|
|
|
*/
|
2025-09-25 09:31:02 -07:00
|
|
|
async 'AssetAssignmentHistory.get'(params) {
|
2023-07-30 14:11:12 -07:00
|
|
|
let result
|
|
|
|
|
|
2022-09-09 08:10:19 -07:00
|
|
|
if(Roles.userIsInRole(Meteor.userId(), "laptop-management", {anyScope:true})) {
|
2023-07-30 14:11:12 -07:00
|
|
|
let query = {}
|
|
|
|
|
let person
|
|
|
|
|
let asset
|
|
|
|
|
let assetType
|
|
|
|
|
|
|
|
|
|
// console.log("AssetAssignmentHistory: ")
|
|
|
|
|
// console.log(params)
|
2022-09-09 08:10:19 -07:00
|
|
|
|
|
|
|
|
if(params.studentId) check(params.studentId, String)
|
|
|
|
|
if(params.staffId) check(params.staffId, String)
|
|
|
|
|
if(params.assetId) check(params.assetId, String)
|
|
|
|
|
if(params.serial) check(params.serial, String)
|
2022-09-12 18:19:57 -07:00
|
|
|
if(params.deviceId) check(params.deviceId, String)
|
|
|
|
|
if(params.email) check(params.email, String)
|
|
|
|
|
|
|
|
|
|
if(params.email) {
|
2025-09-25 09:31:02 -07:00
|
|
|
let person = await Students.findOneAsync({email: params.email})
|
2022-09-12 18:19:57 -07:00
|
|
|
|
|
|
|
|
if(person) params.studentId = person._id;
|
|
|
|
|
else {
|
2025-09-25 09:31:02 -07:00
|
|
|
person = await Staff.findOneAsync({email: params.email})
|
2022-09-12 18:19:57 -07:00
|
|
|
|
|
|
|
|
if(person) params.staffId = person._id;
|
|
|
|
|
// else throw new Meteor.Error("Could not find a student or staff member with the given email.")
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-09 08:10:19 -07:00
|
|
|
|
2023-07-30 14:11:12 -07:00
|
|
|
if(params.serial || params.assetId || params.deviceId) {
|
|
|
|
|
if(params.serial) query.serial = params.serial;
|
|
|
|
|
else if(params.assetId) query.assetId = params.assetId;
|
|
|
|
|
else if(params.deviceId) query.deviceId = params.deviceId;
|
|
|
|
|
|
2025-09-25 09:31:02 -07:00
|
|
|
asset = await Assets.findOneAsync(query)
|
|
|
|
|
if(asset) assetType = await AssetTypes.findOneAsync({_id: asset.assetTypeId})
|
2022-09-09 08:10:19 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-30 14:11:12 -07:00
|
|
|
if(params.studentId) {
|
|
|
|
|
query.assigneeId = params.studentId
|
|
|
|
|
query.assigneeType = "Student"
|
|
|
|
|
}
|
|
|
|
|
else if(params.staffId) {
|
|
|
|
|
query.assigneeId = params.staffId
|
|
|
|
|
query.assigneeType = "Staff"
|
2022-09-12 18:19:57 -07:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-30 14:11:12 -07:00
|
|
|
query = undefined;
|
2022-09-12 18:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-25 09:31:02 -07:00
|
|
|
person = query.assigneeType === "Student" ? await Students.findOneAsync({id: query.assigneeId}) : await Staff.findOneAsync({id: query.assigneeId})
|
2023-07-30 14:11:12 -07:00
|
|
|
}
|
2022-09-09 08:10:19 -07:00
|
|
|
|
2023-07-30 14:11:12 -07:00
|
|
|
if(query) {
|
|
|
|
|
//Sort by the last time the record was updated from most to least recent.
|
2025-09-25 09:31:02 -07:00
|
|
|
result = await AssetAssignmentHistory.find(query, {sort: {endDate: -1}}).fetchAsync();
|
2023-07-30 14:11:12 -07:00
|
|
|
|
|
|
|
|
//Expand the assignee, asset, and asset type data.
|
2022-09-09 08:10:19 -07:00
|
|
|
for(let next of result) {
|
2023-07-30 14:11:12 -07:00
|
|
|
if(person) next.assignee = person
|
2025-09-25 09:31:02 -07:00
|
|
|
else next.assignee = next.assigneeType === "Student" ? await Students.findOneAsync({_id: next.assigneeId}) : await Staff.findOneAsync({_id: next.assigneeId})
|
2023-07-30 14:11:12 -07:00
|
|
|
|
|
|
|
|
if(asset) {
|
|
|
|
|
next.asset = asset
|
|
|
|
|
next.assetType = assetType
|
2022-09-09 08:10:19 -07:00
|
|
|
}
|
2023-07-30 14:11:12 -07:00
|
|
|
else {
|
2025-09-25 09:31:02 -07:00
|
|
|
next.asset = await Assets.findOneAsync({assetId: next.assetId})
|
|
|
|
|
if(next.asset) next.assetType = await AssetTypes.findOneAsync({_id: next.asset.assetTypeId})
|
2022-09-09 08:10:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-30 14:11:12 -07:00
|
|
|
}
|
2022-09-09 08:10:19 -07:00
|
|
|
}
|
2023-07-30 14:11:12 -07:00
|
|
|
|
|
|
|
|
return result
|
2022-09-09 08:10:19 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|