diff --git a/server/infra/database/SpeciesRepository.ts b/server/infra/database/SpeciesRepository.ts index ce75b0c..3bc8ec6 100644 --- a/server/infra/database/SpeciesRepository.ts +++ b/server/infra/database/SpeciesRepository.ts @@ -43,30 +43,31 @@ export default class SpeciesRepository extends BaseRepository { async countByOrganization(organization_id: number) { const totalSql = ` SELECT - species_id as id, total, ts.name, ts.desc - FROM - ( - SELECT - ss.species_id, count(ss.species_id) as total - from webmap.species_stat ss - WHERE - ss.planter_id IN ( - SELECT - id - FROM planter p + species_id as id, total, ts.name, ts.desc + FROM + ( + SELECT + ss.species_id, count(ss.species_id) as total + from webmap.species_stat ss WHERE - p.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${organization_id})) - ) - OR - ss.planting_organization_id = ${organization_id} - GROUP BY ss.species_id - ) s_count - JOIN tree_species ts - ON ts.id = s_count.species_id - ORDER BY total DESC + ss.planter_id IN ( + SELECT + id + FROM planter p + WHERE + p.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${organization_id})) + ) + OR + ss.planting_organization_id = ${organization_id} + GROUP BY ss.species_id + ) s_count + JOIN tree_species ts + ON ts.id = s_count.species_id + ORDER BY total DESC + `; const total = await this.session.getDB().raw(totalSql); - return parseInt(total.rows[0].count.toString()); + return parseInt(total.rows.length); } async getByPlanter(planter_id: number, options: FilterOptions) { diff --git a/server/infra/database/SpeciesRepositoryV2.ts b/server/infra/database/SpeciesRepositoryV2.ts index b4dcb32..1974645 100644 --- a/server/infra/database/SpeciesRepositoryV2.ts +++ b/server/infra/database/SpeciesRepositoryV2.ts @@ -66,7 +66,7 @@ export default class SpeciesRepositoryV2 extends BaseRepository { ORDER BY total DESC `; const total = await this.session.getDB().raw(totalSql); - return parseInt(total.rows[0].count.toString()); + return parseInt(total.rows.length); } async getByPlanter(planter_id: number, options: FilterOptions) {