Skip to content

Commit

Permalink
Merge pull request #287 from OlhaD/feat_filter_growers_by_number_of_c…
Browse files Browse the repository at this point in the history
…aptures

#275 - Feat filter growers by number of captures
  • Loading branch information
jeremydthomas authored Mar 26, 2023
2 parents a1ca59f + ae2bb9e commit c3bed49
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
47 changes: 46 additions & 1 deletion server/infra/database/GrowerAccountRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
delete filterObject.organization_id;
}

// if 'captures_amount_max' === 0, 'captures_amount_min' can be only 0.
if (filterObject.captures_amount_max === 0) {
result.whereNull('c.captures_count');
delete filterObject.captures_amount_min;
delete filterObject.captures_amount_max;
}

// if 'captures_amount_max' === 0 and 'captures_amount_max' is not defined, all results should be returned.
if (
filterObject.captures_amount_min === 0 &&
!filterObject.captures_amount_max
) {
delete filterObject.captures_amount_min;
delete filterObject.captures_amount_max;
}

if (filterObject.captures_amount_min) {
result.where(
`c.captures_count`,
'>=',
`${filterObject.captures_amount_min}`,
);
delete filterObject.captures_amount_min;
}

if (filterObject.captures_amount_max) {
result.where(
`c.captures_count`,
'<=',
`${filterObject.captures_amount_max}`,
);
delete filterObject.captures_amount_max;
}

result.where(filterObject);
}

Expand All @@ -116,6 +150,11 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
ON s.id = ${this.tableName}.organization_id
LEFT JOIN messaging.author AS author
ON author.handle = ${this.tableName}.wallet
LEFT JOIN (
SELECT grower_account_id, COUNT(*) AS captures_count
FROM treetracker.capture
GROUP BY grower_account_id
) c ON ${this.tableName}.id = c.grower_account_id
`),
)
.where((builder) => this.filterWhereBuilder(filter, builder))
Expand Down Expand Up @@ -227,7 +266,8 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
${this.tableName}.*,
s.org_name as organization,
d.device_configurations as devices,
r.regions AS regions
r.regions AS regions,
COALESCE(c.captures_count, 0) AS captures
FROM ${this.tableName}
LEFT JOIN treetracker.capture AS tc
ON ${this.tableName}.id = tc.grower_account_id
Expand All @@ -244,6 +284,11 @@ export default class GrowerAccountRepository extends BaseRepository<GrowerAccoun
ON s.id = ${this.tableName}.organization_id
LEFT JOIN messaging.author AS author
ON author.handle = ${this.tableName}.wallet
LEFT JOIN (
SELECT grower_account_id, COUNT(*) AS captures_count
FROM treetracker.capture
GROUP BY grower_account_id
) c ON ${this.tableName}.id = c.grower_account_id
LEFT JOIN (
SELECT tc.grower_account_id, ARRAY_AGG(row_to_json(dc.*)) AS device_configurations
FROM treetracker.capture AS tc
Expand Down
2 changes: 2 additions & 0 deletions server/interfaces/GrowerAccountFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface GrowerAccountFilter extends DbModel {
wallet?: string;
email?: string;
phone?: string;
captures_amount_min?: number;
captures_amount_max?: number;
}

export default GrowerAccountFilter;
4 changes: 4 additions & 0 deletions server/routers/growerAccountsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ router.get(
wallet: Joi.string(),
email: Joi.string(),
phone: Joi.string(),
captures_amount_min: Joi.number().integer().min(0),
captures_amount_max: Joi.number().integer().min(0),
whereNulls: Joi.array(),
whereNotNulls: Joi.array(),
whereIns: Joi.array(),
Expand Down Expand Up @@ -109,6 +111,8 @@ router.get(
wallet: Joi.string(),
email: Joi.string(),
phone: Joi.string(),
captures_amount_min: Joi.number().integer().min(0),
captures_amount_max: Joi.number().integer().min(0),
whereNulls: Joi.array(),
whereNotNulls: Joi.array(),
whereIns: Joi.array(),
Expand Down
8 changes: 8 additions & 0 deletions server/routers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ const queryFormatter = (req) => {
query.organization_id = JSON.parse(organization_id);
}

if (req.query.captures_amount_min) {
query.captures_amount_min = parseInt(req.query.captures_amount_min);
}

if (req.query.captures_amount_max) {
query.captures_amount_max = parseInt(req.query.captures_amount_max);
}

return query;
};

Expand Down

0 comments on commit c3bed49

Please sign in to comment.