forked from cinn-labs/meteor-multitenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtenancy.server.js
38 lines (32 loc) · 1.35 KB
/
tenancy.server.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
import { Meteor } from 'meteor/meteor';
import Tenancy from './tenancy.common';
Meteor.publish('_groupId', function () {
if (!this.userId) return this.ready();
return Meteor.users.find({ _id: this.userId }, { fields: { [Tenancy.groupName]: 1 } });
});
Tenancy.setUserTenant = function(userId, groupId, callback) {
Meteor.defer(() => Meteor.users.update({ _id: userId }, { $set: { [Tenancy.groupName]: groupId } }, callback));
};
Tenancy.insertHook = function(userId, doc) {
const groupId = Tenancy.currentGroupId(userId);
if(!groupId) return true;
doc[Tenancy.groupName] = groupId;
};
Tenancy.upsertHook = function(userId, selector, modifier, options) {
const groupId = Tenancy.currentGroupId(userId);
if(!groupId) return true;
modifier.$set = modifier.$set || {};
modifier.$set[Tenancy.groupName] = groupId;
};
Tenancy.findHook = function(userId, selector, options) {
const groupId = Tenancy.currentGroupId(userId);
if(!groupId) return true;
selector[Tenancy.groupName] = groupId;
};
Tenancy.prepareCollection = function(collection) {
if(!collection.before) return console.warn(`[TENANCY] Collection ${collection._name} was not succesfully configured.`);
collection.before.find(Tenancy.findHook);
collection.before.findOne(Tenancy.findHook);
collection.before.insert(Tenancy.insertHook);
// collection.before.upsert(Tenancy.upsertHook);
};