import { Candidate } from '../types/candidate' ; import { UserAction } from '../coffee-list.constants' ; import { User } from '../../../core/user/types/user' ; import * as dateHelpers from '../../../shared/helpers/date.helpers' ; export function getCandidatesInStoreFormat ( candidates , user ) { return candidates . map ( candidate => { candidate = prettifyVoteDates ( candidate ) ; candidate = getCandidateWithUserActions ( candidate , user ) ; return candidate ; } ) ; } function prettifyVoteDates ( candidate ) { return { ... candidate , votes : candidate . votes . map ( vote => { return { user : vote . user , date : dateHelpers . convertISOToHuman ( vote . date ) , } ; } ) , } ; } function getCandidateWithUserActions ( candidate , user ) { return { ... candidate , userAction : hasUserVotedForCandidate ( user , candidate ) ? UserAction . RemoveVote : UserAction . AddVote , } ; } function hasUserVotedForCandidate ( user , candidate ) { for ( let vote of candidate . votes ) { if ( vote . user === user . email ) { return true ; } } return false ; }