@@ -22,18 +22,26 @@ type CreatePubRecursiveProps = Omit<Parameters<typeof createPubRecursiveNew>[0],
22
22
23
23
export const createPubRecursive = defineServerAction ( async function createPubRecursive (
24
24
props : CreatePubRecursiveProps & {
25
- formSlug ? : string ;
25
+ formSlug : string ;
26
26
addUserToForm ?: boolean ;
27
27
}
28
28
) {
29
- const { formSlug, addUserToForm, ...createPubProps } = props ;
29
+ const {
30
+ formSlug,
31
+ addUserToForm,
32
+ body : { values, ...body } ,
33
+ ...createPubProps
34
+ } = props ;
30
35
const loginData = await getLoginData ( ) ;
31
36
32
37
if ( ! loginData || ! loginData . user ) {
33
38
return ApiError . NOT_LOGGED_IN ;
34
39
}
35
40
const { user } = loginData ;
36
41
42
+ if ( ! formSlug ) {
43
+ return ApiError . UNAUTHORIZED ;
44
+ }
37
45
const [ form , canCreatePub ] = await Promise . all ( [
38
46
formSlug
39
47
? await getForm ( { communityId : props . communityId , slug : formSlug } ) . executeTakeFirst ( )
@@ -42,11 +50,15 @@ export const createPubRecursive = defineServerAction(async function createPubRec
42
50
userId : user . id ,
43
51
communityId : props . communityId ,
44
52
formSlug,
45
- pubTypeId : createPubProps . body . pubTypeId as PubTypesId ,
53
+ pubTypeId : body . pubTypeId as PubTypesId ,
46
54
} ) ,
47
55
] ) ;
48
56
49
- const isPublicForm = form ?. access === FormAccessType . public ;
57
+ if ( ! form ) {
58
+ return ApiError . UNAUTHORIZED ;
59
+ }
60
+
61
+ const isPublicForm = form . access === FormAccessType . public ;
50
62
51
63
if ( ! canCreatePub && ! isPublicForm ) {
52
64
return ApiError . UNAUTHORIZED ;
@@ -62,7 +74,14 @@ export const createPubRecursive = defineServerAction(async function createPubRec
62
74
const createdPub = await createPubRecursiveNew ( {
63
75
...createPubProps ,
64
76
body : {
65
- ...createPubProps . body ,
77
+ ...body ,
78
+ values : values
79
+ ? Object . fromEntries (
80
+ Object . entries ( values ) . filter ( ( [ slug ] ) =>
81
+ form . elements . find ( ( element ) => element . slug === slug )
82
+ )
83
+ )
84
+ : { } ,
66
85
// adds user to the pub
67
86
// TODO: this should be configured on the form
68
87
members : { [ user . id ] : MemberRole . contributor } ,
@@ -112,7 +131,7 @@ export const updatePub = defineServerAction(async function updatePub({
112
131
JsonValue | Date | { value : JsonValue | Date ; relatedPubId : PubsId } [ ]
113
132
> ;
114
133
stageId ?: StagesId ;
115
- formSlug ? : string ;
134
+ formSlug : string ;
116
135
continueOnValidationError : boolean ;
117
136
deleted : { slug : string ; relatedPubId : PubsId } [ ] ;
118
137
} ) {
@@ -128,7 +147,14 @@ export const updatePub = defineServerAction(async function updatePub({
128
147
return ApiError . COMMUNITY_NOT_FOUND ;
129
148
}
130
149
131
- if ( ! userCanEditPub ( { pubId, userId : loginData . user . id , formSlug } ) ) {
150
+ if ( ! formSlug ) {
151
+ return ApiError . UNAUTHORIZED ;
152
+ }
153
+
154
+ const form = await getForm ( { slug : formSlug , communityId : community . id } ) . executeTakeFirst ( ) ;
155
+ const canEdit = await userCanEditPub ( { pubId, userId : loginData . user . id , formSlug } ) ;
156
+
157
+ if ( ! form || ! canEdit ) {
132
158
return ApiError . UNAUTHORIZED ;
133
159
}
134
160
@@ -154,7 +180,11 @@ export const updatePub = defineServerAction(async function updatePub({
154
180
} ) ;
155
181
156
182
const normalizedValues = normalizePubValues ( processedVals ) ;
183
+
157
184
for ( const { slug, value, relatedPubId } of normalizedValues ) {
185
+ if ( ! form . elements . find ( ( element ) => element . slug === slug ) ) {
186
+ continue ;
187
+ }
158
188
if ( relatedPubId ) {
159
189
updateQuery . relate ( slug , value , relatedPubId , {
160
190
replaceExisting : false ,
0 commit comments