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

feat: ability to upsert single legal values #9056

Merged
merged 4 commits into from
Jan 3, 2025

Conversation

kwasniew
Copy link
Contributor

@kwasniew kwasniew commented Jan 3, 2025

About the changes

Ability to add/update a single legal value for the context.

Important decisions:

  • since it allows to add a new legal value or update description of a single legal value I decided to go for the PUT method since we know the identity of the legal value (context name + legal value name combination is unique).
  • in the event log I'm using the same CONTEXT_FIELD_UPDATED event instead of creating a new event for single legal value updates

Addresses: #8934

Followup PRs:

  • add delete single legal value capability
  • wrap all context updates in transactions since they touch context and event log

Important files

Discussion points

Copy link

vercel bot commented Jan 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-monorepo-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 3, 2025 1:13pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Jan 3, 2025 1:13pm

Copy link
Contributor

github-actions bot commented Jan 3, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Files

Copy link
Contributor

@thomasheartman thomasheartman left a comment

Choose a reason for hiding this comment

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

Regarding your decisions, I don't agree with this one:

since it allows to add a new legal value or update description of a single legal value I decided to go for the PUT method since we know the identity of the legal value (context name + legal value name combination is unique).

Because we're putting to :contextField/legalValues (why isn't it :contextField/legal-values?), I would expect the PUT to replace all the legal values (in which case, it should've also been a list). So in this case, because you're only sending a single value and want that added to the collection, I think POST is the right method to use.

(That way, it'd even make sense to allow sending either a single value or a list of values later, if that's something people want)

On the other hand, if you sent it to :contextField/legal-values/:legalValueName, then I think PUT would be the right verb.

As for reusing the context field updated event: yeah, that sounds right to me.

Comment on lines +150 to +152
const validatedLegalValue = await legalValueSchema.validateAsync(
contextFieldLegalValue.legalValue,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm missing something, but why are we using the schema to validate here? Isn't that automatically taken care of at the endpoint level?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, also, legalValueSchema is not LegalValueSchema from ../../openapi. I thought it looked like Joi, but was a little confused by the import (and didn't look hard enough, apparently).

But everything that the joi schema does, we should be able to do with openapi directly, right? Set string min and max lengths and allow description to be null, undefined, or an arbitrary string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's consistent with our current approach in the context module where openapi validates simple string and joi actual 1-100 characters. I didn't want to introduce a new convention in this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that's fair. I suspect we'll just forget about it and never touch it, then. But that's fine too. It's a little added complexity, but not a big deal here.

Comment on lines +158 to +160
const existingIndex = legalValues.findIndex(
(legalvalue) => legalvalue.value === validatedLegalValue.value,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, my. we store this in a list, huh? I imagine using a map would be better, but it's probably also not something we want to touch now, I guess 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

But then again, maybe order is important? 🤷🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this PR piggybacks on the current implementation

@kwasniew kwasniew merged commit 20fda18 into main Jan 3, 2025
11 checks passed
@kwasniew kwasniew deleted the adding-single-legal-values branch January 3, 2025 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants