1
1
"use server" ;
2
2
3
- import type { JsonValue } from "contracts" ;
3
+ import type { Json , JsonValue } from "contracts" ;
4
4
import type { PubsId , PubTypesId , StagesId , UsersId } from "db/public" ;
5
5
import { Capabilities , FormAccessType , MemberRole , MembershipType } from "db/public" ;
6
6
import { logger } from "logger" ;
@@ -24,9 +24,16 @@ export const createPubRecursive = defineServerAction(async function createPubRec
24
24
props : CreatePubRecursiveProps & {
25
25
formSlug : string ;
26
26
addUserToForm ?: boolean ;
27
+ relation ?: {
28
+ pubId : PubsId ;
29
+ value : Date | Json ;
30
+ slug : string ;
31
+ } ;
27
32
}
28
33
) {
29
34
const {
35
+ communityId,
36
+ relation,
30
37
formSlug,
31
38
addUserToForm,
32
39
body : { values, ...body } ,
@@ -42,16 +49,20 @@ export const createPubRecursive = defineServerAction(async function createPubRec
42
49
if ( ! formSlug ) {
43
50
return ApiError . UNAUTHORIZED ;
44
51
}
45
- const [ form , canCreatePub ] = await Promise . all ( [
46
- formSlug
47
- ? await getForm ( { communityId : props . communityId , slug : formSlug } ) . executeTakeFirst ( )
48
- : null ,
52
+
53
+ const [ form , canCreatePub , canCreateRelation ] = await Promise . all ( [
54
+ getForm ( { communityId, slug : formSlug } ) . executeTakeFirst ( ) ,
49
55
userCanCreatePub ( {
50
56
userId : user . id ,
51
- communityId : props . communityId ,
57
+ communityId,
52
58
formSlug,
53
59
pubTypeId : body . pubTypeId as PubTypesId ,
54
60
} ) ,
61
+ relation &&
62
+ userCanEditPub ( {
63
+ pubId : relation . pubId ,
64
+ userId : user . id ,
65
+ } ) ,
55
66
] ) ;
56
67
57
68
if ( ! form ) {
@@ -73,6 +84,7 @@ export const createPubRecursive = defineServerAction(async function createPubRec
73
84
const result = await maybeWithTrx ( db , async ( trx ) => {
74
85
const createdPub = await createPubRecursiveNew ( {
75
86
...createPubProps ,
87
+ communityId,
76
88
body : {
77
89
...body ,
78
90
values : values
@@ -90,6 +102,17 @@ export const createPubRecursive = defineServerAction(async function createPubRec
90
102
trx,
91
103
} ) ;
92
104
105
+ if ( relation && canCreateRelation && body . id ) {
106
+ await PubOp . update ( relation . pubId , {
107
+ communityId,
108
+ lastModifiedBy,
109
+ continueOnValidationError : false ,
110
+ trx,
111
+ } )
112
+ . relate ( relation . slug , relation . value , body . id )
113
+ . execute ( ) ;
114
+ }
115
+
93
116
if ( addUserToForm && formSlug ) {
94
117
await grantFormAccess (
95
118
{
0 commit comments