Skip to content

Commit 810bff2

Browse files
committed
Fix relation overwrite bug
1 parent c7b2919 commit 810bff2

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

core/app/components/pubs/PubEditor/PubEditorClient.tsx

+5-11
Original file line numberDiff line numberDiff line change
@@ -351,18 +351,12 @@ export const PubEditorClient = ({
351351
},
352352
communityId: community.id,
353353
addUserToForm: isExternalForm,
354-
});
355-
// TODO: this currently overwrites existing pub values of the same field
356-
if (relatedPub) {
357-
await runUpdatePub({
354+
relation: relatedPub && {
355+
slug: relatedPub.slug,
358356
pubId: relatedPub.id,
359-
pubValues: {
360-
[relatedPub.slug]: [{ value: relatedPubValue, relatedPubId: pubId }],
361-
},
362-
continueOnValidationError: true,
363-
deleted: [],
364-
});
365-
}
357+
value: relatedPubValue,
358+
},
359+
});
366360
}
367361
if (didSucceed(result)) {
368362
// Reset dirty state to prevent the unsaved changes warning from

core/app/components/pubs/PubEditor/actions.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use server";
22

3-
import type { JsonValue } from "contracts";
3+
import type { Json, JsonValue } from "contracts";
44
import type { PubsId, PubTypesId, StagesId, UsersId } from "db/public";
55
import { Capabilities, FormAccessType, MemberRole, MembershipType } from "db/public";
66
import { logger } from "logger";
@@ -24,9 +24,16 @@ export const createPubRecursive = defineServerAction(async function createPubRec
2424
props: CreatePubRecursiveProps & {
2525
formSlug: string;
2626
addUserToForm?: boolean;
27+
relation?: {
28+
pubId: PubsId;
29+
value: Date | Json;
30+
slug: string;
31+
};
2732
}
2833
) {
2934
const {
35+
communityId,
36+
relation,
3037
formSlug,
3138
addUserToForm,
3239
body: { values, ...body },
@@ -42,16 +49,20 @@ export const createPubRecursive = defineServerAction(async function createPubRec
4249
if (!formSlug) {
4350
return ApiError.UNAUTHORIZED;
4451
}
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(),
4955
userCanCreatePub({
5056
userId: user.id,
51-
communityId: props.communityId,
57+
communityId,
5258
formSlug,
5359
pubTypeId: body.pubTypeId as PubTypesId,
5460
}),
61+
relation &&
62+
userCanEditPub({
63+
pubId: relation.pubId,
64+
userId: user.id,
65+
}),
5566
]);
5667

5768
if (!form) {
@@ -73,6 +84,7 @@ export const createPubRecursive = defineServerAction(async function createPubRec
7384
const result = await maybeWithTrx(db, async (trx) => {
7485
const createdPub = await createPubRecursiveNew({
7586
...createPubProps,
87+
communityId,
7688
body: {
7789
...body,
7890
values: values
@@ -90,6 +102,17 @@ export const createPubRecursive = defineServerAction(async function createPubRec
90102
trx,
91103
});
92104

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+
93116
if (addUserToForm && formSlug) {
94117
await grantFormAccess(
95118
{

0 commit comments

Comments
 (0)