Merged the AssetAssignment collection with the Asset collection; Updated views for assigning assets; Added view to find asset assignments by assetId.

This commit is contained in:
2022-08-14 17:07:14 -07:00
parent f3b1b38e82
commit cf51200393
13 changed files with 3897 additions and 53 deletions

View File

@@ -4,6 +4,8 @@ import { check } from 'meteor/check';
import { Roles } from 'meteor/alanning:roles';
//import SimpleSchema from "simpl-schema";
import {AssetTypes} from "./asset-types";
import { ReactiveAggregate } from 'meteor/tunguska:reactive-aggregate';
import {AssetAssignments} from "/imports/api/asset-assignments";
export const Assets = new Mongo.Collection('assets');
@@ -31,6 +33,25 @@ const AssetsSchema = new SimpleSchema({
index: 1,
unique: false
},
assigneeId: { //Should be undefined or non-existent if not assigned.
type: String,
label: "Assignee ID",
optional: true,
},
assigneeType: { // 0: Student, 1: Staff, Should be undefined or non-existent if not assigned.
type: SimpleSchema.Integer,
label: "Assignee Type",
optional: true,
min: 0,
max: 1,
exclusiveMin: false,
exclusiveMax: false,
},
assignmentDate: {
type: Date,
label: "Assignment Date",
optional: true,
}
});
Assets.attachSchema(AssetsSchema);
*/
@@ -84,9 +105,91 @@ Meteor.methods({
check(_id, String);
if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
//TODO: Need to first verify there are no checked out assets to the staff member.
//TODO: Ensure we have not assigned this asset??? Not sure if we should require unassigning first.
Assets.remove({_id});
}
},
/**
* Assigns the asset to the assignee. The assignee should either be a Student or Staff member.
* @param assetId The Asset ID (eg: 'Z1Q') of the asset (asset.assetId).
* @param assigneeType One of: 'Student', 'Staff'
* @param assigneeId The Mongo ID of the Student or Staff (person._id).
* @param date The date/time of the action. Will be set to the current date/time if not provided.
*/
'assets.assign'(assetId, assigneeType, assigneeId, date) {
check(assigneeId, String);
check(assigneeType, String);
check(assetId, String);
if(date) check(date, Date);
if(!date) date = new Date();
if(assigneeType !== 'Student' && assigneeType !== 'Staff') {
// Should never happen.
console.error("Error: Received incorrect assignee type in adding an assignment.");
console.error(assigneeType);
}
else if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
let asset = Assets.findOne({assetId});
if(asset) {
if(asset.assigneeId) {
//TODO: Should we unassign and re-assign????
console.error("Asset is already assigned! " + assetId);
throw new Meteor.Error("Asset is already assigned.", "Cannot assign an asset that has already been assigned.");
}
else {
Assets.update({assetId}, {$set: {assigneeType, assigneeId, assignmentDate: date}});
}
}
else {
console.error("Could not find the asset: " + assetId)
}
}
},
/**
* Removes an assignment for the asset.
* TODO: Should create a historical record.
* @param assetId The Asset ID (eg: 'Z1Q') of the asset (asset.assetId).
* @param date The date/time of the action. Will be set to the current date/time if not provided.
*/
'assets.unassign'(assetId, date) {
check(assetId, String);
if(date) check(date, Date);
if(!date) date = new Date();
if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
Assets.update({assetId}, {$unset: {assigneeType, assigneeId, assignmentDate}});
}
},
/**
* A fix to remove the AssetAssignment collection and merge it with the Asset collection.
*/
'assets.fixAssetAssignments'() {
let assignmentDate = new Date();
//This function just removes the need for the asset-assignments collection and merges it with assets.
if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
let assets = Assets.find({}).fetch();
let assetAssignments = AssetAssignments.find({}).fetch();
let assetMap = assets.reduce((map, obj) => {
map[obj.assetId] = obj;
return map;
}, {});
console.log(assetMap);
console.log("");
for(let next of assetAssignments) {
console.log(next);
let asset = assetMap[next.assetId];
console.log("Updating " + asset.assetId + " to be assigned to " + next.assigneeType + ": " + next.assigneeId);
let c = Assets.update({assetId: asset.assetId}, {$set: {assigneeType: next.assigneeType, assigneeId: next.assigneeId, assignmentDate}});
console.log("Updated " + c + " Assets");
console.log(Assets.findOne({assetId: asset.assetId}));
}
}
}
});