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

@@ -72,7 +72,6 @@ Meteor.methods({
check(assigneeId, String);
check(assigneeType, String);
check(assetId, String);
if(assigneeType !== 'Student' && assigneeType !== 'Staff') {
// Should never happen.

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}));
}
}
}
});

View File

@@ -4,7 +4,7 @@ import { Roles } from 'meteor/alanning:roles';
import {check} from "meteor/check";
import {Sites} from "/imports/api/sites";
import {parse} from "csv-parse";
import {Students} from "/imports/api/students";
import { ReactiveAggregate } from 'meteor/tunguska:reactive-aggregate';
export const Staff = new Mongo.Collection('staff');
@@ -13,6 +13,15 @@ if (Meteor.isServer) {
Meteor.publish('staff', function(siteId) {
return Staff.find({siteId});
});
Meteor.publish('staffWithAssetAssignments', function(query) {
ReactiveAggregate(this, Staff, {$lookup: {
from: 'assetAssignments',
localField: '_id',
foreignField: 'assigneeId',
as: 'assignments'
}}, {});
//Note: The options can use {clientCollection: 'your_name_here'} as the options to change the collection name on the client.
});
}
Meteor.methods({
'staff.add'(firstName, lastName, email, siteId) {

View File

@@ -4,6 +4,7 @@ import { check } from 'meteor/check';
import {Sites} from "./sites";
import { Roles } from 'meteor/alanning:roles';
import {parse} from 'csv-parse';
import { ReactiveAggregate } from 'meteor/tunguska:reactive-aggregate';
export const Students = new Mongo.Collection('students');
@@ -14,6 +15,15 @@ if (Meteor.isServer) {
Meteor.publish('students', function(siteId) {
return Students.find({siteId});
});
Meteor.publish('studentWithAssetAssignments', function(query) {
ReactiveAggregate(this, Students, {$lookup: {
from: 'assetAssignments',
localField: '_id',
foreignField: 'assigneeId',
as: 'assignments'
}}, {});
//Note: The options can use {clientCollection: 'your_name_here'} as the options to change the collection name on the client.
});
Meteor.methods({
'students.getPossibleGrades'() {