Skip to content

Commit

Permalink
Showing 4 changed files with 133 additions and 1 deletion.
31 changes: 31 additions & 0 deletions server/handlers/planterHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Joi = require('joi');
const Session = require('../infra/database/Session');
const PlanterRepository = require('../infra/repositories/PlanterRepository');
const { getPlanters } = require('../models/Planter');

const planterQuerySchema = Joi.object({
organization_id: Joi.string().guid(),
limit: Joi.number().integer().greater(0).less(51),
offset: Joi.number().integer().greater(-1),
});

const planterHandlerGet = async function (req, res) {
await planterQuerySchema.validateAsync(req.query, { abortEarly: false });
const session = new Session();
const planterRepo = new PlanterRepository(session);
const organization_id = req.query.organization_id;

const url = `planters${
organization_id ? `?organization_id=${organization_id}` : ''
}`;

const executeGetPlanters = getPlanters(planterRepo);
const result = await executeGetPlanters(req.query, url);

res.send(result);
res.end();
};

module.exports = {
planterHandlerGet,
};
23 changes: 23 additions & 0 deletions server/infra/repositories/PlanterRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const BaseRepository = require('./BaseRepository');

class PlanterRepository extends BaseRepository {
constructor(session) {
super('planter', session);
this._tableName = 'planter';
this._session = session;
}

async getPlantersByOrganization(organization_id, options) {
const limit = options.limit;
const offset = options.offset;
const planters = await this._session.getDB().raw(
`SELECT planter.* FROM public.planter JOIN public.entity ON planter.organization_id = entity.id WHERE stakeholder_uuid = ?
${limit ? `LIMIT ${limit}` : ''} ${offset ? `OFFSET ${offset}` : ''}`,
[organization_id],
);

return planters.rows;
}
}

module.exports = PlanterRepository;
71 changes: 71 additions & 0 deletions server/models/Planter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const Planter = ({
id,
first_name,
last_name,
email,
organization,
phone,
pwd_reset_required,
image_url,
person_id,
ordanization_id,
image_rotation,
}) => {
return Object.freeze({
id,
first_name,
last_name,
email,
organization,
phone,
pwd_reset_required,
image_url,
person_id,
ordanization_id,
image_rotation,
});
};

const QueryOptions = ({ limit = undefined, offset = undefined }) => {
return Object.entries({ limit, offset, order, orderBy: sort_by })
.filter((entry) => entry[1] !== undefined)
.reduce((result, item) => {
result[item[0]] = item[1];
return result;
}, {});
};

const getPlanters = (planterRepo) => async (filterCriteria, url) => {
const organization_id = filterCriteria?.organization_id;
const options = { ...options, ...QueryOptions({ ...filterCriteria }) };

const queryFilterObjects = { ...filterCriteria };
queryFilterObjects.limit = options.limit;

// remove offset property, as it is calculated later
delete queryFilterObjects.offset;

const urlWithLimitAndOffset = `${url}?${query}&offset=`;

const next = `${urlWithLimitAndOffset}${+options.offset + +options.limit}`;
let prev = null;
if (options.offset - +options.limit >= 0) {
prev = `${urlWithLimitAndOffset}${+options.offset - +options.limit}`;
}

const planters = organization_id
? await planterRepo.getPlantersByOrganization(organization_id, options)
: await planterRepo.getByFilter(filterCriteria, options);

return {
planters: planters.map((row) => Planter(row)),
links: {
prev,
next,
},
};
};

module.exports = {
getPlanters,
};
9 changes: 8 additions & 1 deletion server/routes.js
Original file line number Diff line number Diff line change
@@ -14,13 +14,20 @@ const {
treeHandlerGet,
treeHandlerGetPotentialMatches,
} = require('./handlers/treeHandler.js');
const { planterHandlerGet } = require('./handlers/planterHandler.js');

router.post('/captures', validateRequest, captureHandlerPost);
router.get('/captures', validateRequest, captureHandlerGet);
router.patch('/captures/:capture_id', validateRequest, captureHandlerPatch);

router.post('/trees', validateRequest, treeHandlerPost);
router.get('/trees', validateRequest, treeHandlerGet);
router.get('/trees/potential_matches', validateRequest, treeHandlerGetPotentialMatches);
router.get(
'/trees/potential_matches',
validateRequest,
treeHandlerGetPotentialMatches,
);

router.get('/planters', planterHandlerGet);

module.exports = router;

0 comments on commit 41ab16e

Please sign in to comment.