@@ -173,6 +173,47 @@ const getAll = (datasetId, options = QueryOptions.none) => ({ all }) =>
173173 _get ( false ) ( all , options . withCondition ( { datasetId } ) , isTrue ( options . argData . deleted ) )
174174 . then ( map ( ( e ) => e . withAux ( 'currentVersion' , e . aux . currentVersion . withAux ( 'creator' , e . aux . currentVersionCreator ) ) ) ) ;
175175
176+ const getByUuid = ( uuid ) => ( { maybeOne } ) =>
177+ maybeOne ( sql `select * from entities where "uuid" = ${ uuid } limit 1` )
178+ . then ( map ( construct ( Entity ) ) ) ;
179+
180+ ////////////////////////////////////////////////////////////////////////////////
181+ // GETTING ENTITY DEFS
182+
183+ const _getDef = extender ( Entity . Def , Submission , Submission . Def . into ( 'submissionDef' ) , Form ) ( Actor . into ( 'creator' ) ) ( ( fields , extend , options ) => sql `
184+ SELECT ${ fields } FROM entities
185+ JOIN entity_defs ON entities.id = entity_defs."entityId"
186+ LEFT JOIN submission_defs ON submission_defs.id = entity_defs."submissionDefId"
187+ LEFT JOIN (
188+ SELECT submissions.*, submission_defs."userAgent" FROM submissions
189+ JOIN submission_defs ON submissions.id = submission_defs."submissionId" AND root
190+ ) submissions ON submissions.id = submission_defs."submissionId"
191+ LEFT JOIN forms ON submissions."formId" = forms.id
192+ ${ extend || sql `
193+ LEFT JOIN actors ON actors.id=entities."creatorId"
194+ LEFT JOIN actors current_version_actors ON current_version_actors.id=entity_defs."creatorId"
195+ ` }
196+ where ${ equals ( options . condition ) } AND entities."deletedAt" IS NULL
197+ order by entity_defs."createdAt", entity_defs.id
198+ ` ) ;
199+
200+ const getAllDefs = ( datasetId , uuid , options = QueryOptions . none ) => ( { all } ) =>
201+ _getDef ( all , options . withCondition ( { datasetId, uuid } ) )
202+ . then ( map ( ( v ) => {
203+ const isSourceSubmission = ! ! v . submissionDefId ;
204+
205+ // TODO: revisit this when working on POST /entities
206+ const source = new Entity . Def . Source ( {
207+ type : isSourceSubmission ? 'submission' : 'api' ,
208+ details : isSourceSubmission ? {
209+ xmlFormId : v . aux . form . xmlFormId ,
210+ instanceId : v . aux . submission . instanceId ,
211+ instanceName : v . aux . submissionDef . instanceName
212+ } : null
213+ } ) ;
214+
215+ return new Entity . Def ( v , { creator : v . aux . creator , source } ) ;
216+ } ) ) ;
176217
177218// This will check for an entity related to any def of the same submission
178219// as the one specified. Used when trying to reapprove an edited submission.
@@ -183,9 +224,6 @@ const getDefBySubmissionId = (submissionId) => ({ maybeOne }) =>
183224 where s.id = ${ submissionId } limit 1` )
184225 . then ( map ( construct ( Entity . Def ) ) ) ;
185226
186- const getByUuid = ( uuid ) => ( { maybeOne } ) =>
187- maybeOne ( sql `select * from entities where "uuid" = ${ uuid } limit 1` )
188- . then ( map ( construct ( Entity ) ) ) ;
189227
190228
191229////////////////////////////////////////////////////////////////////////////////
@@ -217,5 +255,5 @@ module.exports = {
217255 processSubmissionEvent, streamForExport,
218256 getDefBySubmissionId, getByUuid,
219257 countByDatasetId, getById,
220- getAll
258+ getAll, getAllDefs
221259} ;
0 commit comments