11import {
22 DefaultCrudRepository ,
3+ repository ,
4+ HasManyRepositoryFactory ,
35 Filter ,
6+ Options ,
47 Where ,
58 Count ,
69} from '@loopback/repository' ;
7- import { Planter , PlanterRelations } from '../models' ;
10+ import { Planter , PlanterRelations , PlanterRegistration } from '../models' ;
811import { TreetrackerDataSource } from '../datasources' ;
9- import { inject } from '@loopback/core' ;
12+ import { inject , Getter } from '@loopback/core' ;
13+ import { PlanterRegistrationRepository } from './planterRegistration.repository' ;
1014import expect from 'expect-runtime' ;
1115import { buildFilterQuery } from '../js/buildFilterQuery' ;
1216import { utils } from '../js/utils' ;
@@ -16,10 +20,24 @@ export class PlanterRepository extends DefaultCrudRepository<
1620 typeof Planter . prototype . id ,
1721 PlanterRelations
1822> {
23+ public readonly planterRegs : HasManyRepositoryFactory <
24+ PlanterRegistration ,
25+ typeof Planter . prototype . id
26+ > ;
1927 constructor (
2028 @inject ( 'datasources.treetracker' ) dataSource : TreetrackerDataSource ,
29+ @repository . getter ( 'PlanterRegistrationRepository' )
30+ protected planterRegistrationRepositoryGetter : Getter < PlanterRegistrationRepository > ,
2131 ) {
2232 super ( Planter , dataSource ) ;
33+ this . planterRegs = this . createHasManyRepositoryFactoryFor (
34+ 'planterRegs' ,
35+ planterRegistrationRepositoryGetter ,
36+ ) ;
37+ this . registerInclusionResolver (
38+ 'planterRegs' ,
39+ this . planterRegs . inclusionResolver ,
40+ ) ;
2341 }
2442
2543 async getEntityIdsByOrganizationId (
@@ -91,12 +109,27 @@ export class PlanterRepository extends DefaultCrudRepository<
91109 } ;
92110 }
93111
112+ getPlanterRegistrationJoinClause ( deviceIdentifier : string ) : string {
113+ if ( deviceIdentifier === null ) {
114+ return `LEFT JOIN planter_registrations
115+ ON planter.id=planter_registrations.planter_id
116+ WHERE (planter_registrations.device_identifier ISNULL)
117+ GROUP BY planter.id` ;
118+ }
119+ return `JOIN planter_registrations
120+ ON planter.id=planter_registrations.planter_id
121+ WHERE (planter_registrations.device_identifier='${ deviceIdentifier } ')
122+ GROUP BY planter.id` ;
123+ }
124+
94125 // loopback .find() wasn't applying the org filters
95126 async findWithOrg (
96127 filter ?: Filter < Planter > ,
128+ deviceIdentifier ?: string ,
129+ options ?: Options ,
97130 ) : Promise < ( Planter & PlanterRelations ) [ ] > {
98- if ( ! filter ) {
99- return await this . find ( filter ) ;
131+ if ( ! filter || deviceIdentifier === null ) {
132+ return await this . find ( filter , options ) ;
100133 }
101134
102135 try {
@@ -106,7 +139,14 @@ export class PlanterRepository extends DefaultCrudRepository<
106139 filter ,
107140 ) ;
108141
109- const selectStmt = `SELECT ${ columnNames } FROM planter ` ;
142+ let selectStmt ;
143+ if ( deviceIdentifier ) {
144+ selectStmt = `SELECT planter.* FROM planter ${ this . getPlanterRegistrationJoinClause (
145+ deviceIdentifier ,
146+ ) } `;
147+ } else {
148+ selectStmt = `SELECT ${ columnNames } FROM planter` ;
149+ }
110150
111151 const params = {
112152 filter,
@@ -115,25 +155,37 @@ export class PlanterRepository extends DefaultCrudRepository<
115155 } ;
116156
117157 const query = buildFilterQuery ( selectStmt , params ) ;
158+ // console.log('query ---------', query);
118159
119- const result = await this . execute ( query . sql , query . params ) ;
160+ const result = await this . execute ( query . sql , query . params , options ) ;
120161 return < Planter [ ] > result . map ( ( planter ) => utils . convertCamel ( planter ) ) ;
121162 } else {
122163 throw 'Connector not defined' ;
123164 }
124165 } catch ( e ) {
125166 console . log ( e ) ;
126- return await this . find ( filter ) ;
167+ return await this . find ( filter , options ) ;
127168 }
128169 }
129170
130- async countWithOrg ( where ?: Where < Planter > ) : Promise < Count > {
131- if ( ! where ) {
132- return await this . count ( where ) ;
171+ async countWithOrg (
172+ where ?: Where < Planter > ,
173+ deviceIdentifier ?: string ,
174+ options ?: Options ,
175+ ) : Promise < Count > {
176+ if ( ! where || deviceIdentifier === null ) {
177+ return await this . count ( where , options ) ;
133178 }
134179
135180 try {
136- const selectStmt = `SELECT COUNT(*) FROM planter ` ;
181+ let selectStmt ;
182+ if ( deviceIdentifier ) {
183+ selectStmt = `SELECT COUNT(*) FROM planter ${ this . getPlanterRegistrationJoinClause (
184+ deviceIdentifier ,
185+ ) } `;
186+ } else {
187+ selectStmt = `SELECT COUNT(*) FROM planter` ;
188+ }
137189
138190 const params = {
139191 filter : { where } ,
0 commit comments