-
Notifications
You must be signed in to change notification settings - Fork 3
/
StuffCollection.js
157 lines (145 loc) · 4.64 KB
/
StuffCollection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Meteor } from 'meteor/meteor';
import SimpleSchema from 'simpl-schema';
import { check } from 'meteor/check';
import { _ } from 'meteor/underscore';
import { Roles } from 'meteor/alanning:roles';
import BaseCollection from '../base/BaseCollection';
import { ROLE } from '../role/Role';
export const stuffConditions = ['excellent', 'good', 'fair', 'poor'];
export const stuffPublications = {
stuff: 'Stuff',
stuffAdmin: 'StuffAdmin',
};
class StuffCollection extends BaseCollection {
constructor() {
super('Stuffs', new SimpleSchema({
name: String,
quantity: Number,
owner: String,
condition: {
type: String,
allowedValues: stuffConditions,
defaultValue: 'good',
},
}));
}
/**
* Defines a new Stuff item.
* @param name the name of the item.
* @param quantity how many.
* @param owner the owner of the item.
* @param condition the condition of the item.
* @return {String} the docID of the new document.
*/
define({ name, quantity, owner, condition }) {
const docID = this._collection.insert({
name,
quantity,
owner,
condition,
});
return docID;
}
/**
* Updates the given document.
* @param docID the id of the document to update.
* @param name the new name (optional).
* @param quantity the new quantity (optional).
* @param condition the new condition (optional).
*/
update(docID, { name, quantity, condition }) {
const updateData = {};
if (name) {
updateData.name = name;
}
// if (quantity) { NOTE: 0 is falsy so we need to check if the quantity is a number.
if (_.isNumber(quantity)) {
updateData.quantity = quantity;
}
if (condition) {
updateData.condition = condition;
}
this._collection.update(docID, { $set: updateData });
}
/**
* A stricter form of remove that throws an error if the document or docID could not be found in this collection.
* @param { String | Object } name A document or docID in this collection.
* @returns true
*/
removeIt(name) {
const doc = this.findDoc(name);
check(doc, Object);
this._collection.remove(doc._id);
return true;
}
/**
* Default publication method for entities.
* It publishes the entire collection for admin and just the stuff associated to an owner.
*/
publish() {
if (Meteor.isServer) {
// get the StuffCollection instance.
const instance = this;
/** This subscription publishes only the documents associated with the logged in user */
Meteor.publish(stuffPublications.stuff, function publish() {
if (this.userId) {
const username = Meteor.users.findOne(this.userId).username;
return instance._collection.find({ owner: username });
}
return this.ready();
});
/** This subscription publishes all documents regardless of user, but only if the logged in user is the Admin. */
Meteor.publish(stuffPublications.stuffAdmin, function publish() {
if (this.userId && Roles.userIsInRole(this.userId, ROLE.ADMIN)) {
return instance._collection.find();
}
return this.ready();
});
}
}
/**
* Subscription method for stuff owned by the current user.
*/
subscribeStuff() {
if (Meteor.isClient) {
return Meteor.subscribe(stuffPublications.stuff);
}
return null;
}
/**
* Subscription method for admin users.
* It subscribes to the entire collection.
*/
subscribeStuffAdmin() {
if (Meteor.isClient) {
return Meteor.subscribe(stuffPublications.stuffAdmin);
}
return null;
}
/**
* Default implementation of assertValidRoleForMethod. Asserts that userId is logged in as an Admin or User.
* This is used in the define, update, and removeIt Meteor methods associated with each class.
* @param userId The userId of the logged in user. Can be null or undefined
* @throws { Meteor.Error } If there is no logged in user, or the user is not an Admin or User.
*/
assertValidRoleForMethod(userId) {
this.assertRole(userId, [ROLE.ADMIN, ROLE.USER]);
}
/**
* Returns an object representing the definition of docID in a format appropriate to the restoreOne or define function.
* @param docID
* @return {{owner: (*|number), condition: *, quantity: *, name}}
*/
dumpOne(docID) {
const doc = this.findDoc(docID);
const name = doc.name;
const quantity = doc.quantity;
const condition = doc.condition;
const owner = doc.owner;
return { name, quantity, condition, owner };
}
}
/**
* Provides the singleton instance of this class to all other entities.
*/
export const Stuffs = new StuffCollection();