2017-01-15 11:33:37 -08:00
import { Meteor } from 'meteor/meteor' ;
import { Mongo } from 'meteor/mongo' ;
import { check } from 'meteor/check' ;
2025-07-02 11:18:09 -07:00
import 'meteor/aldeed:collection2/static'
import SimpleSchema from 'meteor/aldeed:simple-schema' ;
2017-01-15 11:33:37 -08:00
2019-07-28 13:47:54 -07:00
let Measures = new Mongo . Collection ( 'Measures' ) ;
2025-07-02 11:18:09 -07:00
if ( Meteor . isServer ) {
//Set MongoDB indexes (or remove them) here.
try {
Measures . rawCollection ( ) . createIndex ( { name : - 1 } , { unique : true } )
} catch ( e ) { console . log ( "Caught exception while setting indexes in MongoDB" ) ; console . error ( e ) }
}
2017-01-15 11:33:37 -08:00
Measures . attachSchema ( new SimpleSchema ( {
name : {
type : String ,
label : "Name" ,
optional : false ,
trim : true ,
2025-07-02 11:18:09 -07:00
// index: 1, Requires aldeed:schema-index which requires an older version fo aldeed:collection2 (3.5.0 vs 4.0.2) This can be achieved by adding indexes to MongoDB directly.
//unique: true
2017-01-15 11:33:37 -08:00
} ,
postfix : {
type : String ,
label : "Postfix" ,
optional : true , //Note: Each does not have a postfix.
trim : true
} ,
order : {
type : SimpleSchema . Integer ,
label : "Order" ,
optional : false ,
min : 0
} ,
createdAt : { //Force the value to the current date on the server.
type : Date ,
label : "Created On" ,
// autoValue: function() { //The disadvantage of autoValue is that it sets the date after insertion, causing the UI to update twice - once where the item has no date, and then again where the date is set. Sorted lists will cause the item to bounce around.
// if(this.isInsert) return new Date();
// else if(this.isUpsert) return {$setOnInsert: new Date()};
// else this.unset();
// },
// denyUpdate: true,
optional : false
} ,
updatedAt : {
type : Date ,
label : "Updated On" ,
// autoValue: function() {
// if(this.isUpdate) return new Date();
// },
// denyInsert: true,
optional : true
} ,
2017-02-03 09:20:29 -08:00
deactivated : {
type : Boolean ,
label : "Deactivated" ,
2017-01-15 11:33:37 -08:00
optional : true
} ,
2017-02-03 09:20:29 -08:00
hidden : {
type : Boolean ,
label : "Hidden" ,
2017-01-15 11:33:37 -08:00
optional : true
}
} ) ) ;
if ( Meteor . isServer ) Meteor . publish ( 'measures' , function ( ) {
return Measures . find ( { } ) ;
} ) ;
if ( Meteor . isServer ) {
Meteor . methods ( {
2025-07-02 11:18:09 -07:00
createMeasure : async function ( name , postfix , order ) {
2017-02-03 09:20:29 -08:00
check ( name , String ) ;
check ( postfix , String ) ;
check ( order , Number ) ;
2017-01-15 11:33:37 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
2025-07-02 11:18:09 -07:00
await Measures . insertAsync ( { name , postfix , order , createdAt : new Date ( ) } ) ;
2017-02-03 09:20:29 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2025-07-02 11:18:09 -07:00
deactivateMeasure : async function ( id ) {
2017-02-03 09:20:29 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
//Measures.remove(id);
2025-07-02 11:18:09 -07:00
await Measures . updateAsync ( id , { $set : { deactivated : true } } , { bypassCollection2 : true } ) ;
2017-02-03 09:20:29 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2025-07-02 11:18:09 -07:00
reactivateMeasure : async function ( id ) {
2017-02-03 09:20:29 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
2025-07-02 11:18:09 -07:00
await Measures . updateAsync ( id , { $set : { deactivated : false } } , { bypassCollection2 : true } ) ;
2017-02-03 09:20:29 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2025-07-02 11:18:09 -07:00
hideMeasure : async function ( id ) { //One step past deactivated - will only show in the measures list if hidden measures are enabled.
2017-02-03 09:20:29 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
//Measures.remove(id);
2025-07-02 11:18:09 -07:00
await Measures . updateAsync ( id , { $set : { hidden : true } } , { bypassCollection2 : true } ) ;
2017-01-15 11:33:37 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2025-07-02 11:18:09 -07:00
showMeasure : async function ( id ) { //Returns the measure to being simply deactivated. Will again show in lists.
2017-01-15 11:33:37 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
2025-07-02 11:18:09 -07:00
await Measures . updateAsync ( id , { $set : { hidden : false } } , { bypassCollection2 : true } ) ;
2017-01-15 11:33:37 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
} ,
2017-02-03 09:20:29 -08:00
//deleteMeasure: function(id) {
// if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
// //TODO: Should troll the database looking for references to remove or replace. This is currently not used.
// Measures.remove(id);
// }
// else throw new Meteor.Error(403, "Not authorized.");
//},
2025-07-02 11:18:09 -07:00
updateMeasure : async function ( id , name , postfix , order ) {
2017-02-03 09:20:29 -08:00
check ( id , String ) ;
check ( name , String ) ;
check ( postfix , String ) ;
check ( order , Number ) ;
2017-01-15 11:33:37 -08:00
if ( Roles . userIsInRole ( this . userId , [ Meteor . UserRoles . ROLE _UPDATE ] ) ) {
2025-07-02 11:18:09 -07:00
await Measures . updateAsync ( id , { $set : { name , postfix , order , updatedAt : new Date ( ) } } ) ;
2017-01-15 11:33:37 -08:00
}
else throw new Meteor . Error ( 403 , "Not authorized." ) ;
}
} ) ;
}
2019-10-07 15:51:50 -07:00
//Allows the client to do DB interaction without calling server side methods, while still retaining control over whether the user can make changes.
Measures . allow ( {
insert : function ( ) { return false ; } ,
update : function ( ) { return false ; } ,
remove : function ( ) { return false ; }
} ) ;
2017-01-15 11:33:37 -08:00
export default Measures ;