Skip to content

Commit

Permalink
Merge branch 'stable' of github.com:opsmill/infrahub into ple-cherry-…
Browse files Browse the repository at this point in the history
…pick-permissions-fixes
  • Loading branch information
pa-lem committed Dec 2, 2024
2 parents 2c09375 + b9fff8c commit bc2b851
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export const getFiltersFromFormData = (formData: Record<string, FormFieldValue>)
}

if (Array.isArray(fieldValue)) {
if (fieldValue.every((value) => typeof value === "string")) {
return [
...acc,
{
name: `${fieldName}__values`,
value: fieldValue,
},
];
}

return [
...acc,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export const getObjectFromFilters = (
};
}

if (fieldKey === "values") {
return {
...acc,
[fieldName]: { value: filter.value } satisfies AttributeType,
};
}

if (fieldKey === "ids") {
const relationshipSchema = schema.relationships?.find(({ name }) => name === fieldName);
if (!relationshipSchema) return acc;
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/src/components/form/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type AttributeValueFromUser =
source: {
type: SourceType;
};
value: string | number | boolean | null;
value: string | string[] | number | boolean | null;
}
| AttributeValueForCheckbox;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,29 @@ export const getCreateMutationFromFormData = (
}

if (typeof fieldData.value === "object") {
const fieldValue = Array.isArray(fieldData.value)
? fieldData.value.map(({ id }) => ({ id }))
: { id: fieldData.value.id };
return {
...acc,
[field.name]: fieldValue,
};
if (Array.isArray(fieldData.value)) {
// To differentiate between list (string[]) and relationship (Node[])
if (fieldData.value.every((value) => typeof value === "string")) {
return {
...acc,
[field.name]: { value: fieldData.value },
};
}

if (fieldData.value.every((value) => "id" in value)) {
return {
...acc,
[field.name]: fieldData.value.map(({ id }) => ({ id })),
};
}
}

if ("id" in fieldData.value) {
return {
...acc,
[field.name]: { id: fieldData.value.id },
};
}
}

const fieldValue = fieldData.value === "" ? null : fieldData.value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DynamicFieldProps, FormFieldValue } from "@/components/form/type";
import {
AttributeValueFromPool,
DynamicFieldProps,
FormFieldValue,
RelationshipValueFromPool,
} from "@/components/form/type";
import { isDeepEqual } from "remeda";

type GetUpdateMutationFromFormDataParams = {
Expand All @@ -19,7 +24,8 @@ export const getUpdateMutationFromFormData = ({

if (
fieldData.source?.type === "pool" &&
field.defaultValue?.source?.id === fieldData?.source?.id
(field.defaultValue as AttributeValueFromPool | RelationshipValueFromPool)?.source?.id ===
fieldData?.source?.id
) {
// If the same pool is selected, then remove from the updates
return acc;
Expand All @@ -38,13 +44,28 @@ export const getUpdateMutationFromFormData = ({
}

if (typeof fieldData.value === "object") {
const fieldValue = Array.isArray(fieldData.value)
? fieldData.value.map(({ id }) => ({ id }))
: { id: fieldData.value.id };
return {
...acc,
[field.name]: fieldValue,
};
if (Array.isArray(fieldData.value)) {
if (fieldData.value.every((value) => typeof value === "string")) {
return {
...acc,
[field.name]: { value: fieldData.value },
};
}

if (fieldData.value.every((value) => "id" in value)) {
return {
...acc,
[field.name]: fieldData.value.map(({ id }) => ({ id })),
};
}
}

if ("id" in fieldData.value) {
return {
...acc,
[field.name]: { id: fieldData.value.id },
};
}
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ describe("getFiltersFromFormData - test", () => {
});
});

it("returns an attribute of kind list value correctly", () => {
// GIVEN
const formData: Record<string, FormFieldValue> = {
field1: { source: { type: "user" }, value: ["value1"] },
};

// WHEN
const filters = getFiltersFromFormData(formData);

// THEN
expect(filters).toHaveLength(1);
expect(filters[0]).toEqual({
name: "field1__values",
value: ["value1"],
});
});

it("returns a relationship of cardinality one's value correctly", () => {
// GIVEN
const formData: Record<string, FormFieldValue> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ describe("getObjectFromFilters - test", () => {
});
});

it("returns value for an attribute of kind list correctly", () => {
// GIVEN
const filters: Array<Filter> = [{ name: "field1__values", value: ["value1"] }];

// WHEN
const objectData = getObjectFromFilters({} as any, filters);

// THEN
expect(objectData).toEqual({
field1: { value: ["value1"] },
});
});

it("returns value for a relationship of cardinality one correctly", () => {
// GIVEN
const filters: Array<Filter> = [{ name: "relationship1__ids", value: ["id1"] }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,70 @@ describe("getCreateMutationFromFormData - test", () => {
field1: { value: 0 },
});
});

describe("Attribute of kind list", () => {
it("set correctly attribute of kind list when value is from schema", () => {
// GIVEN
const fields: Array<DynamicFieldProps> = [
buildField({
name: "listField",
type: "List",
defaultValue: { source: { type: "schema" }, value: ["item1"] },
}),
];
const formData: Record<string, FormAttributeValue> = {
listField: { source: { type: "schema" }, value: ["item1"] },
};

// WHEN
const mutationData = getCreateMutationFromFormData(fields, formData);

// THEN
expect(mutationData).to.deep.equal({});
});

it("set correctly attribute of kind list when value is from user", () => {
// GIVEN
const fields: Array<DynamicFieldProps> = [
buildField({
name: "listField",
type: "List",
defaultValue: { source: null, value: null },
}),
];
const formData: Record<string, FormAttributeValue> = {
listField: { source: { type: "user" }, value: ["item2", "item3"] },
};

// WHEN
const mutationData = getCreateMutationFromFormData(fields, formData);

// THEN
expect(mutationData).to.deep.equal({
listField: { value: ["item2", "item3"] },
});
});

it("set correctly attribute field if value is from user and is an empty array", () => {
// GIVEN
const fields: Array<DynamicFieldProps> = [
buildField({
name: "listField",
type: "List",
defaultValue: { source: { type: "schema" }, value: ["item1"] },
}),
];
const formData: Record<string, FormAttributeValue> = {
listField: { source: { type: "user" }, value: [] },
};

// WHEN
const mutationData = getCreateMutationFromFormData(fields, formData);

// THEN
expect(mutationData).to.deep.equal({
listField: { value: [] },
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,50 @@ describe("getUpdateMutationFromFormData - test", () => {
field1: { is_default: true },
});
});

describe("Attribute of kind list", () => {
it("set correctly attribute of kind list when initial value is null", () => {
// GIVEN
const fields: Array<DynamicFieldProps> = [
buildField({
name: "listField",
type: "List",
defaultValue: { source: null, value: null },
}),
];
const formData: Record<string, FormAttributeValue> = {
listField: { source: { type: "user" }, value: ["item2", "item3"] },
};

// WHEN
const mutationData = getUpdateMutationFromFormData({ fields, formData });

// THEN
expect(mutationData).to.deep.equal({
listField: { value: ["item2", "item3"] },
});
});

it("set correctly attribute of kind list when initial has items", () => {
// GIVEN
const fields: Array<DynamicFieldProps> = [
buildField({
name: "listField",
type: "List",
defaultValue: { source: { type: "user" }, value: ["item1"] },
}),
];
const formData: Record<string, FormAttributeValue> = {
listField: { source: { type: "user" }, value: ["item2", "item3"] },
};

// WHEN
const mutationData = getUpdateMutationFromFormData({ fields, formData });

// THEN
expect(mutationData).to.deep.equal({
listField: { value: ["item2", "item3"] },
});
});
});
});

0 comments on commit bc2b851

Please sign in to comment.