Skip to content

Commit

Permalink
Simplify user profile short code APIs and reduce views
Browse files Browse the repository at this point in the history
  • Loading branch information
esurface committed Jul 18, 2024
1 parent c95317d commit 15cbac9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class ImportUserProfileComponent implements AfterContentInit {
try {
this.appConfig = await this.appConfigService.getAppConfig()
this.shortCode = this.userShortCodeInput.nativeElement.value;
let newUserProfile = await this.http.get(`${this.appConfig.serverUrl}api/${this.appConfig.groupId}/responsesByUserProfileShortCode/${this.shortCode}/?userProfile=true`).toPromise()
if(!!newUserProfile){
let existingUserProfile = await this.http.get(`${this.appConfig.serverUrl}api/${this.appConfig.groupId}/userProfileByShortCode/${this.shortCode}`).toPromise()
if(!!existingUserProfile){
const username = this.userService.getCurrentUser()
this.state = this.STATE_SYNCING
await this.userService.saveUserAccount({ ...this.userAccount, userUUID: newUserProfile['_id'], initialProfileComplete: true })
await this.userService.saveUserAccount({ ...this.userAccount, userUUID: existingUserProfile['_id'], initialProfileComplete: true })
this.totalDocs = (await this.http.get(`${this.appConfig.serverUrl}api/${this.appConfig.groupId}/responsesByUserProfileShortCode/${this.shortCode}/?totalRows=true`).toPromise())['totalDocs']
const docsToQuery = 1000;
let previousProcessedDocs = await this.variableService.get(`${username}-processedDocs`)
Expand Down
1 change: 1 addition & 0 deletions server/src/express-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ app.get('/app/:groupId/responsesByMonthAndFormId/:keys/:limit?/:skip?', isAuthen
// Note that the lack of security middleware here is intentional. User IDs are UUIDs and thus sufficiently hard to guess.
app.get('/api/:groupId/responsesByUserProfileId/:userProfileId/:limit?/:skip?', require('./routes/group-responses-by-user-profile-id.js'))
app.get('/api/:groupId/responsesByUserProfileShortCode/:userProfileShortCode/:limit?/:skip?', require('./routes/group-responses-by-user-profile-short-code.js'))
app.get('/api/:groupId/userProfileByShortCode/:userProfileShortCode', require('./routes/group-user-profile-by-short-code.js'))
app.get('/api/:groupId/:docId', isAuthenticatedOrHasUploadToken, require('./routes/group-doc-read.js'))
app.put('/api/:groupId/:docId', isAuthenticated, require('./routes/group-doc-write.js'))
app.post('/api/:groupId/:docId', isAuthenticated, require('./routes/group-doc-write.js'))
Expand Down
29 changes: 6 additions & 23 deletions server/src/group-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ module.exports.unpaid = function(doc) {
}
}

module.exports.responsesByUserProfileShortCode = function(doc) {
if (doc.collection === "TangyFormResponse") {
module.exports.responsesByUserProfileShortCode = {
map: function (doc) {
if (doc.form && doc.form.id === 'user-profile') {
return emit(doc._id.substr(doc._id.length-6, doc._id.length), true)
return emit(doc._id.substr(doc._id.length-6, doc._id.length), 1)
}
var inputs = doc.items.reduce(function(acc, item) { return acc.concat(item.inputs)}, [])
var userProfileInput = null
Expand All @@ -72,34 +72,17 @@ module.exports.responsesByUserProfileShortCode = function(doc) {
}
})
if (userProfileInput) {
emit(userProfileInput.value.substr(userProfileInput.value.length-6, userProfileInput.value.length), true)
emit(userProfileInput.value.substr(userProfileInput.value.length-6, userProfileInput.value.length), 1)
}
}
},
reduce: '_count'
}

module.exports.userProfileByUserProfileShortCode = function (doc) {
if (doc.collection === "TangyFormResponse" && doc.form && doc.form.id === 'user-profile') {
return emit(doc._id.substr(doc._id.length - 6, doc._id.length), true);
}
}
module.exports.totalDocsByUserProfileShortCode = {
map: function (doc) {
if (doc.form && doc.form.id === 'user-profile') {
return emit(doc._id.substr(doc._id.length-6, doc._id.length), 1)
}
var inputs = doc.items.reduce(function(acc, item) { return acc.concat(item.inputs)}, [])
var userProfileInput = null
inputs.forEach(function(input) {
if (input.name === 'userProfileId') {
userProfileInput = input
}
})
if (userProfileInput) {
emit(userProfileInput.value.substr(userProfileInput.value.length-6, userProfileInput.value.length), 1)
}
},
reduce: '_count'
}

module.exports.groupIssues = function(doc) {
if (doc.collection === "TangyFormResponse" && doc.type === "issue") {
Expand Down
13 changes: 3 additions & 10 deletions server/src/routes/group-responses-by-user-profile-short-code.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
const DB = require('../db.js')
const clog = require('tangy-log').clog
const log = require('tangy-log').log

module.exports = async (req, res) => {
try {
const groupDb = new DB(req.params.groupId)
const userProfileShortCode = req.params.userProfileShortCode
let options = { key: userProfileShortCode, include_docs: true }
let options = { key: userProfileShortCode }
if (req.params.limit) {
options.limit = req.params.limit
}
if (req.params.skip) {
options.skip = req.params.skip
}
if (req.query.totalRows) {
const results = await groupDb.query('totalDocsByUserProfileShortCode', { key: userProfileShortCode, limit: 1,skip: 0, include_docs: false, reduce:true, group:true });
const results = await groupDb.query('responsesByUserProfileShortCode', { key: userProfileShortCode, limit: 1,skip: 0, include_docs: false, reduce: true, group: true });
res.send({ totalDocs: results.rows[0].value })
} else if (req.query.userProfile) {
await groupDb.query("userProfileByUserProfileShortCode", { limit: 0 });
const result = await groupDb.query("userProfileByUserProfileShortCode", { key: userProfileShortCode, limit: 1, include_docs: true });
const profile = result.rows[0]
const data = profile ? {_id: profile.id, key: profile.id, formId: profile.doc.form.id, collection: profile.doc.collection}: undefined
res.send(data)
} else {
const results = await groupDb.query('responsesByUserProfileShortCode', options);
const results = await groupDb.query('responsesByUserProfileShortCode', { ...options, include_docs: true, reduce: false });
const docs = results.rows.map(row => row.doc)
res.send(docs)
}
Expand Down
18 changes: 18 additions & 0 deletions server/src/routes/group-user-profile-by-short-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const DB = require('../db.js')
const log = require('tangy-log').log

module.exports = async (req, res) => {
try {
const groupDb = new DB(req.params.groupId)
const userProfileShortCode = req.params.userProfileShortCode

const result = await groupDb.query("userProfileByUserProfileShortCode", { key: userProfileShortCode, limit: 1, include_docs: true });
const profile = result.rows[0]
const data = profile ? {_id: profile.id, key: profile.id, formId: profile.doc.form.id, collection: profile.doc.collection} : undefined
res.send(data)

} catch (error) {
log.error(error);
res.status(500).send(error);
}
}

0 comments on commit 15cbac9

Please sign in to comment.