@@ -4,52 +4,59 @@ import Property, { TravelTimes } from 'App/Models/Property'
44import PropertyValidator from 'App/Validators/PropertyValidator'
55import SearchService from 'App/Services/SearchService'
66import type { PropertyRatingWithUser } from 'App/Models/PropertyRating'
7+ import UserLocationService from 'App/Services/UserLocationService'
8+ import UserLocation from 'App/Models/UserLocation'
79
810interface ServiceProperty extends Omit < Property , 'ratings' > {
9- travelTimes : TravelTimes
1011 average_ratings ?: number
1112 ratings : PropertyRatingWithUser [ ]
1213}
1314
15+ type UserLocationWithTravelTime = {
16+ userLocation : UserLocation
17+ travelTimes : TravelTimes
18+ }
19+
20+ interface ServicePropertyWithLocations extends ServiceProperty {
21+ locations : Array < UserLocationWithTravelTime >
22+ }
23+
1424export default class PropertyService extends BaseService {
15- public static async getSearchProperties ( searchId : number ) : Promise < ServiceProperty [ ] | null > {
25+ public static async getSearchProperties ( searchId : number ) : Promise < Property [ ] | null > {
1626 const search = await SearchService . getById ( searchId )
1727 if ( ! search ) return null
1828
19- const properties = await Property . query ( )
29+ return await Property . query ( )
2030 . where ( 'search_id' , search ?. id )
2131 . where ( 'is_deleted' , false )
2232 . preload ( 'location' )
2333 . preload ( 'ratings' )
2434 . orderBy ( 'created_at' , 'desc' )
2535 . exec ( )
26-
27- return await Promise . all (
28- properties . map ( async ( property ) => {
29- let travelTimes = { driving : 0 , transit : 0 , walking : 0 }
30- try {
31- travelTimes = await property . getTravelTimes ( {
32- lat : search . location . lat ,
33- lng : search . location . lng ,
34- } )
35- } catch ( e ) {
36- console . log ( e )
37- }
38- return { ...property . toJSON ( ) , travelTimes } as ServiceProperty
39- } )
40- )
4136 }
4237
4338 public static async findById ( id : number , searchId : number ) {
4439 const property = await this . getById ( id , searchId )
4540
46- const search = await SearchService . getById ( searchId )
47- if ( ! search || ! property ) return super . response . notFound ( )
41+ const userLocations = await UserLocationService . getAllByUserId ( )
42+ if ( ! property ) return super . response . notFound ( )
4843
49- const travelTimes = await property . getTravelTimes ( {
50- lat : search . location . lat ,
51- lng : search . location . lng ,
52- } )
44+ let locations : Array < UserLocationWithTravelTime > = [ ]
45+
46+ if ( userLocations ) {
47+ locations = await Promise . all (
48+ userLocations . map ( async ( location ) => {
49+ const travelTimes = await property . getTravelTimes ( {
50+ lat : location . location . lat ,
51+ lng : location . location . lng ,
52+ } )
53+ return {
54+ userLocation : location ,
55+ travelTimes,
56+ }
57+ } )
58+ )
59+ }
5360
5461 const propertyObj : Property = property . toJSON ( ) as Property
5562
@@ -59,10 +66,10 @@ export default class PropertyService extends BaseService {
5966
6067 return {
6168 ...propertyObj ,
62- travelTimes ,
69+ locations ,
6370 ratings,
6471 average_ratings : property . averageRating ,
65- } as ServiceProperty
72+ } as ServicePropertyWithLocations
6673 }
6774
6875 public static async getById ( id : number , searchId : number ) {
0 commit comments