-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
publications.js
160 lines (153 loc) · 4.61 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
import { ReactiveAggregate } from 'meteor/tunguska:reactive-aggregate'
import { Match } from 'meteor/check'
import Timecards from '../timecards.js'
import Projects from '../../projects/projects.js'
import { checkAuthentication, buildDetailedTimeEntriesForPeriodSelector } from '../../../utils/server_method_helpers.js'
Meteor.publish('periodTimecards', async function periodTimecards({ startDate, endDate, userId }) {
check(startDate, Date)
check(endDate, Date)
check(userId, String)
await checkAuthentication(this)
let projectList = await Projects.find(
{
$and: [
{ $or: [{ userId: this.userId }, { public: true }, { team: this.userId }] },
{ $or: [{ archived: false }, { archived: { $exists: false } }] },
],
},
{ fields: { _id: 1 } },
).fetchAsync()
projectList = projectList.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', async function periodTimecards({ projectId, startDate, endDate }) {
check(startDate, Date)
check(endDate, Date)
check(projectId, String)
await 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', async function myTimecardsForDate({ date }) {
check(date, String)
await 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,
filters,
}) {
check(projectId, Match.OneOf(String, Array))
check(userId, Match.OneOf(String, Array))
check(customer, Match.OneOf(String, Array))
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, filters,
})
const countsId = projectId instanceof Array ? projectId.join('') : projectId
const handle = Timecards.find(selector[0], selector[1]).observeChanges({
added: () => {
count += 1
if (!initializing) {
this.changed('counts', countsId, { count })
}
},
removed: () => {
count -= 1
this.changed('counts', countsId, { count })
},
})
initializing = false
this.added('counts', countsId, { count })
this.ready()
this.onStop(() => handle.stop())
})
Meteor.publish('getDetailedTimeEntriesForPeriod', async function getDetailedTimeEntriesForPeriod({
projectId,
userId,
customer,
period,
dates,
search,
sort,
limit,
page,
filters,
}) {
check(projectId, Match.OneOf(String, Array))
check(userId, Match.OneOf(String, Array))
check(customer, Match.OneOf(String, Array))
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))
check(filters, Match.Maybe(Object))
await checkAuthentication(this)
const selector = buildDetailedTimeEntriesForPeriodSelector({
projectId, search, customer, period, dates, userId, limit, page, sort, filters,
})
return Timecards.find(selector[0], selector[1])
})
Meteor.publish('singleTimecard', async function singleTimecard(_id) {
check(_id, String)
await checkAuthentication(this)
const timecard = await Timecards.findOneAsync({ _id })
const project = await Projects.findOneAsync({ _id: timecard.projectId })
if (!this.userId || (!await Timecards.findOneAsync({ userId: this.userId }) && !project.public)) {
return this.ready()
}
return Timecards.find({ _id })
})