Modified Assets to update the assetId in the history records when it gets changed. Changing the assetId is handy when a sticker is removed, making it possible to just alter the ID instead of re-printing the sticker.

This commit is contained in:
2023-07-30 14:11:12 -07:00
parent 2cbb4a47b5
commit 4d20bf73b7
6 changed files with 408 additions and 142 deletions

View File

@@ -34,8 +34,16 @@ if (Meteor.isServer) {
* @returns {any} Array of Asset Assignment History objects.
*/
'AssetAssignmentHistory.get'(params) {
let result
if(Roles.userIsInRole(Meteor.userId(), "laptop-management", {anyScope:true})) {
let query = {};
let query = {}
let person
let asset
let assetType
// console.log("AssetAssignmentHistory: ")
// console.log(params)
if(params.studentId) check(params.studentId, String)
if(params.staffId) check(params.staffId, String)
@@ -56,81 +64,52 @@ if (Meteor.isServer) {
}
}
if(params.serial) query.serial = params.serial;
else if(params.assetId) query.assetId = params.assetId;
else if(params.deviceId) query.deviceId = params.deviceId;
else if(params.studentId) {
query.assigneeId = params.studentId
query.assigneeType = "Student"
}
else if(params.staffId) {
query.assigneeId = params.staffId
query.assigneeType = "Staff"
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;
asset = Assets.findOne(query)
if(asset) assetType = AssetTypes.findOne({_id: asset.assetTypeId})
}
else {
query = undefined;
}
if(query) {
//Sort by the last time the record was updated from most to least recent.
let result = AssetAssignmentHistory.find(query, {sort: {endDate: -1}}).fetch();
let assets = [];
// Get the current assignment for the device or person.
if(query.assetId || query.deviceId || query.serial) {
let asset = Assets.findOne(query)
if(asset) assets = [asset]
if(params.studentId) {
query.assigneeId = params.studentId
query.assigneeType = "Student"
}
else if(params.staffId) {
query.assigneeId = params.staffId
query.assigneeType = "Staff"
}
else {
// Find the assets assigned to the person.
assets = Assets.find({assigneeId: params.studentId ? params.studentId : params.staffId}).fetch()
query = undefined;
}
person = query.assigneeType === "Student" ? Students.findOne({id: query.assigneeId}) : Staff.findOne({id: query.assigneeId})
}
if(query) {
//Sort by the last time the record was updated from most to least recent.
result = AssetAssignmentHistory.find(query, {sort: {endDate: -1}}).fetch();
// Prepend a partial assignment history record to the list. We want to show active assignments in the results.
for(let asset of assets) {
if(asset && asset.assigneeId) {
let assetType = AssetTypes.findOne(asset.assetTypeId)
let current = {
_id: 0,
assetKey: asset._id,
assetId: asset.assetId,
serial: asset.serial,
assetTypeName: assetType.name,
assigneeType: asset.assigneeType,
assigneeId: asset.assigneeId,
startDate: asset.assignmentDate,
startCondition: asset.condition,
startConditionDetails: asset.conditionDetails
}
result = [current, ...result]
}
}
//Add some additional data to the records.
//Expand the assignee, asset, and asset type data.
for(let next of result) {
// console.log(next)
if(next.assetKey) {
next.asset = Assets.findOne({_id: next.assetKey})
if(person) next.assignee = person
else next.assignee = next.assigneeType === "Student" ? Students.findOne({_id: next.assigneeId}) : Staff.findOne({_id: next.assigneeId})
if(asset) {
next.asset = asset
next.assetType = assetType
}
else if(next.assetId) {
next.asset = Assets.findOne({assetId: next.assetId});
}
if(next.asset) {
next.assetType = AssetTypes.findOne({_id: next.asset.assetTypeId})
}
if(next.assigneeId) {
next.assignee = next.asset.assigneeType === "Student" ? Students.findOne({_id: next.assigneeId}) : Staff.findOne({_id: next.assigneeId})
else {
next.asset = Assets.findOne({assetId: next.assetId})
if(next.asset) next.assetType = AssetTypes.findOne({_id: next.asset.assetTypeId})
}
}
return result;
} else return null;
}
}
else return null;
return result
}
});
}

View File

@@ -123,6 +123,7 @@ Meteor.methods({
if(serial) check(serial, String);
check(condition, String);
if(conditionDetails) check(conditionDetails, String);
const existing = Assets.findOne({_id})
if(!conditions.includes(condition)) {
//Should never happen.
@@ -133,6 +134,12 @@ Meteor.methods({
if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
//TODO: Need to first verify there are no checked out assets to the staff member.
Assets.update({_id}, {$set: {assetTypeId, assetId, serial, condition, conditionDetails}});
if(assetId !== existing.assetId) {
//When changing the asset id we also need to update the other locations in the data where that ID exists.
// assetAssignmentHistory.assetId
AssetAssignmentHistory.updateMany({assetId: existing.assetId}, {$set: {assetId}})
}
}
else throw new Meteor.Error("User Permission Error");
},
@@ -217,7 +224,7 @@ Meteor.methods({
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, condition, conditionDetails}});
Assets.update({assetId}, {$set: {assigneeType, assigneeId, assignmentDate: date, condition, conditionDetails, assignedBy: Meteor.userId()}});
}
}
else {
@@ -257,11 +264,11 @@ Meteor.methods({
let assetType = AssetTypes.findOne({_id: asset.assetTypeId});
try {
AssetAssignmentHistory.insert({assetKey: asset._id, assetId, serial: asset.serial, assetTypeName: (assetType ? assetType.name : "UNK"), assigneeType: asset.assigneeType, assigneeId: asset.assigneeId, startDate: asset.assignmentDate, endDate: date, startCondition: asset.condition, endCondition: condition, startConditionDetails: asset.conditionDetails, endConditionDetails: conditionDetails, comment});
AssetAssignmentHistory.insert({assetKey: asset._id, assetId, serial: asset.serial, assetTypeName: (assetType ? assetType.name : "UNK"), assigneeType: asset.assigneeType, assigneeId: asset.assigneeId, startDate: asset.assignmentDate, endDate: date, startCondition: asset.condition, endCondition: condition, startConditionDetails: asset.conditionDetails, endConditionDetails: conditionDetails, comment, unassignedBy: Meteor.userId(), assignedBy: asset.assignedBy});
} catch (e) {
console.error(e);
}
Assets.update({assetId}, {$unset: {assigneeType: "", assigneeId: "", assignmentDate: ""}, $set: {condition, conditionDetails}});
Assets.update({assetId}, {$unset: {assigneeType: "", assigneeId: "", assignmentDate: "", assignedBy: ""}, $set: {condition, conditionDetails}});
}
else {
console.error("Could not find the asset: " + assetId);

View File

@@ -72,7 +72,7 @@ if (Meteor.isServer) {
else if(params.studentId) {
const student = Students.findOne({_id: params.studentId})
console.log(student)
// console.log(student)
if(student) query.email = student.email;
else query = undefined
}