Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Related pubs in form #910

Merged
merged 25 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8de637b
Add RelatedPubsElement
allisonking Jan 16, 2025
ef1047f
Add side panel
allisonking Jan 16, 2025
c5dc1a1
Add pub selection
allisonking Jan 16, 2025
44f9d2e
wip saving values
allisonking Jan 21, 2025
4e32399
Remove pubtype check from related pubs form element
allisonking Jan 21, 2025
c998757
maybe normalize pub update?
allisonking Jan 21, 2025
f76e2ab
Add related pubs when creating a pub
allisonking Jan 21, 2025
3402e49
Start adding value component
allisonking Jan 21, 2025
5d7466c
Use useFieldArray to make array manipulation easier
allisonking Jan 22, 2025
6e32a06
Refactor so related pubs can use the same form element
allisonking Jan 22, 2025
4a48630
Make updatePub work for related values
allisonking Jan 22, 2025
3178f14
Fix tests
allisonking Jan 22, 2025
46379df
A few more fixes
allisonking Jan 22, 2025
1995b69
Handle null related fields
allisonking Jan 23, 2025
7586f01
Fix demo component for text input
allisonking Jan 23, 2025
33c2bfd
Add related pub e2e test
allisonking Jan 23, 2025
0b7067d
Fix button type to stop inadvertent submitting
allisonking Jan 23, 2025
7dcea5d
Merge branch 'main' into aking/791/related-pubs-in-form
allisonking Jan 23, 2025
ef7321f
Try increasing timeout
allisonking Jan 24, 2025
bcefc7b
Add wait for
allisonking Jan 29, 2025
226b602
Merge branch 'main' into aking/791/related-pubs-in-form
allisonking Jan 30, 2025
5bbbcaf
Fix date instantiation of related fields
allisonking Feb 3, 2025
94204a2
Use pub title
allisonking Feb 5, 2025
fcea612
Add better error message
allisonking Feb 5, 2025
ad1d545
Merge branch 'main' into aking/791/related-pubs-in-form
allisonking Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make updatePub work for related values
  • Loading branch information
allisonking committed Jan 23, 2025
commit 4a4863041d75650ef323aaa7352fe47f08b9ccdc
73 changes: 48 additions & 25 deletions core/lib/server/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {

import { sql, Transaction } from "kysely";
import { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/postgres";
import partition from "lodash.partition";

import type {
CreatePubRequestBodyWithNullsNew,
Expand Down Expand Up @@ -1128,34 +1129,56 @@ export const updatePub = async ({
};
}

// separate out relation fields
// call replacePubRelationsBySlug
// Separate into fields with relationships and those without
const [pubValuesWithRelations, pubValuesWithoutRelations] = partition(
pubValuesWithSchemaNameAndFieldId,
(pv) => pv.relatedPubId
);

const result = await autoRevalidate(
trx
.insertInto("pub_values")
.values(
pubValuesWithSchemaNameAndFieldId.map(({ value, fieldId }) => ({
pubId,
fieldId,
value: JSON.stringify(value),
lastModifiedBy,
}))
)
.onConflict((oc) =>
oc
// we have a unique index on pubId and fieldId where relatedPubId is null
.columns(["pubId", "fieldId"])
.where("relatedPubId", "is", null)
.doUpdateSet((eb) => ({
value: eb.ref("excluded.value"),
lastModifiedBy: eb.ref("excluded.lastModifiedBy"),
if (pubValuesWithRelations.length) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might result in an inconsistent return—should I update replacePubRelationsBySlug to also return something like what the .insertInto("pub_values") below does and then combine them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that if this works then just leave it, i'm currently rewriting a bunch of this anyway!

await replacePubRelationsBySlug({
pubId,
relations: pubValuesWithRelations.map(
(pv) =>
({
value: pv.value,
slug: pv.slug,
relatedPubId: pv.relatedPubId,
}) as AddPubRelationsInput
),
communityId,
lastModifiedBy,
trx,
});
}

if (pubValuesWithoutRelations.length) {
const result = await autoRevalidate(
trx
.insertInto("pub_values")
.values(
pubValuesWithRelations.map(({ value, fieldId }) => ({
pubId,
fieldId,
value: JSON.stringify(value),
lastModifiedBy,
}))
)
.returningAll()
).execute();
)
.onConflict((oc) =>
oc
// we have a unique index on pubId and fieldId where relatedPubId is null
.columns(["pubId", "fieldId"])
.where("relatedPubId", "is", null)
.doUpdateSet((eb) => ({
value: eb.ref("excluded.value"),
lastModifiedBy: eb.ref("excluded.lastModifiedBy"),
}))
)
.returningAll()
).execute();

return result;
return result;
}
});

return result;
Expand Down