diff --git a/imports/api/asset-assignment-history.js b/imports/api/asset-assignment-history.js new file mode 100644 index 0000000..36df18f --- /dev/null +++ b/imports/api/asset-assignment-history.js @@ -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 + */ \ No newline at end of file diff --git a/imports/api/assets.js b/imports/api/assets.js index 86f6f82..49b6293 100644 --- a/imports/api/assets.js +++ b/imports/api/assets.js @@ -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})); + // } + // } } }); diff --git a/imports/api/index.js b/imports/api/index.js index 082304b..1066491 100644 --- a/imports/api/index.js +++ b/imports/api/index.js @@ -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."); \ No newline at end of file diff --git a/imports/ui/Assets/AssignmentByAsset.svelte b/imports/ui/Assets/AssignmentByAsset.svelte index 081a485..72a8013 100644 --- a/imports/ui/Assets/AssignmentByAsset.svelte +++ b/imports/ui/Assets/AssignmentByAsset.svelte @@ -9,6 +9,8 @@ import {Assets} from "/imports/api/assets"; import {Students} from "/imports/api/students"; import {AssetTypes} from "/imports/api/asset-types"; + import Button, { Label } from '@smui/button'; + import Dialog, { Title, Content, Actions } from '@smui/dialog'; onMount(async () => { Meteor.subscribe('assets'); @@ -53,6 +55,11 @@ const formatDate = (date) => { return date.toLocaleDateString('en-us', {weekday: 'long', year: 'numeric', month: 'short', day: 'numeric'}); } + const unassign = () => { + if(confirm("Unassign Asset?")) { + Meteor.call("assets.unassign", foundAsset.assetId); + } + }