Skip to content
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

Add study page #718

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
"plugin:react/recommended",
"prettier",
"universe/web",
"universe/node"
"universe/node",
],
"parser": "@babel/eslint-parser",
"env": {
"browser": true,
"node": true
"node": true,
},
"parserOptions": {
"requireConfigFile": false,
"babelOptions": {
"presets": ["@babel/preset-react"]
"presets": ["@babel/preset-react"],
},
"ecmaFeatures": {
"jsx": true
"jsx": true,
},
"ecmaVersion": 15,
"sourceType": "module",
"parserOptions": {
"project": "./tsconfig.json"
}
"project": "./tsconfig.json",
},
},
"plugins": [
"react",
Expand All @@ -32,31 +32,34 @@
"no-only-tests",
"sort-keys-fix",
"prettier",
"promise"
"promise",
],
"rules": {
"react/prop-types": "off",
"no-unused-vars": ["error", { "varsIgnorePattern": "^_", "argsIgnorePattern": "^_" }],
"no-unused-vars": [
"error",
{ "varsIgnorePattern": "^_", "argsIgnorePattern": "^_" },
],
"import/order": [
"error",
{
"alphabetize": {
"caseInsensitive": true,
"order": "asc"
"order": "asc",
},
"groups": ["builtin", "external", "internal"],
"newlines-between": "always",
"pathGroups": [
{
"group": "external",
"pattern": "react",
"position": "before"
}
"position": "before",
},
],
"pathGroupsExcludedImportTypes": ["react"]
}
"pathGroupsExcludedImportTypes": ["react"],
},
],
"no-only-tests/no-only-tests": "error",
"react/react-in-jsx-scope": "off"
}
"react/react-in-jsx-scope": "off",
},
}
4 changes: 3 additions & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import indexRouter from './routes/index'
import participantsRouter from './routes/participants'
import siteMetadata from './routes/siteMetadata'
import usersRouter from './routes/users'
import userStudiesRouter from './routes/userStudies'
import { verifyHash } from './utils/crypto/hash'

const localStrategy = Strategy
Expand Down Expand Up @@ -152,9 +153,9 @@ passport.deserializeUser(async function (user, done) {
done(null, user)
})

app.use('/', adminRouter)
app.use('/', assessmentsRouter)
app.use('/', assessmentData)
app.use('/', adminRouter)
app.use('/', authRouter)
app.use('/', chartsRouter)
app.use('/', configurationsRouter)
Expand All @@ -163,6 +164,7 @@ app.use('/', indexRouter)
app.use('/', participantsRouter)
app.use('/', siteMetadata)
app.use('/', usersRouter)
app.use('/', userStudiesRouter)
app.use('./img', express.static(path.join(__dirname, '../public/img')))

app.get('/*', async (req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('assessmentDayDataController', () => {
projection: {
_id: 0,
'participants.synced': 0,
updatedAt: 0,
},
}
)
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/api/assessmentDayDataController/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const AssessmentDayDataController = {
appDb,
{ study },
{
$set: {
setAttributes: {
study,
participants: [
{
Expand All @@ -105,7 +105,7 @@ const AssessmentDayDataController = {
appDb,
{ participants: { $elemMatch: { participant } } },
{
$set: {
setAttributes: {
'participants.$.daysInStudy': maxDayInDayData,
'participants.$.synced': new Date(),
},
Expand All @@ -116,7 +116,7 @@ const AssessmentDayDataController = {
appDb,
{ study },
{
$addToSet: {
addToSetAttributes: {
participants: {
Active,
Consent: parsedConsent,
Expand Down
16 changes: 10 additions & 6 deletions server/controllers/api/siteMetadataController/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ const SiteMetadataController = {

participants.forEach((participant, i) => {
participants[i].Consent = new Date(participant.Consent)
if (participants[i]?.synced)
participants[i].synced = new Date(participant.synced)
})

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

if (!studyMetadata) {
await SiteMetadataModel.upsert(
appDb,
{ study },
{ $set: { ...metadata, participants } }
{
setAttributes: {
...metadata,
participants,
createdAt: new Date(),
},
}
)
} else {
Promise.all(
Expand All @@ -36,7 +40,7 @@ const SiteMetadataController = {
await SiteMetadataModel.upsert(
appDb,
{ study },
{ $addToSet: { participants: participant } }
{ addToSetAttributes: { participants: participant } }
)
} else {
const updatedAttributes = Object.keys(participant).reduce(
Expand All @@ -54,7 +58,7 @@ const SiteMetadataController = {
$elemMatch: { participant: participant.participant },
},
},
{ $set: updatedAttributes }
{ setAttributes: updatedAttributes }
)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,22 @@ describe('siteMetadataController', () => {
role: 'metadata',
study: 'site',
extension: '.csv',
updatedAt: new Date(),
},
participants: [
{
participant: 'YA1',
Active: 1,
Consent: '2022-06-02',
study: 'YA',
daysInStudy: 55,
},
{
participant: 'YA2',
Active: 1,
Consent: '2022-06-02',
study: 'YA',
daysInStudy: 105,
},
],
})
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/studiesController/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StudiesModel from '../../models/StudiesModel'

const StudiesController = {
const studiesController = {
index: async (req, res) => {
try {
const { appDb } = req.app.locals
Expand All @@ -13,4 +13,4 @@ const StudiesController = {
},
}

export default StudiesController
export default studiesController
10 changes: 5 additions & 5 deletions server/controllers/studiesController/studiesController.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import StudiesController from '.'
import studiesController from '.'
import { createRequest, createResponse } from '../../../test/fixtures'

describe('StudiesController', () => {
describe(StudiesController.index, () => {
describe('studiesController', () => {
describe(studiesController.index, () => {
describe('When successful', () => {
it('returns a status of 200 with a list of distinct studies', async () => {
const request = createRequest()
Expand All @@ -11,7 +11,7 @@ describe('StudiesController', () => {

request.app.locals.appDb.distinct.mockResolvedValueOnce(listOfStudies)

await StudiesController.index(request, response)
await studiesController.index(request, response)

expect(response.status).toHaveBeenCalledWith(200)
expect(response.json).toHaveBeenCalledWith({
Expand All @@ -28,7 +28,7 @@ describe('StudiesController', () => {
new Error('some error')
)

await StudiesController.index(request, response)
await studiesController.index(request, response)

expect(response.status).toHaveBeenCalledWith(400)
expect(response.json).toHaveBeenCalledWith({
Expand Down
31 changes: 31 additions & 0 deletions server/controllers/userStudiesController/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import UserModel from '../../models/UserModel'
import UserStudiesModel from '../../models/UserStudiesModel'

const userStudiesController = {
index: async (req, res) => {
try {
const { appDb } = req.app.locals
const isSortRequested = Object.hasOwn(req.query, 'sort')
const parsedSortParams =
isSortRequested && Object.keys(req.query.sort).length
? req.query.sort
: { sortBy: 'study', sortDirection: 'ASC' }
const userAccess = await UserModel.findOne(
appDb,
{ uid: req.user.uid },
{ access: 1 }
)
const userStudies = await UserStudiesModel.all(
appDb,
userAccess.access,
parsedSortParams
)

return res.status(200).json({ data: userStudies })
} catch (error) {
return res.status(400).json({ message: error.message })
}
},
}

export default userStudiesController
Loading
Loading