Skip to content

Commit

Permalink
feat: update trees route to support planted date range filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dagmawig committed Apr 10, 2022
1 parent 2fb842b commit ec72fd2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
19 changes: 19 additions & 0 deletions __tests__/e2e/trees.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ describe('trees', () => {
1000 * 60,
);

it(
'trees?startDate=2021-02-26&endDate=2021-12-22',
async () => {
const response = await supertest(app).get(
'/trees?startDate=2021-02-26&endDate=2021-12-22',
);
expect(response.status).toBe(200);
for (let i = 0; i < response.body.trees.length; i++) {
expect(
new Date(response.body.trees[i].time_created).getTime(),
).toBeGreaterThanOrEqual(new Date('2021-02-26T00:00:00Z').getTime());
expect(
new Date(response.body.trees[i].time_created).getTime(),
).toBeLessThan(new Date('2021-12-23T00:00:00Z').getTime());
}
},
1000 * 60,
);

it(
'trees/featured',
async () => {
Expand Down
5 changes: 5 additions & 0 deletions docs/api/spec/query-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ paths:
- schema:
type: string
in: query
name: startDate & endDate
description: Filter by date range
- schema:
type: string
in: query
/trees/featured:
get:
summary: get featured tree list
Expand Down
22 changes: 22 additions & 0 deletions server/infra/database/TreeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,26 @@ export default class TreeRepository extends BaseRepository<Tree> {
const object = await this.session.getDB().raw(sql);
return object.rows;
}

async getByDateRange(
date_range: { startDate: string; endDate: string },
options: FilterOptions,
) {
const { limit, offset } = options;
const startDateISO = `${date_range.startDate }T00:00:00.000Z`;
const endDateISO = new Date(
new Date(`${date_range.endDate }T00:00:00.000Z`).getTime() + 86400000,
).toISOString();
const sql = `
SELECT
*
FROM trees
WHERE time_created >= '${startDateISO}'::timestamp
AND time_created < '${endDateISO}'::timestamp
LIMIT ${limit}
OFFSET ${offset}
`;
const object = await this.session.getDB().raw(sql);
return object.rows;
}
}
12 changes: 11 additions & 1 deletion server/models/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import Tree from 'interfaces/Tree';
import { delegateRepository } from '../infra/database/delegateRepository';
import TreeRepository from '../infra/database/TreeRepository';

type Filter = Partial<{ organization_id: number }>;
type Filter = Partial<{
organization_id: number;
date_range: { startDate: string; endDate: string };
}>;

function getByFilter(
treeRepository: TreeRepository,
Expand All @@ -17,6 +20,13 @@ function getByFilter(
options,
);
return trees;
} if (filter.date_range) {
log.warn('using date range filter...');
const trees = await treeRepository.getByDateRange(
filter.date_range,
options,
);
return trees;
}
const trees = await treeRepository.getByFilter(filter, options);
return trees;
Expand Down
21 changes: 18 additions & 3 deletions server/routers/treesRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import TreeRepository from '../infra/database/TreeRepository';
import TreeModel from '../models/Tree';

const router = express.Router();
type Filter = Partial<{ planter_id: number; organization_id: number }>;
type Filter = Partial<{
planter_id: number;
organization_id: number;
date_range: { startDate: string; endDate: string };
}>;

router.get(
'/featured',
Expand Down Expand Up @@ -43,22 +47,33 @@ router.get(
organization_id: Joi.number().integer().min(0),
limit: Joi.number().integer().min(1).max(1000),
offset: Joi.number().integer().min(0),
startDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
endDate: Joi.string().regex(/^\d{4}-\d{2}-\d{2}$/),
}),
);
const { limit = 20, offset = 0, planter_id, organization_id } = req.query;
const {
limit = 20,
offset = 0,
planter_id,
organization_id,
startDate,
endDate,
} = req.query;
const repo = new TreeRepository(new Session());
const filter: Filter = {};
if (planter_id) {
filter.planter_id = planter_id;
} else if (organization_id) {
filter.organization_id = organization_id;
} else if (startDate && endDate) {
filter.date_range = { startDate, endDate };
}
const result = await TreeModel.getByFilter(repo)(filter, {
limit,
offset,
});
res.send({
total: null,
total: startDate && endDate ? result.length : null,
offset,
limit,
trees: result,
Expand Down

0 comments on commit ec72fd2

Please sign in to comment.