/
publications.js
176 lines (168 loc) · 4.95 KB
/
publications.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { ReactiveAggregate } from 'meteor/tunguska:reactive-aggregate'
import Timecards from '../timecards.js'
import Projects from '../../projects/projects.js'
import { periodToDates } from '../../../utils/periodHelpers.js'
import { checkAuthentication, getProjectListById, buildDetailedTimeEntriesForPeriodSelector } from '../../../utils/server_method_helpers.js'
Meteor.publish('projectTimecards', function projectTimecards({ projectId, period, userId }) {
check(projectId, String)
check(period, String)
check(userId, String)
checkAuthentication(this)
const projectList = getProjectListById(projectId)
if (period && period !== 'all') {
const { startDate, endDate } = periodToDates(period)
if (userId === 'all') {
return Timecards.find({
projectId: { $in: projectList },
date: { $gte: startDate, $lte: endDate },
})
}
return Timecards.find({
projectId: { $in: projectList },
userId,
date: { $gte: startDate, $lte: endDate },
})
}
if (userId === 'all') {
return Timecards.find({ projectId: { $in: projectList } })
}
return Timecards.find({ projectId: { $in: projectList }, userId })
})
Meteor.publish('periodTimecards', function periodTimecards({ startDate, endDate, userId }) {
check(startDate, Date)
check(endDate, Date)
check(userId, String)
checkAuthentication(this)
const projectList = Projects.find(
{ $or: [{ userId: this.userId }, { public: true }, { team: this.userId }] },
{ $fields: { _id: 1 } },
).fetch().map((value) => value._id)
if (userId === 'all') {
return Timecards.find({
projectId: { $in: projectList },
date: { $gte: startDate, $lte: endDate },
})
}
return Timecards.find({
projectId: { $in: projectList },
userId,
date: { $gte: startDate, $lte: endDate },
})
})
Meteor.publish('userTimeCardsForPeriodByProjectByTask', function periodTimecards({ projectId, startDate, endDate }) {
check(startDate, Date)
check(endDate, Date)
check(projectId, String)
checkAuthentication(this)
return ReactiveAggregate(this, Timecards, [
{
$match: {
projectId,
userId: this.userId,
date: { $gte: startDate, $lte: endDate },
},
},
{
$group: {
_id: { $concat: ['$projectId', '|', '$task'] },
entries: { $push: '$$ROOT' },
},
},
], { clientCollection: 'clientTimecards', specificWarnings: { objectId: false } })
})
Meteor.publish('myTimecardsForDate', function myTimecardsForDate({ date }) {
check(date, String)
checkAuthentication(this)
const startDate = new Date(date)
const endDate = new Date(date)
startDate.setHours(0)
endDate.setHours(23, 59)
return Timecards.find({
userId: this.userId,
date: { $gte: startDate, $lte: endDate },
})
})
Meteor.publish('getDetailedTimeEntriesForPeriodCount', function getDetailedTimeEntriesForPeriodCount({
projectId,
userId,
customer,
period,
dates,
search,
}) {
check(projectId, String)
check(userId, String)
check(customer, String)
check(period, String)
if (period === 'custom') {
check(dates, Object)
check(dates.startDate, Date)
check(dates.endDate, Date)
}
check(search, Match.Maybe(String))
let count = 0
let initializing = true
const selector = buildDetailedTimeEntriesForPeriodSelector({
projectId, search, customer, period, dates, userId,
})
const handle = Timecards.find(selector[0], selector[1]).observeChanges({
added: () => {
count += 1
if (!initializing) {
this.changed('counts', projectId, { count })
}
},
removed: () => {
count -= 1
this.changed('counts', projectId, { count })
},
})
initializing = false
this.added('counts', projectId, { count })
this.ready()
this.onStop(() => handle.stop())
})
Meteor.publish('getDetailedTimeEntriesForPeriod', function getDetailedTimeEntriesForPeriod({
projectId,
userId,
customer,
period,
dates,
search,
sort,
limit,
page,
}) {
check(projectId, String)
check(userId, String)
check(customer, String)
check(period, String)
check(search, Match.Maybe(String))
check(sort, Match.Maybe(Object))
if (period === 'custom') {
check(dates, Object)
check(dates.startDate, Date)
check(dates.endDate, Date)
}
if (sort) {
check(sort.column, Number)
check(sort.order, String)
}
check(limit, Number)
check(page, Match.Maybe(Number))
checkAuthentication(this)
const selector = buildDetailedTimeEntriesForPeriodSelector({
projectId, search, customer, period, dates, userId, limit, page, sort,
})
return Timecards.find(selector[0], selector[1])
})
Meteor.publish('singleTimecard', function singleTimecard(_id) {
check(_id, String)
checkAuthentication(this)
const timecard = Timecards.findOne({ _id })
const project = Projects.findOne({ _id: timecard.projectId })
if (!this.userId || (!Timecards.findOne({ userId: this.userId }) && !project.public)) {
return this.ready()
}
return Timecards.find({ _id })
})