Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions server/infra/database/PlanterRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class PlanterRepository extends BaseRepository<Planter> {
`),
)
.where('planter.id', id)
.andWhere('planter.show_in_map', true)
.first();

if (!object) {
Expand Down Expand Up @@ -56,6 +57,7 @@ export default class PlanterRepository extends BaseRepository<Planter> {
AND planter_registrations.created_at = (Select Min(created_at) from planter_registrations as planter_reg where planter_reg.planter_id=planter.id)
LEFT JOIN webmap.planter_location l ON l.id = planter.id
WHERE planter.organization_id in (select entity_id from getEntityRelationshipChildren(${organization_id}))
AND planter.show_in_map = true
${
options.orderBy
? `order by ${options.orderBy.column} ${options.orderBy.direction}`
Expand Down Expand Up @@ -88,6 +90,7 @@ export default class PlanterRepository extends BaseRepository<Planter> {
COUNT(*)
FROM planter
WHERE planter.organization_id in (select entity_id from getEntityRelationshipChildren(${organization_id}))
AND planter.show_in_map = true
`;
const total = await this.session.getDB().raw(totalSql);
return parseInt(total.rows[0].count.toString());
Expand All @@ -105,6 +108,7 @@ export default class PlanterRepository extends BaseRepository<Planter> {
ON planter.id = planter_registrations.planter_id
AND planter_registrations.created_at = (Select Min(created_at) from planter_registrations as planter_reg where planter_reg.planter_id=planter.id)
LEFT JOIN webmap.planter_location l ON l.id = planter.id
WHERE planter.show_in_map = true
${
options.orderBy
? `order by ${options.orderBy.column} ${options.orderBy.direction}`
Expand All @@ -117,6 +121,16 @@ export default class PlanterRepository extends BaseRepository<Planter> {
return object.rows;
}

async countByFilter(filter: Filter) {
const totalSql = `
SELECT COUNT(*)
FROM planter
WHERE planter.show_in_map = true
`;
const total = await this.session.getDB().raw(totalSql);
return parseInt(total.rows[0].count.toString());
}

async getByName(keyword: string, options: FilterOptions) {
const { limit, offset } = options;
const sql = `
Expand All @@ -128,7 +142,8 @@ export default class PlanterRepository extends BaseRepository<Planter> {
LEFT JOIN planter_registrations
ON planter.id = planter_registrations.planter_id
AND planter_registrations.created_at = (Select Min(created_at) from planter_registrations as planter_reg where planter_reg.planter_id=planter.id) LEFT JOIN webmap.planter_location l ON l.id = planter.id
WHERE planter.first_name LIKE '${keyword}%' OR planter.last_name LIKE '${keyword}%'
WHERE (planter.first_name LIKE '${keyword}%' OR planter.last_name LIKE '${keyword}%')
AND planter.show_in_map = true
ORDER BY planter.first_name, planter.last_name
LIMIT ${limit}
OFFSET ${offset}
Expand All @@ -155,7 +170,8 @@ export default class PlanterRepository extends BaseRepository<Planter> {
SELECT
COUNT(*)
FROM planter
WHERE planter.first_name LIKE '${keyword}%' OR planter.last_name LIKE '${keyword}%'
WHERE (planter.first_name LIKE '${keyword}%' OR planter.last_name LIKE '${keyword}%')
AND planter.show_in_map = true
`;
const total = await this.session.getDB().raw(totalSql);
return parseInt(total.rows[0].count.toString());
Expand All @@ -171,8 +187,9 @@ export default class PlanterRepository extends BaseRepository<Planter> {
LEFT JOIN webmap.planter_location l ON l.id = planter.id
join (
SELECT json_array_elements(data -> 'planters') AS planter_id FROM webmap.config WHERE name = 'featured-planter'
) AS t ON
t.planter_id::text::integer = planter.id;
) AS t ON
t.planter_id::text::integer = planter.id
WHERE planter.show_in_map = true
`;
const object = await this.session.getDB().raw(sql);

Expand Down
20 changes: 13 additions & 7 deletions server/infra/database/TreeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export default class TreeRepository extends BaseRepository<Tree> {
planter.organization_id in ( SELECT entity_id from getEntityRelationshipChildren(${organization_id}))
OR
trees.planting_organization_id = ${organization_id}
) and trees.active = true
)
AND trees.active = true
AND planter.show_in_map = true
`;
const total = await this.session.getDB().raw(totalSql);
return parseInt(total.rows[0].count.toString());
Expand All @@ -136,8 +138,8 @@ export default class TreeRepository extends BaseRepository<Tree> {
FROM trees
LEFT JOIN planter ON trees.planter_id = planter.id
LEFT JOIN entity ON entity.id = planter.organization_id
LEFT JOIN tree_species
on trees.species_id = tree_species.id
LEFT JOIN tree_species
on trees.species_id = tree_species.id
LEFT JOIN region
on ST_WITHIN(trees.estimated_geometric_location, region.geom)
and region.type_id in (select id from region_type where type = 'country')
Expand All @@ -147,7 +149,8 @@ export default class TreeRepository extends BaseRepository<Tree> {
OR
trees.planting_organization_id = ${organization_id}
)
and trees.active = true
AND trees.active = true
AND planter.show_in_map = true
LIMIT ${limit}
OFFSET ${offset}
`;
Expand Down Expand Up @@ -260,18 +263,21 @@ export default class TreeRepository extends BaseRepository<Tree> {
const sql = `
SELECT trees.* ,tree_species.name as species_name,
country.id as country_id, country.name as country_name
FROM trees
FROM trees
join (
--- convert json array to row
SELECT json_array_elements(data -> 'trees') AS tree_id FROM webmap.config WHERE name = 'featured-tree'
) AS t ON
) AS t ON
--- cast json type t.tree_id to integer
t.tree_id::text::integer = trees.id
LEFT JOIN tree_species
JOIN planter
ON trees.planter_id = planter.id
LEFT JOIN tree_species
ON trees.species_id = tree_species.id
LEFT JOIN region as country ON ST_WITHIN(trees.estimated_geometric_location, country.geom)
and country.type_id in
(SELECT id FROM region_type WHERE type = 'country')
WHERE planter.show_in_map = true
`;
const object = await this.session.getDB().raw(sql);
return object.rows;
Expand Down
2 changes: 1 addition & 1 deletion server/routers/organizationsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ router.get(
'/',
handlerWrapper(async (req, res) => {
const query = { ...req.query };
query.ids = JSON.parse(req.query.ids);
query.ids = JSON.parse(req.query.ids || '[]');
Joi.assert(
query,
Joi.object().keys({
Expand Down