Skip to content

Commit 8c20ed1

Browse files
authored
Merge branch 'master' into cleanup-helper-fns
2 parents 1ccda9b + 2cebcaf commit 8c20ed1

6 files changed

Lines changed: 74 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# [2.21.0](https://github.com/Greenstand/treetracker-admin/compare/v2.20.3...v2.21.0) (2021-11-06)
2+
3+
4+
### Features
5+
6+
* **growers:** filter growers by deviceIdentifier ([#598](https://github.com/Greenstand/treetracker-admin/issues/598)) ([cea9d24](https://github.com/Greenstand/treetracker-admin/commit/cea9d240a9cf9b6b29d325172870a1f3fb399554))
7+
18
## [2.20.3](https://github.com/Greenstand/treetracker-admin/compare/v2.20.2...v2.20.3) (2021-10-27)
29

310

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "treetracker-admin-api",
3-
"version": "2.20.3",
3+
"version": "2.21.0",
44
"private": true,
55
"description": "Treetracker Admin API Server",
66
"keywords": [

src/controllers/planter.controller.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import { Planter, Trees } from '../models';
2020
import { TreesFilter } from './trees.controller';
2121
import { PlanterRepository, TreesRepository } from '../repositories';
2222

23-
// Extend the LoopBack filter types for the Planter model to include organizationId
24-
type PlanterWhere = (Where<Planter> & { organizationId?: number }) | undefined;
23+
// Extend the LoopBack filter types for the Planter model to include organizationId and deviceIdentifier
24+
type PlanterWhere =
25+
| (Where<Planter> & { deviceIdentifier?: string; organizationId?: number })
26+
| undefined;
2527
export type PlanterFilter = Filter<Planter> & { where: PlanterWhere };
2628

2729
export class PlanterController {
@@ -44,6 +46,8 @@ export class PlanterController {
4446
@param.query.object('where', getWhereSchemaFor(Planter))
4547
where?: PlanterWhere,
4648
): Promise<Count> {
49+
const deviceIdentifier = where?.deviceIdentifier;
50+
4751
// Replace organizationId with full entity tree and planter
4852
if (where) {
4953
const { organizationId, ...whereWithoutOrganizationId } = where;
@@ -55,7 +59,7 @@ export class PlanterController {
5559
}
5660
// console.log('get /planter/count where -->', where);
5761

58-
return await this.planterRepository.countWithOrg(where);
62+
return await this.planterRepository.countWithOrg(where, deviceIdentifier);
5963
}
6064

6165
@get('/planter', {
@@ -74,6 +78,8 @@ export class PlanterController {
7478
@param.query.object('filter', getFilterSchemaFor(Planter))
7579
filter?: PlanterFilter,
7680
): Promise<Planter[]> {
81+
const deviceIdentifier = filter?.where?.deviceIdentifier;
82+
7783
// Replace organizationId with full entity tree and planter
7884
if (filter?.where) {
7985
const { organizationId, ...whereWithoutOrganizationId } = filter.where;
@@ -84,7 +90,7 @@ export class PlanterController {
8490
);
8591
}
8692

87-
return await this.planterRepository.findWithOrg(filter);
93+
return await this.planterRepository.findWithOrg(filter, deviceIdentifier);
8894
}
8995

9096
@get('/planter/{id}', {

src/controllers/planterRegistration.controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ export class PlanterRegistrationController {
3333

3434
const sql = `SELECT * FROM planter_registrations
3535
LEFT JOIN (
36-
SELECT region.name AS country, region.geom FROM region, region_type
37-
WHERE region_type.type='country' AND region.type_id=region_type.id
38-
) AS region ON ST_DWithin(region.geom, planter_registrations.geom, 0.01)`;
36+
SELECT
37+
region.name AS country,
38+
region.geom FROM region, region_type
39+
WHERE region_type.type='country'
40+
AND region.type_id=region_type.id
41+
) AS region
42+
ON ST_DWithin(region.geom, planter_registrations.geom, 0.01)`;
3943

4044
const params = {
4145
filter,

src/models/planter.model.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Entity, model, property } from '@loopback/repository';
1+
import { Entity, model, property, hasMany } from '@loopback/repository';
2+
import { PlanterRegistration } from './planterRegistration.model';
23

34
/* eslint-disable @typescript-eslint/no-empty-interface */
45

@@ -183,6 +184,9 @@ export class Planter extends Entity {
183184
})
184185
imageRotation?: Number;
185186

187+
@hasMany(() => PlanterRegistration, { keyTo: 'planterId' })
188+
planterRegs: PlanterRegistration[];
189+
186190
// Define well-known properties here
187191

188192
// Indexer property to allow additional data

src/repositories/planter.repository.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Constructor, inject } from '@loopback/core';
22
import {
33
DefaultCrudRepository,
4+
repository,
5+
HasManyRepositoryFactory,
46
Filter,
7+
Options,
58
Where,
69
Count,
710
} from '@loopback/repository';
@@ -23,16 +26,29 @@ export class PlanterRepository extends UtilsRepositoryMixin<
2326
>(DefaultCrudRepository) {
2427
constructor(
2528
@inject('datasources.treetracker') dataSource: TreetrackerDataSource,
29+
@repository.getter('PlanterRegistrationRepository')
30+
protected planterRegistrationRepositoryGetter: Getter<PlanterRegistrationRepository>,
2631
) {
2732
super(Planter, dataSource);
33+
this.planterRegs = this.createHasManyRepositoryFactoryFor(
34+
'planterRegs',
35+
planterRegistrationRepositoryGetter,
36+
);
37+
this.registerInclusionResolver(
38+
'planterRegs',
39+
this.planterRegs.inclusionResolver,
40+
);
2841
}
2942

3043
// default .find() wasn't applying the org filters
44+
3145
async findWithOrg(
3246
filter?: Filter<Planter>,
47+
deviceIdentifier?: string,
48+
options?: Options,
3349
): Promise<(Planter & PlanterRelations)[]> {
34-
if (!filter) {
35-
return await this.find(filter);
50+
if (!filter || deviceIdentifier === null) {
51+
return await this.find(filter, options);
3652
}
3753

3854
try {
@@ -42,7 +58,14 @@ export class PlanterRepository extends UtilsRepositoryMixin<
4258
filter,
4359
);
4460

45-
const selectStmt = `SELECT ${columnNames} FROM planter `;
61+
let selectStmt;
62+
if (deviceIdentifier) {
63+
selectStmt = `SELECT planter.* FROM planter ${this.getPlanterRegistrationJoinClause(
64+
deviceIdentifier,
65+
)}`;
66+
} else {
67+
selectStmt = `SELECT ${columnNames} FROM planter`;
68+
}
4669

4770
const params = {
4871
filter,
@@ -51,25 +74,37 @@ export class PlanterRepository extends UtilsRepositoryMixin<
5174
};
5275

5376
const query = buildFilterQuery(selectStmt, params);
77+
// console.log('query ---------', query);
5478

55-
const result = await this.execute(query.sql, query.params);
79+
const result = await this.execute(query.sql, query.params, options);
5680
return <Planter[]>result.map((planter) => utils.convertCamel(planter));
5781
} else {
5882
throw 'Connector not defined';
5983
}
6084
} catch (e) {
6185
console.log(e);
62-
return await this.find(filter);
86+
return await this.find(filter, options);
6387
}
6488
}
6589

66-
async countWithOrg(where?: Where<Planter>): Promise<Count> {
67-
if (!where) {
68-
return await this.count(where);
90+
async countWithOrg(
91+
where?: Where<Planter>,
92+
deviceIdentifier?: string,
93+
options?: Options,
94+
): Promise<Count> {
95+
if (!where || deviceIdentifier === null) {
96+
return await this.count(where, options);
6997
}
7098

7199
try {
72-
const selectStmt = `SELECT COUNT(*) FROM planter `;
100+
let selectStmt;
101+
if (deviceIdentifier) {
102+
selectStmt = `SELECT COUNT(*) FROM planter ${this.getPlanterRegistrationJoinClause(
103+
deviceIdentifier,
104+
)}`;
105+
} else {
106+
selectStmt = `SELECT COUNT(*) FROM planter`;
107+
}
73108

74109
const params = {
75110
filter: { where },

0 commit comments

Comments
 (0)