Skip to content

Commit

Permalink
Merge pull request #3718 from Tangerine-Community/fixes-to-v3.31.0-rc-18
Browse files Browse the repository at this point in the history
feat(import-user-profiles): namespace `processedDocs`
  • Loading branch information
esurface authored Jul 18, 2024
2 parents b1824b5 + 15cbac9 commit 2a6ee4d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Component, ViewChild, ElementRef, AfterContentInit } from '@angular/cor
import { HttpClient } from '@angular/common/http';
import { UserService } from '../../shared/_services/user.service';
import { Router } from '@angular/router';
import PouchDB from 'pouchdb';
import { AppConfigService } from 'src/app/shared/_services/app-config.service';
import { AppConfig } from 'src/app/shared/_classes/app-config.class';
import { _TRANSLATE } from 'src/app/shared/translation-marker';
import { VariableService } from 'src/app/shared/_services/variable.service';


@Component({
Expand Down Expand Up @@ -33,7 +33,8 @@ export class ImportUserProfileComponent implements AfterContentInit {
private router: Router,
private http: HttpClient,
private userService: UserService,
private appConfigService: AppConfigService
private appConfigService: AppConfigService,
private variableService: VariableService
) { }

ngAfterContentInit() {
Expand All @@ -56,13 +57,15 @@ 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 processedDocs = +localStorage.getItem('processedDocs') || 0;
let previousProcessedDocs = await this.variableService.get(`${username}-processedDocs`)
let processedDocs = parseInt(previousProcessedDocs) || 0;
while (processedDocs < this.totalDocs) {
this.docs = await this.http.get(`${this.appConfig.serverUrl}api/${this.appConfig.groupId}/responsesByUserProfileShortCode/${this.shortCode}/${docsToQuery}/${processedDocs}`).toPromise()
for (let doc of this.docs) {
Expand All @@ -71,9 +74,9 @@ export class ImportUserProfileComponent implements AfterContentInit {
}
processedDocs += this.docs.length;
this.processedDocs = processedDocs
localStorage.setItem('processedDocs', String(processedDocs))
await this.variableService.set(`${username}-processedDocs`, String(processedDocs))
}
} else{
} else {
this.state = this.STATE_NOT_FOUND
}
this.router.navigate([`/${this.appConfig.homeUrl}`] );
Expand Down
1 change: 1 addition & 0 deletions develop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ OPTIONS="--link $T_COUCHDB_CONTAINER_NAME:couchdb \
--volume $(pwd)/server/package.json:/tangerine/server/package.json:delegated \
--volume $(pwd)/server/src:/tangerine/server/src:delegated \
--volume $(pwd)/client/src:/tangerine/client/src:delegated \
--volume $(pwd)/client/dist:/tangerine/client/dist:delegated \
--volume $(pwd)/server/reporting:/tangerine/server/reporting:delegated \
--volume $(pwd)/upgrades:/tangerine/upgrades:delegated \
--volume $(pwd)/scripts/generate-csv/bin.js:/tangerine/scripts/generate-csv/bin.js:delegated \
Expand Down
2 changes: 1 addition & 1 deletion docs/developer/debugging-reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Summary of steps:
1. Enter the container on command line with `docker exec -it tangerine bash`.
1. Clear reporting cache with command `reporting-cache-clear`.
1. Run a batch with debugger enabled by running command `node --inspect-brk=0.0.0.0:9228 $(which reporting-worker-batch)`.
1. Latch onto debugging session using Chrome inspect. You may need to click "configure" and add `localhost:9228` to "Target discovery settings".
1. Latch onto debugging session using [Chrome Inspect](chrome://inspect/#devices). You may need to click "configure" and add `localhost:9228` to "Target discovery settings".


## Instructions
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
31 changes: 7 additions & 24 deletions server/src/group-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,7 @@ module.exports.unpaid = function(doc) {
}
}

module.exports.responsesByUserProfileShortCode = function(doc) {
if (doc.collection === "TangyFormResponse") {
if (doc.form && doc.form.id === 'user-profile') {
return emit(doc._id.substr(doc._id.length-6, doc._id.length), true)
}
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), true)
}
}
}

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 = {
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), 1)
Expand All @@ -101,6 +78,12 @@ module.exports.totalDocsByUserProfileShortCode = {
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.groupIssues = function(doc) {
if (doc.collection === "TangyFormResponse" && doc.type === "issue") {
var lastFilledOutNode;
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);
}
}
13 changes: 13 additions & 0 deletions server/src/upgrade/v3.31.0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node
const util = require("util");
const exec = util.promisify(require('child_process').exec)

async function go() {
console.log('Updating views with a new view used for the User Profile listing.')
try {
await exec(`/tangerine/server/src/scripts/push-all-groups-views.js `)
} catch (e) {
console.log(e)
}
}
go()

0 comments on commit 2a6ee4d

Please sign in to comment.