Skip to content

Commit 2501664

Browse files
authored
fix import (#754)
1 parent 0c0f680 commit 2501664

File tree

3 files changed

+42
-75
lines changed

3 files changed

+42
-75
lines changed

server/controllers/api/assessmentDayDataController/index.js

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,11 @@ const AssessmentDayDataController = {
4242
appDb,
4343
query
4444
)
45-
console.log('PARTICIPANT DATA')
46-
console.dir(
47-
{
48-
participantAssessmentData,
49-
metadata,
50-
assessment_variables,
51-
Consent,
52-
query,
53-
},
54-
{ depth: null }
55-
)
45+
5646
let sortedDayData = participant_assessments
5747
let maxDayInDayData = Math.max(
5848
...participant_assessments.map((pa) => pa.day)
5949
)
60-
console.dir(participant_assessments, { depth: null })
6150
if (participantAssessmentData) {
6251
sortedDayData = sortDayData(
6352
participantAssessmentData,
@@ -66,93 +55,71 @@ const AssessmentDayDataController = {
6655
maxDayInDayData = Math.max(
6756
...sortedDayData.map((dayData) => dayData.day)
6857
)
69-
console.log('Participant data update')
7058
await AssessmentDayDataModel.upsert(appDb, query, {
7159
...participantAssessmentData,
7260
...metadata,
7361
daysInStudy: maxDayInDayData,
7462
dayData: sortedDayData,
7563
})
76-
console.log('After AssessmentDayDataModel.update')
7764
} else {
78-
console.log('NEW PARTICIPANT DATA UPSERT')
7965
await AssessmentDayDataModel.upsert(appDb, query, {
8066
...metadata,
8167
Consent: parsedConsent,
8268
dayData: participant_assessments,
8369
})
84-
console.log('After AssessmentDayDataModel.upsert')
8570
}
8671

8772
const studyMetadata = await SiteMetadataModel.findOne(appDb, {
8873
study,
8974
})
90-
console.log('After Study Metadata')
91-
console.dir({ studyMetadata }, { depth: null })
9275

9376
if (!studyMetadata) {
94-
console.log('new site metadata')
95-
await SiteMetadataModel.upsert(
77+
await SiteMetadataModel.upsert$Set(
9678
appDb,
9779
{ study },
9880
{
99-
setAttributes: {
100-
study,
101-
participants: [
102-
{
103-
Active,
104-
Consent: parsedConsent,
105-
study,
106-
participant,
107-
daysInStudy: maxDayInDayData,
108-
synced: new Date(),
109-
},
110-
],
111-
},
81+
study,
82+
participants: [
83+
{
84+
Active,
85+
Consent: parsedConsent,
86+
study,
87+
participant,
88+
daysInStudy: maxDayInDayData,
89+
synced: new Date(),
90+
},
91+
],
11292
}
11393
)
114-
console.log('After no studyMetadata')
11594
} else {
11695
const isParticipantInDocument = await SiteMetadataModel.findOne(appDb, {
11796
participants: { $elemMatch: { participant } },
11897
})
119-
console.log('Participant check')
120-
console.dir({ isParticipantInDocument }, { depth: null })
12198

12299
if (isParticipantInDocument) {
123-
console.log('participant update site metadata upsert')
124-
await SiteMetadataModel.upsert(
100+
await SiteMetadataModel.upsert$Set(
125101
appDb,
126102
{ participants: { $elemMatch: { participant } } },
127103
{
128-
setAttributes: {
129-
'participants.$.daysInStudy': maxDayInDayData,
130-
'participants.$.synced': new Date(),
131-
},
104+
'participants.$.daysInStudy': maxDayInDayData,
105+
'participants.$.synced': new Date(),
132106
}
133107
)
134-
console.log('After participant in document')
135-
console.dir({ isParticipantInDocument }, { depth: null })
136108
} else {
137-
console.log('new participant site metadata upsert')
138-
await SiteMetadataModel.upsert(
109+
await SiteMetadataModel.upsert$addToSet(
139110
appDb,
140111
{ study },
141112
{
142-
addToSetAttributes: {
143-
participants: {
144-
Active,
145-
Consent: parsedConsent,
146-
daysInStudy: maxDayInDayData,
147-
study,
148-
participant,
149-
synced: new Date(),
150-
},
113+
participants: {
114+
Active,
115+
Consent: parsedConsent,
116+
daysInStudy: maxDayInDayData,
117+
study,
118+
participant,
119+
synced: new Date(),
151120
},
152121
}
153122
)
154-
console.log('After SiteMetadata addToSet')
155-
console.dir({ isParticipantInDocument }, { depth: null })
156123
}
157124
}
158125

@@ -161,8 +128,6 @@ const AssessmentDayDataController = {
161128
assessmentQuery,
162129
newAssessmentProperties
163130
)
164-
console.log('After current Assessment')
165-
console.dir({ currentAssessment }, { depth: null })
166131

167132
if (assessment_variables.length) {
168133
await Promise.all(
@@ -179,7 +144,6 @@ const AssessmentDayDataController = {
179144
)
180145
})
181146
)
182-
console.log('After Inserting assessment Variables')
183147
}
184148
return res
185149
.status(200)

server/controllers/api/siteMetadataController/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ const SiteMetadataController = {
1414
const studyMetadata = await SiteMetadataModel.findOne(appDb, { study })
1515

1616
if (!studyMetadata) {
17-
await SiteMetadataModel.upsert(
17+
await SiteMetadataModel.upsert$Set(
1818
appDb,
1919
{ study },
2020
{
21-
setAttributes: {
22-
...metadata,
23-
participants,
24-
createdAt: new Date(),
25-
},
21+
...metadata,
22+
participants,
23+
createdAt: new Date(),
2624
}
2725
)
2826
} else {
@@ -37,10 +35,10 @@ const SiteMetadataController = {
3735
}
3836
)
3937
if (!isParticipantInSiteMetadata) {
40-
await SiteMetadataModel.upsert(
38+
await SiteMetadataModel.upsert$addToSet(
4139
appDb,
4240
{ study },
43-
{ addToSetAttributes: { participants: participant } }
41+
{ participants: participant }
4442
)
4543
} else {
4644
const updatedAttributes = Object.keys(participant).reduce(
@@ -51,14 +49,14 @@ const SiteMetadataController = {
5149
},
5250
{}
5351
)
54-
await SiteMetadataModel.upsert(
52+
await SiteMetadataModel.upsert$Set(
5553
appDb,
5654
{
5755
participants: {
5856
$elemMatch: { participant: participant.participant },
5957
},
6058
},
61-
{ setAttributes: updatedAttributes }
59+
updatedAttributes
6260
)
6361
}
6462
})

server/models/SiteMetadataModel/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const queryOptions = {
77
const SiteMetadataModel = {
88
findOne: async (db, query) =>
99
await db.collection(collections.metadata).findOne(query),
10-
upsert: async (db, query, { setAttributes, addToSetAttributes }) =>
10+
upsert$Set: async (db, query, setAttributes) =>
1111
await db.collection(collections.metadata).findOneAndUpdate(
1212
query,
1313
{
14-
$set: { ...(setAttributes || {}), updatedAt: new Date() },
15-
$addToSet: Object.keys(addToSetAttributes || {}).length
16-
? { ...addToSetAttributes, createdAt: new Date() }
17-
: {},
14+
$set: setAttributes,
15+
},
16+
queryOptions
17+
),
18+
upsert$addToSet: async (db, query, addToSetAttributes) =>
19+
await db.collection(collections.metadata).findOneAndUpdate(
20+
query,
21+
{
22+
$addToSet: addToSetAttributes,
1823
},
1924
queryOptions
2025
),

0 commit comments

Comments
 (0)