@@ -110,13 +110,13 @@ with sch as
110110 values (default)
111111 returning *),
112112def as
113- (insert into form_defs ("formId", "schemaId", xml, name, hash, sha, sha256, version, "keyId", "xlsBlobId", "draftToken", "enketoId", "createdAt")
114- select nextval(pg_get_serial_sequence('forms', 'id')), sch.id, ${ form . xml } , ${ def . name } , ${ def . hash } , ${ def . sha } , ${ def . sha256 } , ${ def . version } , ${ def . keyId } , ${ form . xls . xlsBlobId || null } , ${ def . draftToken || null } , ${ def . enketoId || null } , clock_timestamp()
113+ (insert into form_defs ("formId", "schemaId", xml, name, hash, sha, sha256, version, "keyId", "xlsBlobId", "draftToken", "enketoId", "createdAt", "webformsEnabled" )
114+ select nextval(pg_get_serial_sequence('forms', 'id')), sch.id, ${ form . xml } , ${ def . name } , ${ def . hash } , ${ def . sha } , ${ def . sha256 } , ${ def . version } , ${ def . keyId } , ${ form . xls . xlsBlobId || null } , ${ def . draftToken || null } , ${ def . enketoId || null } , clock_timestamp(), ${ def . webformsEnabled }
115115 from sch
116116 returning *),
117117form as
118- (insert into forms (id, "xmlFormId", state, "projectId", "draftDefId", "acteeId", "enketoId", "enketoOnceId", "createdAt", "webformsEnabled" )
119- select def."formId", ${ form . xmlFormId } , ${ form . state || 'open' } , ${ project . id } , def.id, ${ form . acteeId } , ${ form . enketoId || null } , ${ form . enketoOnceId || null } , def."createdAt", ${ form . webformsEnabled || DEFAULT_WEBFORMS_ENABLED } from def
118+ (insert into forms (id, "xmlFormId", state, "projectId", "draftDefId", "acteeId", "enketoId", "enketoOnceId", "createdAt")
119+ select def."formId", ${ form . xmlFormId } , ${ form . state || 'open' } , ${ project . id } , def.id, ${ form . acteeId } , ${ form . enketoId || null } , ${ form . enketoOnceId || null } , def."createdAt" from def
120120 returning forms.*)
121121select id from form` )
122122 . then ( ( ) => Forms . getByProjectAndXmlFormId ( project . id , form . xmlFormId , Form . DraftVersion ) )
@@ -149,10 +149,12 @@ const createNew = (partial, project) => async ({ Actees, Datasets, FormAttachmen
149149 0.5
150150 ) ;
151151
152+ const webformsEnabled = keyId ? false : DEFAULT_WEBFORMS_ENABLED ;
153+
152154 // Save draft form
153155 const savedForm = await Forms . _createNew (
154156 partial . with ( { acteeId } ) ,
155- partial . def . with ( { keyId, draftToken, enketoId } ) ,
157+ partial . def . with ( { keyId, draftToken, enketoId, webformsEnabled } ) ,
156158 project
157159 ) ;
158160
@@ -273,7 +275,9 @@ const createVersion = (partial, form, publish, duplicating = false) => async ({
273275 // will just copy over whatever enketo ids do or don't exist already.
274276
275277 // Save draft def
276- const savedDef = await Forms . _createNewDef ( partial , form , publish , { draftToken, enketoId, schemaId, keyId } ) ;
278+ // In production, form.associated should never be null but in testing code we call Form.createVersion directly
279+ const webformsEnabled = keyId ? false : ( form . associated ?. webformsEnabled ?? DEFAULT_WEBFORMS_ENABLED ) ;
280+ const savedDef = await Forms . _createNewDef ( partial , form , publish , { draftToken, enketoId, schemaId, keyId, webformsEnabled } ) ;
277281
278282 // Prepare the form fields
279283 if ( ! match )
@@ -655,6 +659,10 @@ const _getSql = ((fields, extend, options, defVersion, deleted = false, actorId)
655659 defVersion === Form . WithoutDef ? sql `${ fields } FROM forms` :
656660 sql `${ fields } FROM forms LEFT OUTER JOIN form_defs on ${ versionJoinCondition ( defVersion ) } `
657661 }
662+ JOIN (
663+ SELECT forms.id, form_defs."webformsEnabled" FROM forms
664+ JOIN form_defs ON form_defs.id=coalesce(forms."currentDefId", forms."draftDefId")
665+ ) AS "formAssociated" ON "formAssociated".id = forms.id
658666${ extend || sql `
659667 left outer join
660668 (select "formId", count(id)::integer as "submissions", max("createdAt") as "lastSubmission",
@@ -707,7 +715,7 @@ const _get = (exec, options, defVersion, xml, deleted, actorId) => {
707715 if ( xml !== Form . IncludeXml && xml !== Form . WithoutXml ) throw new Error ( 'Must use Form.WithoutXml or Form.IncludeXml; actually got: ' + xml . toString ( ) ) ;
708716 if ( defVersion === Form . WithoutDef && xml === Form . IncludeXml ) throw new Error ( 'Cannot request xml without a form def' ) ;
709717
710- const frames = [ Form ] ;
718+ const frames = [ Form , Form . Associated ] ;
711719 if ( defVersion !== Form . WithoutDef ) frames . push ( Form . Def ) ;
712720 if ( xml === Form . IncludeXml ) frames . push ( Form . Xml ) ;
713721 return extender ( ...frames ) ( Form . Extended , Actor . into ( 'createdBy' ) ) ( _getSql ) ( exec , options , defVersion , deleted , actorId ) ;
0 commit comments