@@ -9,6 +9,17 @@ function generateSlug(title: string): string {
99 return `${ base } -${ Date . now ( ) } ` ;
1010}
1111
12+ // Frozen copy of the working form stored at publish time — what the public is served.
13+ function snapshotOf ( data : any ) {
14+ return {
15+ title : data . title ,
16+ description : data . description ?? '' ,
17+ fields : data . fields ?? [ ] ,
18+ conditionalLogic : data . conditionalLogic ?? [ ] ,
19+ settings : data . settings ?? { } ,
20+ } ;
21+ }
22+
1223export default ( { strapi } : { strapi : any } ) => ( {
1324 async find ( ) {
1425 const forms = await strapi . db . query ( FORM_UID ) . findMany ( {
@@ -40,11 +51,23 @@ export default ({ strapi }: { strapi: any }) => ({
4051
4152 async create ( data : any ) {
4253 const slug = generateSlug ( data . title || 'form' ) ;
43- return strapi . db . query ( FORM_UID ) . create ( { data : { ...data , slug } } ) ;
54+ const payload : any = { ...data , slug } ;
55+ // Publishing captures the working copy as the immutable live snapshot.
56+ if ( data . publishedAt ) payload . publishedData = snapshotOf ( data ) ;
57+ return strapi . db . query ( FORM_UID ) . create ( { data : payload } ) ;
4458 } ,
4559
4660 async update ( id : number , data : any ) {
47- return strapi . db . query ( FORM_UID ) . update ( { where : { id } , data } ) ;
61+ const payload : any = { ...data } ;
62+ if ( data . publishedAt ) {
63+ // Publish: freeze the current working copy as the live version.
64+ payload . publishedData = snapshotOf ( data ) ;
65+ } else {
66+ // Save draft: never unpublish or overwrite what the public sees.
67+ delete payload . publishedAt ;
68+ delete payload . publishedData ;
69+ }
70+ return strapi . db . query ( FORM_UID ) . update ( { where : { id } , data : payload } ) ;
4871 } ,
4972
5073 async delete ( id : number ) {
@@ -55,7 +78,7 @@ export default ({ strapi }: { strapi: any }) => ({
5578 const original = await strapi . db . query ( FORM_UID ) . findOne ( { where : { id } } ) ;
5679 if ( ! original ) return null ;
5780
58- const { id : _id , slug, createdAt, updatedAt, publishedAt, ...rest } = original ;
81+ const { id : _id , slug, createdAt, updatedAt, publishedAt, publishedData , ...rest } = original ;
5982 return strapi . db . query ( FORM_UID ) . create ( {
6083 data : {
6184 ...rest ,
@@ -65,35 +88,31 @@ export default ({ strapi }: { strapi: any }) => ({
6588 } ) ;
6689 } ,
6790
91+ // Public views serve the published snapshot only — draft edits never leak.
6892 async getPublicSchemaById ( id : number ) {
6993 const form = await this . findOne ( id ) ;
70- if ( ! form ) return null ;
71- return {
72- data : {
73- id : form . id ,
74- title : form . title ,
75- slug : form . slug ,
76- description : form . description ,
77- fields : form . fields || [ ] ,
78- settings : form . settings || { } ,
79- } ,
80- } ;
94+ return publicSchemaFrom ( form ) ;
8195 } ,
8296
8397 async getPublicSchema ( slug : string ) {
8498 const form = await this . findBySlug ( slug ) ;
85- if ( ! form ) return null ;
86-
87- return {
88- data : {
89- id : form . id ,
90- title : form . title ,
91- slug : form . slug ,
92- description : form . description ,
93- fields : form . fields || [ ] ,
94- conditionalLogic : form . conditionalLogic || [ ] ,
95- settings : form . settings || { } ,
96- } ,
97- } ;
99+ return publicSchemaFrom ( form ) ;
98100 } ,
99101} ) ;
102+
103+ function publicSchemaFrom ( form : any ) {
104+ if ( ! form || ! form . publishedAt ) return null ;
105+ // Legacy fallback: forms published before publishedData existed serve their current copy.
106+ const p = form . publishedData || snapshotOf ( form ) ;
107+ return {
108+ data : {
109+ id : form . id ,
110+ slug : form . slug ,
111+ title : p . title ,
112+ description : p . description ,
113+ fields : p . fields || [ ] ,
114+ conditionalLogic : p . conditionalLogic || [ ] ,
115+ settings : p . settings || { } ,
116+ } ,
117+ } ;
118+ }
0 commit comments