Skip to content

Commit 480d087

Browse files
committed
feat(getVotes): getVotes for a single voter
It is possible, with getVotes api, to retrieve the votes of a single voter for an event
1 parent e28ab57 commit 480d087

File tree

2 files changed

+390
-12
lines changed

2 files changed

+390
-12
lines changed

src/api/votes-apis.ts

+30-11
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@ import { getObjectId } from './utils';
2424
import { logError } from '../lib/utils';
2525
import { Credentials } from '../model/credentials';
2626

27-
export function getVotes(votesColl: Collection, id?: string | { eventId: string }): Observable<Vote[]> {
28-
let eventId: any;
29-
if (id && typeof id === 'string') {
30-
eventId = id;
31-
} else if (id) {
32-
eventId = id['eventId'];
27+
export function getVotes(
28+
votesColl: Collection,
29+
params?: string | { eventId: string; voterId?: any },
30+
): Observable<Vote[]> {
31+
let _eventId: any;
32+
if (params && typeof params === 'string') {
33+
_eventId = params;
34+
} else if (params) {
35+
_eventId = params['eventId'];
36+
}
37+
let selector: any = _eventId
38+
? { cancelled: { $exists: false }, eventId: _eventId }
39+
: { cancelled: { $exists: false } };
40+
if (params && params['voterId']) {
41+
const upperCaseVoterId: any = {};
42+
const voterId = params['voterId'];
43+
for (const key in voterId) {
44+
upperCaseVoterId[key] = voterId[key].toUpperCase();
45+
}
46+
selector.voterId = upperCaseVoterId;
3347
}
34-
const selector = eventId ? { cancelled: { $exists: false }, eventId } : { cancelled: { $exists: false } };
3548
return findObs(votesColl, selector).pipe(toArray());
3649
}
3750
export function getAllVotes(votesColl: Collection): Observable<Vote[]> {
@@ -122,10 +135,16 @@ function voterIdToUpperCase(voterId: { firstName: string; lastName: string } | C
122135
lastName: voterId['lastName'].toUpperCase().trim(),
123136
};
124137
} else {
125-
resp = {
126-
nickname: voterId['nickname'] ? voterId['nickname'].toUpperCase().trim() : '',
127-
userId: voterId['userId'] ? voterId['userId'].toUpperCase().trim() : '',
128-
};
138+
if (voterId['nickname']) {
139+
resp = {
140+
nickname: voterId['nickname'].toUpperCase().trim(),
141+
};
142+
}
143+
if (voterId['userId']) {
144+
resp = {
145+
userId: voterId['userId'].toUpperCase().trim(),
146+
};
147+
}
129148
}
130149
return resp;
131150
}

0 commit comments

Comments
 (0)