generated from Greenstand/treetracker-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- v1.34.0
- v1.33.4
- v1.33.3
- v1.33.2
- v1.33.1
- v1.33.0
- v1.32.1
- v1.32.0
- v1.31.0
- v1.30.3
- v1.30.2
- v1.30.1
- v1.30.0
- v1.29.0
- v1.28.0
- v1.27.0
- v1.26.0
- v1.25.1
- v1.25.0
- v1.24.0
- v1.23.8
- v1.23.7
- v1.23.6
- v1.23.5
- v1.23.4
- v1.23.3
- v1.23.2
- v1.23.1
- v1.23.0
- v1.22.1
- v1.22.0
- v1.21.0
- v1.20.0
- v1.19.11
- v1.19.10
- v1.19.9
- v1.19.8
- v1.19.7
- v1.19.6
- v1.19.5
- v1.19.4
- v1.19.3
- v1.19.2
- v1.19.1
- v1.19.0
- v1.18.0
- v1.17.0
- v1.16.1
- v1.16.0
- v1.15.2
- v1.15.1
- v1.15.0
- v1.14.1
- v1.14.0
- v1.13.4
- v1.13.3
- v1.13.2
- v1.13.1
- v1.13.0
- v1.12.6
- v1.12.5
- v1.12.4
- v1.12.3
- v1.12.2
- v1.12.1
- v1.12.0
- v1.11.6
- v1.11.5
- v1.11.4
- v1.11.3
- v1.11.2
- v1.11.1
- v1.11.0
- v1.10.0
- v1.9.5
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.2
- v1.8.1
- v1.8.0
Showing
4 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters