Added assignment history and unassignment feature (not yet tested).
This commit is contained in:
14
imports/api/asset-assignment-history.js
Normal file
14
imports/api/asset-assignment-history.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import {Mongo} from "meteor/mongo";
|
||||
|
||||
export const AssetAssignmentHistory = new Mongo.Collection('assetAssignmentHistory');
|
||||
|
||||
/*
|
||||
Maintains a historical record of asset assignments.
|
||||
assetId: The asset's assigned ID (not a MongoID).
|
||||
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.
|
||||
startCondition: TODO
|
||||
endCondition: TODO
|
||||
*/
|
||||
@@ -5,6 +5,7 @@ import { Roles } from 'meteor/alanning:roles';
|
||||
//import SimpleSchema from "simpl-schema";
|
||||
import {AssetTypes} from "./asset-types";
|
||||
import {AssetAssignments} from "/imports/api/asset-assignments";
|
||||
import {AssetAssignmentHistory} from "/imports/api/asset-assignment-history";
|
||||
|
||||
// console.log("Setting Up Assets...")
|
||||
|
||||
@@ -91,6 +92,7 @@ Meteor.methods({
|
||||
Assets.insert({assetTypeId, assetId});
|
||||
}
|
||||
}
|
||||
else throw new Meteor.Error("User Permission Error");
|
||||
},
|
||||
'assets.update'(_id, assetId, serial) {
|
||||
check(_id, String);
|
||||
@@ -101,6 +103,7 @@ Meteor.methods({
|
||||
//TODO: Need to first verify there are no checked out assets to the staff member.
|
||||
Assets.update({_id}, {$set: {assetId, serial}});
|
||||
}
|
||||
else throw new Meteor.Error("User Permission Error");
|
||||
},
|
||||
'assets.remove'(_id) {
|
||||
check(_id, String);
|
||||
@@ -109,6 +112,7 @@ Meteor.methods({
|
||||
//TODO: Ensure we have not assigned this asset??? Not sure if we should require unassigning first.
|
||||
Assets.remove({_id});
|
||||
}
|
||||
else throw new Meteor.Error("User Permission Error");
|
||||
},
|
||||
/**
|
||||
* Assigns the asset to the assignee. The assignee should either be a Student or Staff member.
|
||||
@@ -129,8 +133,9 @@ Meteor.methods({
|
||||
// Should never happen.
|
||||
console.error("Error: Received incorrect assignee type in adding an assignment.");
|
||||
console.error(assigneeType);
|
||||
throw new Meteor.Error("Error: Received incorrect assignee type in adding an assignment.");
|
||||
}
|
||||
else if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
|
||||
else if(Roles.userIsInRole(Meteor.userId(), "laptop-management", {anyScope:true})) {
|
||||
let asset = Assets.findOne({assetId});
|
||||
|
||||
if(asset) {
|
||||
@@ -147,6 +152,7 @@ Meteor.methods({
|
||||
console.error("Could not find the asset: " + assetId)
|
||||
}
|
||||
}
|
||||
else throw new Meteor.Error("User Permission Error");
|
||||
},
|
||||
/**
|
||||
* Removes an assignment for the asset.
|
||||
@@ -160,37 +166,53 @@ Meteor.methods({
|
||||
|
||||
if(!date) date = new Date();
|
||||
|
||||
if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
|
||||
Assets.update({assetId}, {$unset: {assigneeType, assigneeId, assignmentDate}});
|
||||
if(Roles.userIsInRole(Meteor.userId(), "laptop-management", {anyScope:true})) {
|
||||
let asset = Assets.findOne({assetId});
|
||||
|
||||
if(asset) {
|
||||
try {
|
||||
AssetAssignmentHistory.insert({assetId, assigneeType: asset.assigneeType, assigneeId: asset.assigneeId, startDate: asset.assignmentDate, endDate: date});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
Assets.update({assetId}, {$unset: {assigneeType, assigneeId, assignmentDate}});
|
||||
}
|
||||
else {
|
||||
console.error("Could not find the asset: " + assetId);
|
||||
throw new Meteor.Error("Could not find the asset: " + assetId);
|
||||
}
|
||||
}
|
||||
else throw new Meteor.Error("User Permission Error");
|
||||
},
|
||||
/**
|
||||
* 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}));
|
||||
}
|
||||
}
|
||||
// Removed this since it should no longer be relevant.
|
||||
|
||||
// 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}));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -7,5 +7,6 @@ import "./sites.js";
|
||||
import "./asset-types.js";
|
||||
import "./assets.js";
|
||||
import "./asset-assignments.js";
|
||||
import "./asset-assignment-history.js";
|
||||
|
||||
// console.log("Finished setting up server side models.");
|
||||
Reference in New Issue
Block a user