-
-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(9586): implement freetext search in cht datasource #9625
base: master
Are you sure you want to change the base?
Changes from 43 commits
ac0ab67
cd11adc
b7c5d1f
f91e393
52ac11b
c1b68cf
45c0ccf
acf903a
89927b1
45abf58
6f5c326
192af16
bb98dcf
43efbef
ede85fd
2b33c07
3bfcace
074b0c1
1907907
a629116
c45cee8
5400736
bf46e4a
b1cf669
98fd4e2
ee32262
b5bc6e6
4a18fc8
eba1b61
9157059
d5fed9c
30d058e
662598c
89eb816
3ea31ef
a7a9272
ef8b93f
43c3896
f97f928
2065125
f8087b9
e6aea4f
c21e52a
c487a7e
ce1013a
19143b4
8dc7de3
91a0125
49553b7
22da4b9
4a0d2bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const auth = require('../auth'); | ||
const { Contact, Qualifier } = require('@medic/cht-datasource'); | ||
const ctx = require('../services/data-context'); | ||
const serverUtils = require('../server-utils'); | ||
|
||
const getContact = ({ with_lineage }) => ctx.bind(with_lineage === 'true' ? Contact.v1.getWithLineage : Contact.v1.get); | ||
const getContactIds = () => ctx.bind(Contact.v1.getIdsPage); | ||
|
||
const checkUserPermissions = async (req) => { | ||
const userCtx = await auth.getUserCtx(req); | ||
if (!auth.isOnlineOnly(userCtx) || !auth.hasAllPermissions(userCtx, 'can_view_contacts')) { | ||
return Promise.reject({ code: 403, message: 'Insufficient privileges' }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
v1: { | ||
get: serverUtils.doOrError(async (req, res) => { | ||
await checkUserPermissions(req); | ||
const { uuid } = req.params; | ||
const contact = await getContact(req.query)(Qualifier.byUuid(uuid)); | ||
|
||
if (!contact) { | ||
return serverUtils.error({ status: 404, message: 'Contact not found' }, req, res); | ||
} | ||
|
||
return res.json(contact); | ||
}), | ||
getIds: serverUtils.doOrError(async (req, res) => { | ||
await checkUserPermissions(req); | ||
|
||
if (!req.query.freetext && !req.query.type) { | ||
return serverUtils.error({ status: 400, message: 'Either query param freetext or type is required' }, req, res); | ||
} | ||
const qualifier = {}; | ||
|
||
if (req.query.freetext) { | ||
Object.assign(qualifier, Qualifier.byFreetext(req.query.freetext)); | ||
} | ||
|
||
if (req.query.type) { | ||
Object.assign(qualifier, Qualifier.byContactType(req.query.type)); | ||
} | ||
|
||
const docs = await getContactIds()(qualifier, req.query.cursor, req.query.limit); | ||
|
||
return res.json(docs); | ||
}), | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const auth = require('../auth'); | ||
const ctx = require('../services/data-context'); | ||
const serverUtils = require('../server-utils'); | ||
const { Report, Qualifier } = require('@medic/cht-datasource'); | ||
|
||
const getReport = () => ctx.bind(Report.v1.get); | ||
const getReportIds = () => ctx.bind(Report.v1.getIdsPage); | ||
|
||
const checkUserPermissions = async (req) => { | ||
const userCtx = await auth.getUserCtx(req); | ||
if (!auth.isOnlineOnly(userCtx) || !auth.hasAllPermissions(userCtx, 'can_view_reports')) { | ||
return Promise.reject({ code: 403, message: 'Insufficient privileges' }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
v1: { | ||
get: serverUtils.doOrError(async (req, res) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a big fan of this callback style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've been doing this pattern for all the REST endpoints that call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a try-catch block is not so much duplication, and it's more transparent than a nested callback. |
||
await checkUserPermissions(req); | ||
const { uuid } = req.params; | ||
const report = await getReport()(Qualifier.byUuid(uuid)); | ||
|
||
if (!report) { | ||
return serverUtils.error({ status: 404, message: 'Report not found' }, req, res); | ||
} | ||
|
||
return res.json(report); | ||
}), | ||
getIds: serverUtils.doOrError(async (req, res) => { | ||
await checkUserPermissions(req); | ||
|
||
const qualifier = Qualifier.byFreetext(req.query.freetext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So ... this endpoint .. if it doesn't get neither a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
const docs = await getReportIds()(qualifier, req.query.cursor, req.query.limit); | ||
|
||
return res.json(docs); | ||
}) | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,10 @@ const exportData = require('./controllers/export-data'); | |
const records = require('./controllers/records'); | ||
const forms = require('./controllers/forms'); | ||
const users = require('./controllers/users'); | ||
const contact = require('./controllers/contact'); | ||
const person = require('./controllers/person'); | ||
const place = require('./controllers/place'); | ||
const report = require('./controllers/report'); | ||
const { people, places } = require('@medic/contacts')(config, db, dataContext); | ||
const upgrade = require('./controllers/upgrade'); | ||
const settings = require('./controllers/settings'); | ||
|
@@ -492,6 +494,12 @@ app.postJson('/api/v1/people', function(req, res) { | |
app.get('/api/v1/person', person.v1.getAll); | ||
app.get('/api/v1/person/:uuid', person.v1.get); | ||
|
||
app.get('/api/v1/contact/id', contact.v1.getIds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, REST API conventions are to name the API endpoint in a plural way like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That discussion happened in the parent ticket before we spun off the child isssue: #9544 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @jkuester . Your argument here is that "we've already decided and your input is not welcome?" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for being aggressive and confrontational in the above comment. I maintain my comment about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just trying to provide the context for the discussion that Sugat referenced. 😬 I am happy to continue the design discussion here to come to an agreed upon approach. It will just be most efficient if we all understand what was already said to get us here. When starting work on new REST endpoints for the cht-datasource code, we chose to go with the pattern of singular entity names (so
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with being consistent. I personally can't recall seeing APIs in the world that used this singular form, so for me this seems quite unintuitive. |
||
app.get('/api/v1/contact/:uuid', contact.v1.get); | ||
|
||
app.get('/api/v1/report/id', report.v1.getIds); | ||
app.get('/api/v1/report/:uuid', report.v1.get); | ||
|
||
app.postJson('/api/v1/bulk-delete', bulkDocs.bulkDelete); | ||
|
||
// offline users are not allowed to hydrate documents via the hydrate API | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in an earlier comment, ideally we should be able to use a
Qualifier.and
function to combine qualifier objects. Something like: