-
-
Notifications
You must be signed in to change notification settings - Fork 737
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,10 @@ import { | |
} from '../../types'; | ||
import type { IPrivateProjectChecker } from '../private-project/privateProjectCheckerType'; | ||
import type EventService from '../events/event-service'; | ||
import { contextSchema } from '../../services/context-schema'; | ||
import { contextSchema, legalValueSchema } from '../../services/context-schema'; | ||
import { NameExistsError } from '../../error'; | ||
import { nameSchema } from '../../schema/feature-schema'; | ||
import type { LegalValueSchema } from '../../openapi'; | ||
|
||
class ContextService { | ||
private eventService: EventService; | ||
|
@@ -126,7 +127,6 @@ class ContextService { | |
); | ||
const value = await contextSchema.validateAsync(updatedContextField); | ||
|
||
// update | ||
await this.contextFieldStore.update(value); | ||
|
||
const { createdAt, sortOrder, ...previousContextField } = contextField; | ||
|
@@ -140,6 +140,45 @@ class ContextService { | |
}); | ||
} | ||
|
||
async updateContextFieldLegalValue( | ||
contextFieldLegalValue: { name: string; legalValue: LegalValueSchema }, | ||
auditUser: IAuditUser, | ||
): Promise<void> { | ||
const contextField = await this.contextFieldStore.get( | ||
contextFieldLegalValue.name, | ||
); | ||
const validatedLegalValue = await legalValueSchema.validateAsync( | ||
contextFieldLegalValue.legalValue, | ||
); | ||
|
||
const legalValues = contextField.legalValues | ||
? [...contextField.legalValues] | ||
: []; | ||
|
||
const existingIndex = legalValues.findIndex( | ||
(legalvalue) => legalvalue.value === validatedLegalValue.value, | ||
); | ||
Comment on lines
+158
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then again, maybe order is important? 🤷🏼 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this PR piggybacks on the current implementation |
||
|
||
if (existingIndex !== -1) { | ||
legalValues[existingIndex] = validatedLegalValue; | ||
} else { | ||
legalValues.push(validatedLegalValue); | ||
} | ||
|
||
const newContextField = { ...contextField, legalValues }; | ||
|
||
await this.contextFieldStore.update(newContextField); | ||
|
||
await this.eventService.storeEvent({ | ||
type: CONTEXT_FIELD_UPDATED, | ||
createdBy: auditUser.username, | ||
createdByUserId: auditUser.id, | ||
ip: auditUser.ip, | ||
preData: contextField, | ||
data: newContextField, | ||
}); | ||
} | ||
|
||
async deleteContextField( | ||
name: string, | ||
auditUser: IAuditUser, | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, also,
legalValueSchema
is notLegalValueSchema
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?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.