Skip to content

Commit

Permalink
Additional consoles
Browse files Browse the repository at this point in the history
* consoles
  • Loading branch information
Elkrival committed May 29, 2024
1 parent 30ae24e commit 60b7cb5
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 134 deletions.
15 changes: 6 additions & 9 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,22 @@ app.use('/', usersRouter)
app.use('/', userStudiesRouter)
app.use('./img', express.static(path.join(__dirname, '../public/img')))

app.get('/*', async (req, res) => {
app.get('/*', async (req, res, next) => {
return res.sendFile(
path.join(__dirname, '..', 'public', 'index.html'),
(err) => {
if (err) {
console.error(err)

res.status(500).send(err)
return next(err)
}
}
)
})

//catch any other error
app.use(function (err, _req, res) {
if (err) {
console.log('=====Error======', err)
return res.status(err.status).json({ error: err.message })
}
app.use(function (err, _req, res, _) {
console.error(err)

return res.send(err)
})

export default app
272 changes: 147 additions & 125 deletions server/controllers/api/assessmentDayDataController/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,154 +5,176 @@ import SiteMetadataModel from '../../../models/SiteMetadataModel'
import { collections } from '../../../utils/mongoCollections'

const AssessmentDayDataController = {
create: async (req, res) => {
const { appDb } = req.app.locals
const { metadata, participant_assessments, assessment_variables } = req.body

if (!metadata || !participant_assessments.length)
return res.status(400).json({ message: 'Nothing to import' })

const { assessment, participant, study, Consent, Active } = metadata
let parsedConsent = null

create: async (req, res, next) => {
try {
if (Consent) {
parsedConsent = new Date(Consent)
}
} catch {
// Missing consent dates could come in a number of formats,
// so we attempt to parse the date and leave it as null if there's
// an error
}
const { appDb } = req.app.locals
const { metadata, participant_assessments, assessment_variables } =
req.body

const query = {
assessment,
participant,
}
const assessmentQuery = {
name: assessment,
}
const newAssessmentProperties = AssessmentModel.withDefaults({
name: assessment,
})
if (!metadata || !participant_assessments.length)
return res.status(400).json({ message: 'Nothing to import' })

const participantAssessmentData = await AssessmentDayDataModel.findOne(
appDb,
query
)
const { assessment, participant, study, Consent, Active } = metadata
let parsedConsent = null

let sortedDayData = participant_assessments
let maxDayInDayData = Math.max(
...participant_assessments.map((pa) => pa.day)
)
try {
if (Consent) {
parsedConsent = new Date(Consent)
}
} catch {
// Missing consent dates could come in a number of formats,
// so we attempt to parse the date and leave it as null if there's
// an error
}

if (participantAssessmentData) {
sortedDayData = sortDayData(
participantAssessmentData,
participant_assessments
)
maxDayInDayData = Math.max(...sortedDayData.map((dayData) => dayData.day))

await AssessmentDayDataModel.update(appDb, query, {
...participantAssessmentData,
...metadata,
Consent: parsedConsent,
daysInStudy: maxDayInDayData,
dayData: sortedDayData,
})
} else {
await AssessmentDayDataModel.create(appDb, {
...metadata,
Consent: parsedConsent,
dayData: participant_assessments,
const query = {
assessment,
participant,
}
const assessmentQuery = {
name: assessment,
}
const newAssessmentProperties = AssessmentModel.withDefaults({
name: assessment,
})
}

const studyMetadata = await SiteMetadataModel.findOne(appDb, {
study,
})

if (!studyMetadata) {
await SiteMetadataModel.upsert(
const participantAssessmentData = await AssessmentDayDataModel.findOne(
appDb,
{ study },
{
setAttributes: {
study,
participants: [
{
Active,
Consent: parsedConsent,
study,
participant,
daysInStudy: maxDayInDayData,
synced: new Date(),
},
],
},
}
query
)
} else {
const isParticipantInDocument = await SiteMetadataModel.findOne(appDb, {
participants: { $elemMatch: { participant } },
})
if (isParticipantInDocument) {
await SiteMetadataModel.upsert(
appDb,
{ participants: { $elemMatch: { participant } } },
{
setAttributes: {
'participants.$.daysInStudy': maxDayInDayData,
'participants.$.synced': new Date(),
},
}
console.log('PARTICIPANT DATA')
console.dir({ participantAssessmentData }, { depth: null })
let sortedDayData = participant_assessments
let maxDayInDayData = Math.max(
...participant_assessments.map((pa) => pa.day)
)

if (participantAssessmentData) {
sortedDayData = sortDayData(
participantAssessmentData,
participant_assessments
)
maxDayInDayData = Math.max(
...sortedDayData.map((dayData) => dayData.day)
)

await AssessmentDayDataModel.update(appDb, query, {
...participantAssessmentData,
...metadata,
Consent: parsedConsent,
daysInStudy: maxDayInDayData,
dayData: sortedDayData,
})
console.log('After AssessmentDayDataModel.update')
} else {
await AssessmentDayDataModel.create(appDb, {
...metadata,
Consent: parsedConsent,
dayData: participant_assessments,
})
console.log('After AssessmentDayDataModel.create')
}

const studyMetadata = await SiteMetadataModel.findOne(appDb, {
study,
})
console.log('After Study Metadata')
console.dir({ studyMetadata }, { depth: null })

if (!studyMetadata) {
await SiteMetadataModel.upsert(
appDb,
{ study },
{
addToSetAttributes: {
participants: {
Active,
Consent: parsedConsent,
daysInStudy: maxDayInDayData,
study,
participant,
synced: new Date(),
},
setAttributes: {
study,
participants: [
{
Active,
Consent: parsedConsent,
study,
participant,
daysInStudy: maxDayInDayData,
synced: new Date(),
},
],
},
}
)
}
}

const currentAssessment = await AssessmentModel.upsert(
appDb,
assessmentQuery,
newAssessmentProperties
)

if (assessment_variables.length) {
await Promise.all(
assessment_variables.map(async ({ name }) => {
const variableAttributes = {
name,
assessment_id: currentAssessment._id,
}
console.log('After no studyMetadata')
} else {
const isParticipantInDocument = await SiteMetadataModel.findOne(appDb, {
participants: { $elemMatch: { participant } },
})
console.log('Participant check')
console.dir({ isParticipantInDocument }, { depth: null })

return await AssessmentVariablesModel.upsert(
if (isParticipantInDocument) {
await SiteMetadataModel.upsert(
appDb,
variableAttributes,
variableAttributes
{ participants: { $elemMatch: { participant } } },
{
setAttributes: {
'participants.$.daysInStudy': maxDayInDayData,
'participants.$.synced': new Date(),
},
}
)
})
console.log('After participant in document')
console.dir({ isParticipantInDocument }, { depth: null })
} else {
await SiteMetadataModel.upsert(
appDb,
{ study },
{
addToSetAttributes: {
participants: {
Active,
Consent: parsedConsent,
daysInStudy: maxDayInDayData,
study,
participant,
synced: new Date(),
},
},
}
)
console.log('After SiteMetadata addToSet')
console.dir({ isParticipantInDocument }, { depth: null })
}
}

const currentAssessment = await AssessmentModel.upsert(
appDb,
assessmentQuery,
newAssessmentProperties
)
console.log('After current Assessment')
console.dir({ currentAssessment }, { depth: null })

if (assessment_variables.length) {
await Promise.all(
assessment_variables.map(async ({ name }) => {
const variableAttributes = {
name,
assessment_id: currentAssessment._id,
}

return await AssessmentVariablesModel.upsert(
appDb,
variableAttributes,
variableAttributes
)
})
)
console.log('After Inserting assessment Variables')
}
return res
.status(200)
.json({ data: `${participant} ${assessment} data imported` })
} catch (err) {
return next(err)
}

return res
.status(200)
.json({ data: `${participant} ${assessment} data imported` })
},
destroy: async (req, res) => {
try {
Expand Down

0 comments on commit 60b7cb5

Please sign in to comment.