get_my/imports/api/userLast.js
2025-07-23 19:44:24 -05:00

67 lines
No EOL
2.3 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
export const UserLast = new Mongo.Collection('userLast');
UserLast.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
async 'add.userLast' (view, viewId) {
check(view, String);
check(viewId, String);
if (!this.userId) {
throw new Meteor.Error('Not able to change user view last setting. Make sure you are logged in with valid system administrator credentials.');
}
// first let's find out if there's an entry for this user and view, and if so
// we'll just edit that entry with updated information
let userListInfo = await UserLast.findOneAsync({ userId: this.userId, view: view });
if (!userListInfo) {
// console.log("Adding new user last item.");
return await UserLast.insertAsync({
userId: this.userId,
view: view,
viewId: viewId,
dateAdded: Date(),
});
} else {
// console.log("Editing existing user last itme.");
// entry exists, call the edit function instead
let result = await Meteor.callAsync('edit.userLast', view, viewId);
if (!result) {
try {
// console.log("Issue editing existing entry in userLast. Check the logs.");
} catch(error) {
console.log(" ERROR adding userLast item: " + error);
console.log(error.message);
console.log(error.stack);
}
} else {
return true;
}
}
},
async 'edit.userLast' (view, viewId) {
check(view, String);
check(viewId, String);
if (!this.userId) {
throw new Meteor.Error('Not able to change user view last setting. Make sure you are logged in with valid system administrator credentials.');
}
// console.log("Edit in progress.");
return await UserLast.updateAsync({ view: view, userId: this.userId }, {
$set: {
viewId: viewId,
dateLastUpdate: Date(),
}
});
}
});