Skip to content

Commit

Permalink
fix: contract ids to be type string
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Nov 10, 2023
2 parents 272ec97 + a54e2b9 commit 43b4a69
Show file tree
Hide file tree
Showing 39 changed files with 951 additions and 51 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {

rules: {
// disabled or modified because too strict
'no-nested-ternary': 'off',
'no-restricted-globals': 'warn',
'no-console': ['warn', { allow: ['info', 'error'] }],
'import/prefer-default-export': 'off',
Expand Down
124 changes: 124 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,127 @@
## [1.70.7](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.6...v1.70.7) (2023-10-21)

### Bug Fixes

- added new type allowance for organization_id ([494e17b](https://github.com/Greenstand/treetracker-query-api/commit/494e17b8d694ee0489e96bf047e38164639a2e59))

## [1.70.6](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.5...v1.70.6) (2023-10-04)

### Bug Fixes

- queryFormatter ([6a33308](https://github.com/Greenstand/treetracker-query-api/commit/6a33308ed4adf7315066dbcbf272c933e0ca08d7))

## [1.70.5](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.4...v1.70.5) (2023-09-27)

### Bug Fixes

- species v2 ([7883430](https://github.com/Greenstand/treetracker-query-api/commit/788343072c25ae8a0d0936ffbbba3cea0b911128))

## [1.70.4](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.3...v1.70.4) (2023-09-16)

### Bug Fixes

- do not display inactive trees, and throw 404 error ([9bafbf6](https://github.com/Greenstand/treetracker-query-api/commit/9bafbf6f3b8691f56fd0a374912444394cfb0d2a))

## [1.70.3](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.2...v1.70.3) (2023-08-16)

### Bug Fixes

- species total bug ([70a64ab](https://github.com/Greenstand/treetracker-query-api/commit/70a64abd2995806fc50f3fa494d079d265a7323d))

## [1.70.2](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.1...v1.70.2) (2023-08-16)

### Bug Fixes

- bug with total by a commit ([32df39a](https://github.com/Greenstand/treetracker-query-api/commit/32df39a98cd8673d71602bbb8c4471c38ec53ba6))

## [1.70.1](https://github.com/Greenstand/treetracker-query-api/compare/v1.70.0...v1.70.1) (2023-07-31)

### Bug Fixes

- trigger deployment ([4f6e363](https://github.com/Greenstand/treetracker-query-api/commit/4f6e363bc6419f3e4afcad368d289a80af1b07a2))

# [1.70.0](https://github.com/Greenstand/treetracker-query-api/compare/v1.69.2...v1.70.0) (2023-07-19)

### Features

- trees/uuid ([4ea7d2c](https://github.com/Greenstand/treetracker-query-api/commit/4ea7d2cd55a6565f72acc22ebba3f10d25275869))

## [1.69.2](https://github.com/Greenstand/treetracker-query-api/compare/v1.69.1...v1.69.2) (2023-07-04)

### Bug Fixes

- minor update ([ffd264a](https://github.com/Greenstand/treetracker-query-api/commit/ffd264a0f78d003fb004eef6759ef53032b01994))
- wallet about ([481707d](https://github.com/Greenstand/treetracker-query-api/commit/481707d0d434fc47b4acfecff9bb469b403ef453))

## [1.69.1](https://github.com/Greenstand/treetracker-query-api/compare/v1.69.0...v1.69.1) (2023-06-15)

### Bug Fixes

- shows only acitve trees for GET /tokens ([7cdeb02](https://github.com/Greenstand/treetracker-query-api/commit/7cdeb02c4deaab7443119a5e3c5a28efed984864)), closes [#322](https://github.com/Greenstand/treetracker-query-api/issues/322)

# [1.69.0](https://github.com/Greenstand/treetracker-query-api/compare/v1.68.2...v1.69.0) (2023-06-04)

### Features

- neartest wallet case ([8e1182a](https://github.com/Greenstand/treetracker-query-api/commit/8e1182a96a7c6d766ddae8add2affd376dcbf7f7))

## [1.68.2](https://github.com/Greenstand/treetracker-query-api/compare/v1.68.1...v1.68.2) (2023-06-03)

### Bug Fixes

- support wallet name, map name, format mistake ([b4cb0b6](https://github.com/Greenstand/treetracker-query-api/commit/b4cb0b62fbf9d69aff98585129dabe400b65f010))

## [1.68.1](https://github.com/Greenstand/treetracker-query-api/compare/v1.68.0...v1.68.1) (2023-06-03)

### Bug Fixes

- bound is wrong to limit the neearest request ([b76abf3](https://github.com/Greenstand/treetracker-query-api/commit/b76abf35c4461d17260a1886f498c5f01ca5ad76))

# [1.68.0](https://github.com/Greenstand/treetracker-query-api/compare/v1.67.4...v1.68.0) (2023-06-02)

### Features

- git location nearest ([a6bec57](https://github.com/Greenstand/treetracker-query-api/commit/a6bec57ae092529068fd902e6d63d0b4851c3f11))

## [1.67.4](https://github.com/Greenstand/treetracker-query-api/compare/v1.67.3...v1.67.4) (2023-05-27)

### Bug Fixes

- version trigger ([cc24027](https://github.com/Greenstand/treetracker-query-api/commit/cc24027f9ced7a3aee66f8bbf703cc21394206b9))

## [1.67.3](https://github.com/Greenstand/treetracker-query-api/compare/v1.67.2...v1.67.3) (2023-05-27)

### Bug Fixes

- wrong planter count ([14dae92](https://github.com/Greenstand/treetracker-query-api/commit/14dae9219a6baf4504e596969cc9615583822bb9))
- wrong tree count ([e7b4cab](https://github.com/Greenstand/treetracker-query-api/commit/e7b4cab29273beea9e7ed1f2dd74372f66e4c67e))

## [1.67.2](https://github.com/Greenstand/treetracker-query-api/compare/v1.67.1...v1.67.2) (2023-05-20)

### Bug Fixes

- remove `v2` and let's the client to decide the version number ([bb8e730](https://github.com/Greenstand/treetracker-query-api/commit/bb8e730ceea2a09d7a2636605d859e8f053fcb31))

## [1.67.1](https://github.com/Greenstand/treetracker-query-api/compare/v1.67.0...v1.67.1) (2023-05-04)

### Bug Fixes

- uuid capital problem ([fac9485](https://github.com/Greenstand/treetracker-query-api/commit/fac9485658f4b83af814d701ef32dd8ae4958dbd))

# [1.67.0](https://github.com/Greenstand/treetracker-query-api/compare/v1.66.0...v1.67.0) (2023-04-25)

### Features

- add grower_reference_id as a filter field for captures ([b6bea8c](https://github.com/Greenstand/treetracker-query-api/commit/b6bea8cd210a6869b312d95b119c2bbed65cdee8))
- add grower_reference_id as a filter field for verify ([3a8b1cc](https://github.com/Greenstand/treetracker-query-api/commit/3a8b1ccdb9fc6d03e0b4126e022d77a6679286ef))
- add reference_id as a valid field for growers query ([a92728a](https://github.com/Greenstand/treetracker-query-api/commit/a92728aa0d18ceee060aa60115edf30a45552a7f))

# [1.66.0](https://github.com/Greenstand/treetracker-query-api/compare/v1.65.0...v1.66.0) (2023-04-25)

### Features

- make results of captures query distinct to avoid duplicates ([7d99679](https://github.com/Greenstand/treetracker-query-api/commit/7d996798bfbf8cfd66678e12c4061c84709883f3))

# [1.65.0](https://github.com/Greenstand/treetracker-query-api/compare/v1.64.0...v1.65.0) (2023-03-29)

### Bug Fixes
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ npm ci
Run the server with database settings:

```
DATABASE_URL=[...] npm run server
DATABASE_URL=[...] npm run dev
```

Please join our slack channel to get help with setting up the database.
Expand Down Expand Up @@ -142,3 +142,6 @@ DATABASE_URL=[the link provided] NODE_TLS_REJECT_UNAUTHORIZED='0' npm run test-e
.
.
.
.
.
.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "treetracker-query-api",
"version": "1.65.0",
"version": "1.70.7",
"private": false,
"keywords": [
"ecology"
Expand Down
5 changes: 4 additions & 1 deletion server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import boundsRouter from './routers/boundsRouter';
import capturesRouter from './routers/capturesRouter';
import contractsRouter from './routers/contractsRouter';
import countriesRouter from './routers/countriesRouter';
import gisRouter from './routers/gisRouter';
import growerAccountsRouter from './routers/growerAccountsRouter';
import organizationsRouter from './routers/organizationsRouter';
import plantersRouter from './routers/plantersRouter';
import rawCapturesRouter from './routers/rawCapturesRouter';
import speciesRouter from './routers/speciesRouter';
import speciesRouterV2 from './routers/speciesRouterV2';
import tokensRouter from './routers/tokensRouter';
import transactionsRouter from './routers/transactionsRouter';
import treesRouter from './routers/treesRouter';
Expand Down Expand Up @@ -67,7 +69,7 @@ app.use('/planters', plantersRouter);
app.use('/organizations', organizationsRouter);
app.use('/v2/organizations', organizationsRouterV2);
app.use('/species', speciesRouter);
app.use('/v2/species', speciesRouter);
app.use('/v2/species', speciesRouterV2);
app.use('/wallets', walletsRouter);
app.use('/v2/wallets', walletsRouter);
app.use('/transactions', transactionsRouter);
Expand All @@ -78,6 +80,7 @@ app.use('/raw-captures', rawCapturesRouter);
app.use('/v2/growers', growerAccountsRouter);
app.use('/v2/trees', treesRouterV2);
app.use('/bounds', boundsRouter);
app.use('/gis', gisRouter);
app.use('/contract', contractsRouter);
// Global error handler
app.use(errorHandler);
Expand Down
4 changes: 3 additions & 1 deletion server/infra/database/BoundsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export default class BoundsRepository {
return BoundsRepository.convertStringToBounds(bounds);
}

async filterByOrganisation(organisationId: string): Promise<Bounds> {
async filterByOrganisation(
organisationId: string[] | string,
): Promise<Bounds> {
const organisationBoundsSql = `
select
ST_EXTENT(ST_GeomFromText('POINT(' || t.lon || ' ' || t.lat || ')', 4326)) as bounds
Expand Down
12 changes: 11 additions & 1 deletion server/infra/database/CaptureRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export default class CaptureRepository extends BaseRepository<Capture> {
delete filterObject.reference_id;
}

if (filterObject.grower_reference_id) {
result.where(
`treetracker.grower_account.reference_id`,
'=',
filterObject.grower_reference_id,
);
delete filterObject.grower_reference_id;
}

if (filterObject.organization_id) {
result.where(`${this.tableName}.planting_organization_id`, 'in', [
...filterObject.organization_id,
Expand Down Expand Up @@ -154,7 +163,8 @@ export default class CaptureRepository extends BaseRepository<Capture> {
`,
),
)
.where((builder) => this.filterWhereBuilder(filter, builder));
.where((builder) => this.filterWhereBuilder(filter, builder))
.distinct();

promise = promise.orderBy(
sort?.order_by || 'treetracker.capture.created_at',
Expand Down
2 changes: 1 addition & 1 deletion server/infra/database/ContractRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default class ContractRepository extends BaseRepository<Contract> {
return result[0].count;
}

async getById(id: string | number) {
async getById(id: string) {
const object = await this.session
.getDB()
.select(
Expand Down
96 changes: 96 additions & 0 deletions server/infra/database/GisRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Bounds from 'interfaces/Bounds';

Check warning on line 1 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

'Bounds' is defined but never used
import HttpError from 'utils/HttpError';

Check warning on line 2 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

'HttpError' is defined but never used
import Session from './Session';

export default class GisRepository {
session: Session;

constructor(session: Session) {
this.session = session;
}

async getNearest(params): Promise<unknown> {
let { zoom_level, lng, lat } = params;
zoom_level = parseInt(zoom_level);
lng = parseFloat(lng);
lat = parseFloat(lat);
console.log('lng:', lng);

Check warning on line 17 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

Unexpected console statement
let sql;
if (zoom_level <= 11) {
sql = `
SELECT
ST_ASGeoJson(centroid)
FROM
active_tree_region
WHERE
zoom_level = ${zoom_level}
ORDER BY
active_tree_region.centroid <->
ST_SetSRID(ST_MakePoint(${lng}, ${lat}),4326)
LIMIT 1;
`;
} else if (zoom_level < 15 && zoom_level > 11) {
sql = `
SELECT
ST_ASGeoJson(location)
FROM
clusters
ORDER BY
location <->
ST_SetSRID(ST_MakePoint(${lng}, ${lat}),4326)
LIMIT 1;
`;
} else if (zoom_level >= 15) {
sql = `
WITH box AS (
SELECT ST_Extent(ST_MakeLine(ST_Project(ST_SetSRID(ST_MakePoint(${lng}, ${lat}),4326), 10000, radians(45))::geometry, ST_Project(ST_SetSRID(ST_MakePoint(${lng}, ${lat}),4326), 10000, radians(225))::geometry)) AS geom
)
SELECT
ST_ASGeoJson(estimated_geometric_location)
FROM
trees
${
params.wallet_id
? 'join wallet.token on trees.uuid = wallet.token.capture_id::text join wallet.wallet on wallet.token.wallet_id = wallet.wallet.id '
: ''
}
${
params.organization_id || params.map_name
? 'join planter on trees.planter_id = planter.id'
: ''
}
WHERE
active = true
${
params.wallet_id
? /[0-9a-f-]{36}/.test(params.wallet_id)
? `and wallet.token.wallet_id = '${params.wallet_id}' `
: `and wallet.wallet.name = '${params.wallet_id}'`
: ''
}
${params.planter_id ? `and planter_id = ${params.planter_id}` : ''}
${
params.organization_id
? `and planter.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${params.organization_id}))`
: ''
}
${
params.map_name
? `and planter.organization_id in ( SELECT entity_id from getEntityRelationshipChildren((select id from entity where map_name = '${params.map_name}')))`
: ''
}
and estimated_geometric_location && (select geom from box)
ORDER BY
estimated_geometric_location <->
ST_SetSRID(ST_MakePoint(${lng}, ${lat}),4326)
LIMIT 1;
`;
}
console.log('query:', sql);

Check warning on line 89 in server/infra/database/GisRepository.ts

View workflow job for this annotation

GitHub Actions / Run all tests

Unexpected console statement
const result = await this.session.getDB().raw(sql);
// {"st_asgeojson":"{\"type\":\"Point\",\"coordinates\":[39.1089215842116,-5.12839483715479]}"}
return result.rows.length > 0
? JSON.parse(result.rows[0].st_asgeojson)
: null;
}
}
19 changes: 15 additions & 4 deletions server/infra/database/GrowerAccountRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
delete filterObject.id;
}

if (filterObject.reference_id) {
result.where(
`${this.tableName}.reference_id`,
'=',
filterObject.reference_id,
);
delete filterObject.reference_id;
}

if (filterObject.first_name) {
result.where(
`${this.tableName}.first_name`,
Expand Down Expand Up @@ -91,9 +100,11 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
}

if (filterObject.organization_id) {
result.where(`${this.tableName}.organization_id`, 'in', [
...filterObject.organization_id,
]);
result.where(
`${this.tableName}.organization_id`,
'=',
`${filterObject.organization_id}`,
);
delete filterObject.organization_id;
}

Expand Down Expand Up @@ -357,7 +368,7 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
.select(
knex.raw(`
COUNT(${this.tableName}.id) AS count
FROM ${this.tableName}
FROM ${this.tableName}
`),
)
.where((builder) => this.filterWhereBuilder(filter, builder))
Expand Down
Loading

0 comments on commit 43b4a69

Please sign in to comment.