Filter duplicate vote records

This commit is contained in:
Samuel Elliott 2023-03-28 22:55:16 +01:00
parent 5e563d7c05
commit f418583382
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6

View File

@ -881,6 +881,8 @@ class Server extends HttpServer {
await this.handlePresenceRequest(req, res, presence_user_nsaid, true);
const TimestampSymbol = Symbol('Timestamp');
const VoteKeySymbol = Symbol('VoteKey');
const response: {
result: {
id: string;
@ -889,6 +891,7 @@ class Server extends HttpServer {
fest_team: FestTeam_votingStatus;
updated_at: string;
[TimestampSymbol]: number;
[VoteKeySymbol]: string;
}[];
} = {
result: [],
@ -949,16 +952,24 @@ class Server extends HttpServer {
fest_team,
updated_at: timestamp.toISOString(),
[TimestampSymbol]: timestamp.getTime(),
[VoteKeySymbol]: fest_id + '/' + fest_team_id + '/' + fest_team.myVoteState,
});
} catch (err) {
debug('Error reading fest voting status records', id, match[1]);
debug('Error reading fest voting status records', id, match[1], err);
}
}
}
if (!response.result.length) throw new ResponseError(404, 'not_found', 'No fest voting status history for this user');
response.result.sort((a, b) => b[TimestampSymbol] - a[TimestampSymbol]);
response.result.sort((a, b) => a[TimestampSymbol] - b[TimestampSymbol]);
response.result = response.result.filter((result, index, results) => {
const prev_result = results[index - 1];
return !prev_result || result[VoteKeySymbol] !== prev_result[VoteKeySymbol];
});
response.result.reverse();
return response;
}