Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F82927923
studies.coffee
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Sep 14, 06:39
Size
3 KB
Mime Type
text/x-c++
Expires
Mon, Sep 16, 06:39 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20776102
Attached To
R7177 epiph
studies.coffee
View Options
class
@Study
constructor:
(doc) ->
_
.
extend
this
,
doc
creator:
->
Meteor
.
users
.
findOne
_id:
@creatorId
editingNotAllowed:
->
false
@Studies =
new
Meteor
.
Collection
(
"studies"
,
transform:
(doc) ->
new
Study
(
doc
)
)
Studies
.
before
.
insert
BeforeInsertTimestampHook
Studies
.
before
.
update
BeforeUpdateTimestampHook
schema =
'title'
:
type:
String
'isLocked'
:
type:
Boolean
defaultValue:
false
'creatorId'
:
type:
String
'updatedAt'
:
type:
Number
optional:
true
'createdAt'
:
type:
Number
optional:
true
Studies
.
attachSchema
new
SimpleSchema
(
schema
)
Meteor
.
methods
"createStudy"
:
->
checkIfAdmin
()
Studies
.
insert
title:
"new Study"
creatorId:
Meteor
.
userId
()
"updateStudyTitle"
:
(studyId, title) ->
checkIfAdmin
()
check
(
title
,
String
)
study =
Studies
.
findOne
studyId
throw
new
Meteor
.
Error
(
403
,
"study not found."
)
unless
study
?
throw
new
Meteor
.
Error
(
400
,
"Study is locked. Please unlock it first."
)
if
study
.
isLocked
Studies
.
update
studyId
,
$set: title:
title
return
if
Meteor
.
isServer
Meteor
.
methods
"lockStudy"
:
(studyId, forceReason) ->
checkIfAdmin
()
check
studyId
,
String
check
forceReason
,
String
study =
Studies
.
findOne
studyId
throw
new
Meteor
.
Error
(
403
,
"study not found."
)
unless
study
?
throw
new
Meteor
.
Error
(
400
,
"study is already locked."
)
if
study
.
isLocked
Meteor
.
call
"logActivity"
,
"lock study (#{study.title})"
,
"notice"
,
forceReason
,
null
Studies
.
update
studyId
,
$set: isLocked:
true
return
"unlockStudy"
:
(studyId, forceReason) ->
checkIfAdmin
()
check
studyId
,
String
check
forceReason
,
String
study =
Studies
.
findOne
studyId
throw
new
Meteor
.
Error
(
403
,
"study not found."
)
unless
study
?
throw
new
Meteor
.
Error
(
400
,
"study is already unlocked."
)
if
study
.
isLocked
?
and
!
study
.
isLocked
Meteor
.
call
"logActivity"
,
"unlock study (#{study.title})"
,
"notice"
,
forceReason
,
null
Studies
.
update
studyId
,
$set: isLocked:
false
return
"removeStudy"
:
(studyId, forceReason) ->
checkIfAdmin
()
check
studyId
,
String
study =
Studies
.
findOne
studyId
throw
new
Meteor
.
Error
(
403
,
"study not found."
)
unless
study
?
throw
new
Meteor
.
Error
(
400
,
"study is locked. Please unlock it first."
)
if
study
.
isLocked
#check if a visit with answer exists
visitTemplateIds =
[]
StudyDesigns
.
find
(
studyId:
studyId
).
forEach
(sd) ->
sd
.
visits
.
forEach
(vt) ->
visitTemplateIds
.
push
vt
.
_id
visitIds =
Visits
.
find
(
designVisitId: $in:
visitTemplateIds
).
map
(v) ->
v
.
_id
hasData =
Answers
.
find
(
visitId: $in:
visitIds
).
count
()
>
0
if
hasData
and
!
forceReason
?
throw
new
Meteor
.
Error
(
400
,
"answersExistForStudy"
)
if
hasData
Meteor
.
call
"logActivity"
,
"remove study (#{study.title}) which has data"
,
"critical"
,
forceReason
,
study
else
Meteor
.
call
"logActivity"
,
"remove empty study (#{study.title})"
,
"notice"
,
null
,
study
Answers
.
remove
visitId: $in:
visitIds
Visits
.
remove
designVisitId: $in:
visitTemplateIds
Patients
.
remove
studyId:
studyId
StudyDesigns
.
remove
studyId:
studyId
Studies
.
remove
_id:
studyId
return
Event Timeline
Log In to Comment