mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-18 00:19:49 +01:00
feat: ability to delete single legal values (#9058)
Co-authored-by: Thomas Heartman <thomas@getunleash.io>
This commit is contained in:
parent
790d813455
commit
c56200e030
@ -140,7 +140,7 @@ class ContextService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateContextFieldLegalValue(
|
async updateLegalValue(
|
||||||
contextFieldLegalValue: { name: string; legalValue: LegalValueSchema },
|
contextFieldLegalValue: { name: string; legalValue: LegalValueSchema },
|
||||||
auditUser: IAuditUser,
|
auditUser: IAuditUser,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
@ -179,6 +179,34 @@ class ContextService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteLegalValue(
|
||||||
|
contextFieldLegalValue: { name: string; legalValue: string },
|
||||||
|
auditUser: IAuditUser,
|
||||||
|
): Promise<void> {
|
||||||
|
const contextField = await this.contextFieldStore.get(
|
||||||
|
contextFieldLegalValue.name,
|
||||||
|
);
|
||||||
|
|
||||||
|
const newContextField = {
|
||||||
|
...contextField,
|
||||||
|
legalValues: contextField.legalValues?.filter(
|
||||||
|
(legalValue) =>
|
||||||
|
legalValue.value !== contextFieldLegalValue.legalValue,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
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(
|
async deleteContextField(
|
||||||
name: string,
|
name: string,
|
||||||
auditUser: IAuditUser,
|
auditUser: IAuditUser,
|
||||||
|
@ -204,6 +204,43 @@ test('should add and update a single context field with new legal values', async
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should delete a single context field legal value', async () => {
|
||||||
|
expect.assertions(1);
|
||||||
|
|
||||||
|
// add a new context field legal value
|
||||||
|
await request
|
||||||
|
.post(`${base}/api/admin/context/environment/legal-values`)
|
||||||
|
.send({
|
||||||
|
value: 'valueA',
|
||||||
|
})
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
await request
|
||||||
|
.post(`${base}/api/admin/context/environment/legal-values`)
|
||||||
|
.send({
|
||||||
|
value: 'valueB',
|
||||||
|
})
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
await request
|
||||||
|
.delete(`${base}/api/admin/context/environment/legal-values/valueB`)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const { body } = await request.get(`${base}/api/admin/context/environment`);
|
||||||
|
|
||||||
|
expect(body).toMatchObject({
|
||||||
|
name: 'environment',
|
||||||
|
legalValues: [{ value: 'valueA' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify delete is idempotent
|
||||||
|
await request
|
||||||
|
.delete(`${base}/api/admin/context/environment/legal-values/valueB`)
|
||||||
|
.expect(200);
|
||||||
|
});
|
||||||
|
|
||||||
test('should not delete a unknown context field', () => {
|
test('should not delete a unknown context field', () => {
|
||||||
expect.assertions(0);
|
expect.assertions(0);
|
||||||
|
|
||||||
|
@ -45,6 +45,10 @@ interface ContextParam {
|
|||||||
contextField: string;
|
contextField: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DeleteLegalValueParam extends ContextParam {
|
||||||
|
legalValue: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class ContextController extends Controller {
|
export class ContextController extends Controller {
|
||||||
private contextService: ContextService;
|
private contextService: ContextService;
|
||||||
|
|
||||||
@ -172,7 +176,7 @@ export class ContextController extends Controller {
|
|||||||
this.route({
|
this.route({
|
||||||
method: 'post',
|
method: 'post',
|
||||||
path: '/:contextField/legal-values',
|
path: '/:contextField/legal-values',
|
||||||
handler: this.updateContextFieldLegalValue,
|
handler: this.updateLegalValue,
|
||||||
permission: UPDATE_CONTEXT_FIELD,
|
permission: UPDATE_CONTEXT_FIELD,
|
||||||
middleware: [
|
middleware: [
|
||||||
openApiService.validPath({
|
openApiService.validPath({
|
||||||
@ -188,6 +192,25 @@ export class ContextController extends Controller {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.route({
|
||||||
|
method: 'delete',
|
||||||
|
path: '/:contextField/legal-values/:legalValue',
|
||||||
|
handler: this.deleteLegalValue,
|
||||||
|
acceptAnyContentType: true,
|
||||||
|
permission: UPDATE_CONTEXT_FIELD,
|
||||||
|
middleware: [
|
||||||
|
openApiService.validPath({
|
||||||
|
tags: ['Context'],
|
||||||
|
summary: 'Delete legal value for the context field',
|
||||||
|
description: `Removes the specified custom context field legal value. Does not validate that the legal value is not in use and does not remove usage from constraints that use it.`,
|
||||||
|
operationId: 'deleteContextFieldLegalValue',
|
||||||
|
responses: {
|
||||||
|
200: emptyResponse,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
this.route({
|
this.route({
|
||||||
method: 'delete',
|
method: 'delete',
|
||||||
path: '/:contextField',
|
path: '/:contextField',
|
||||||
@ -291,14 +314,28 @@ export class ContextController extends Controller {
|
|||||||
res.status(200).end();
|
res.status(200).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateContextFieldLegalValue(
|
async updateLegalValue(
|
||||||
req: IAuthRequest<ContextParam, void, LegalValueSchema>,
|
req: IAuthRequest<ContextParam, void, LegalValueSchema>,
|
||||||
res: Response,
|
res: Response,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const name = req.params.contextField;
|
const name = req.params.contextField;
|
||||||
const legalValue = req.body;
|
const legalValue = req.body;
|
||||||
|
|
||||||
await this.contextService.updateContextFieldLegalValue(
|
await this.contextService.updateLegalValue(
|
||||||
|
{ name, legalValue },
|
||||||
|
req.audit,
|
||||||
|
);
|
||||||
|
res.status(200).end();
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteLegalValue(
|
||||||
|
req: IAuthRequest<DeleteLegalValueParam, void>,
|
||||||
|
res: Response,
|
||||||
|
): Promise<void> {
|
||||||
|
const name = req.params.contextField;
|
||||||
|
const legalValue = req.params.legalValue;
|
||||||
|
|
||||||
|
await this.contextService.deleteLegalValue(
|
||||||
{ name, legalValue },
|
{ name, legalValue },
|
||||||
req.audit,
|
req.audit,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user