11import { app , HttpRequest , InvocationContext } from '@azure/functions'
2- import { EventWithRating , PontozoException } from '@pontozo/common'
2+ import { EventWithRating , PontozoException , RatingStatus } from '@pontozo/common'
33import { getUserFromHeaderIfPresent } from '../../service/auth.service'
44import Event from '../../typeorm/entities/Event'
55import EventRating from '../../typeorm/entities/EventRating'
@@ -17,19 +17,34 @@ export const getOneEvent = async (req: HttpRequest, context: InvocationContext):
1717
1818 const user = getUserFromHeaderIfPresent ( req )
1919 const ads = await getAppDataSource ( context )
20+ const erRepo = ads . getRepository ( EventRating )
2021 const eventQuery = ads . getRepository ( Event ) . findOne ( { where : { id : eventId } , relations : { organisers : true , stages : true } } )
21- const userRatingQuery = ads
22- . getRepository ( EventRating )
23- . findOne ( { where : { eventId : eventId , userId : user ?. szemely_id } , relations : { stages : true } } )
24- const [ event , userRating ] = await Promise . all ( [ eventQuery , user ? userRatingQuery : Promise . resolve ( null ) ] )
22+ const userRatingQuery = erRepo . findOne ( { where : { eventId, userId : user ?. szemely_id } , relations : { stages : true } } )
23+ const allRatingsQuery = erRepo . find ( { where : { eventId } } )
24+ const [ event , userRating , allRatings ] = await Promise . all ( [ eventQuery , user ? userRatingQuery : Promise . resolve ( null ) , allRatingsQuery ] )
2525
2626 if ( ! event ) {
2727 throw new PontozoException ( 'A verseny nem található!' , 404 )
2828 }
29+ const now = new Date ( ) . getTime ( )
30+ let submittedRatingCount = 0
31+ let editingNow = 0
32+ allRatings . forEach ( ( er ) => {
33+ if ( er . status === RatingStatus . SUBMITTED ) {
34+ submittedRatingCount ++
35+ return
36+ }
37+ // ratings started in the past hour that haven't been submitted yet
38+ if ( new Date ( er . createdAt ) . getTime ( ) > now - 1000 * 60 * 60 ) {
39+ editingNow ++
40+ }
41+ } )
2942 return {
3043 jsonBody : {
3144 event,
3245 userRating,
46+ submittedRatingCount,
47+ editingNow,
3348 } ,
3449 }
3550 } catch ( error ) {
0 commit comments