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:
@@ -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
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user