Skip to content

Commit 7ee478f

Browse files
committed
feat: new way to get country leader board
1 parent 05d0060 commit 7ee478f

File tree

1 file changed

+8
-38
lines changed

1 file changed

+8
-38
lines changed

server/infra/database/CountryRepository.ts

+8-38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import log from 'loglevel';
12
import Country from 'interfaces/Country';
23
import HttpError from 'utils/HttpError';
34
import BaseRepository from './BaseRepository';
@@ -88,48 +89,17 @@ export default class CountryRepository extends BaseRepository<Country> {
8889
regionName = 'South America';
8990
break;
9091
default:
91-
regionName = '';
92+
regionName = 'Global';
9293
}
9394

94-
let sql: string;
95-
96-
if (regionName === '') {
97-
sql = `
98-
select r2.*,region.name, ST_AsGeoJSON(centroid) as centroid from (
99-
select count(r.region_id) as planted,r.region_id as region_id from (
100-
select distinct(tree_id),region_id from public.active_tree_region
101-
left join region_type on type_id = region_type.id
102-
where region_type.type = 'country') r
103-
group by region_id
104-
order by planted desc
105-
limit ${10}) r2
106-
left join region
107-
on r2.region_id = region.id
108-
`;
109-
} else {
110-
sql = `
111-
select r.*, region.name, ST_AsGeoJSON(region.centroid) as centroid from (
112-
select count(region.id) as planted, region.id
113-
from (
114-
select trees.* from trees, region c
115-
where c.name = '${regionName}' and ST_WITHIN(trees.estimated_geometric_location, c.geom)
116-
) as trees_in_continent
117-
LEFT JOIN region
118-
on ST_WITHIN(trees_in_continent.estimated_geometric_location, region.geom)
119-
left join region_type
120-
on region.type_id = region_type.id
121-
where
122-
region_type.type = 'country'
123-
group by region.id
124-
order by count(region.id) desc
125-
limit 10
126-
) r left join region
127-
on r.id = region.id
128-
`;
129-
}
95+
const sql = "select * from webmap.config where name = 'country-leader-board'";
13096

13197
const object = await this.session.getDB().raw(sql);
132-
return object.rows;
98+
if (object.rows.length === 0) {
99+
throw new HttpError(404, `Can not found data`);
100+
}
101+
log.warn('countries result:', object);
102+
return object.rows[0].data[regionName];
133103

134104
// The sql is too slow, need to optimize TODO
135105
// return [

0 commit comments

Comments
 (0)