Skip to content

Commit

Permalink
feat: use object in filter definition
Browse files Browse the repository at this point in the history
  • Loading branch information
leopuleo committed Oct 25, 2023
1 parent cb701e9 commit 414b41f
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 213 deletions.
4 changes: 2 additions & 2 deletions packages/api-aco/__tests__/filter.hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const name = "Filter Lifecycle Events";
const namespace = "demo-lifecycle-events";
const operation = Operation.AND;
const groups = [
JSON.stringify({
{
operation: Operation.OR,
filters: [
{
Expand All @@ -17,7 +17,7 @@ const groups = [
value: "any-value"
}
]
})
}
];

describe("Filter Lifecycle Events", () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/api-aco/__tests__/filter.so.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe("`filter` CRUD", () => {
data: {
...filterMocks.filterA,
groups: [
JSON.stringify({
{
operation: "",
filters: [
{
Expand All @@ -261,7 +261,7 @@ describe("`filter` CRUD", () => {
value: "any"
}
]
})
}
]
}
});
Expand Down Expand Up @@ -292,10 +292,10 @@ describe("`filter` CRUD", () => {
data: {
...filterMocks.filterA,
groups: [
JSON.stringify({
{
operation: Operation.AND,
filters: []
})
}
]
}
});
Expand Down Expand Up @@ -326,7 +326,7 @@ describe("`filter` CRUD", () => {
data: {
...filterMocks.filterA,
groups: [
JSON.stringify({
{
operation: Operation.AND,
filters: [
{
Expand All @@ -335,7 +335,7 @@ describe("`filter` CRUD", () => {
value: ""
}
]
})
}
]
}
});
Expand Down
9 changes: 8 additions & 1 deletion packages/api-aco/__tests__/graphql/filter.gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ const DATA_FIELD = /* GraphQL */ `
description
namespace
operation
groups
groups {
operation
filters {
field
condition
value
}
}
createdBy {
id
displayName
Expand Down
12 changes: 6 additions & 6 deletions packages/api-aco/__tests__/mocks/filter.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const filterMocks = {
namespace: "demo-1",
operation: Operation.AND,
groups: [
JSON.stringify({
{
operation: Operation.AND,
filters: [
{
Expand All @@ -22,7 +22,7 @@ export const filterMocks = {
value: "value 2"
}
]
})
}
]
},
filterB: {
Expand All @@ -32,7 +32,7 @@ export const filterMocks = {
namespace: "demo-1",
operation: Operation.OR,
groups: [
JSON.stringify({
{
operation: Operation.OR,
filters: [
{
Expand All @@ -46,7 +46,7 @@ export const filterMocks = {
value: "value 2"
}
]
})
}
]
},
filterC: {
Expand All @@ -56,7 +56,7 @@ export const filterMocks = {
namespace: "demo-2",
operation: Operation.AND,
groups: [
JSON.stringify({
{
operation: Operation.OR,
filters: [
{
Expand All @@ -70,7 +70,7 @@ export const filterMocks = {
value: "value 2"
}
]
})
}
]
}
};
3 changes: 1 addition & 2 deletions packages/api-aco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"@webiny/pubsub": "0.0.0",
"@webiny/utils": "0.0.0",
"@webiny/validation": "0.0.0",
"lodash": "^4.4.2",
"zod": "^3.21.4"
"lodash": "^4.4.2"
},
"devDependencies": {
"@babel/cli": "^7.22.6",
Expand Down
27 changes: 0 additions & 27 deletions packages/api-aco/src/createAcoFields.ts

This file was deleted.

40 changes: 31 additions & 9 deletions packages/api-aco/src/filter/filter.gql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorResponse, ListResponse } from "@webiny/handler-graphql/responses";
import { GraphQLSchemaPlugin } from "@webiny/handler-graphql/plugins/GraphQLSchemaPlugin";

import { checkPermissions } from "~/utils/checkPermissions";
import { ensureAuthentication } from "~/utils/ensureAuthentication";
import { resolve } from "~/utils/resolve";

import { AcoContext } from "~/types";
Expand All @@ -13,33 +13,55 @@ export const filterSchema = new GraphQLSchemaPlugin<AcoContext>({
OR
}
type GroupFilter {
field: String!
condition: String!
value: String!
}
type Group {
operation: OperationEnum!
filters: [GroupFilter]!
}
type Filter {
id: ID!
name: String!
description: String
namespace: String!
operation: OperationEnum!
groups: [JSON]!
groups: [Group]!
savedOn: DateTime
createdOn: DateTime
createdBy: AcoUser
}
input GroupFilterInput {
field: String!
condition: String!
value: String!
}
input GroupInput {
operation: OperationEnum!
filters: [GroupFilterInput]!
}
input FilterCreateInput {
id: ID!
name: String!
description: String
namespace: String!
operation: OperationEnum!
groups: [JSON]!
groups: [GroupInput]!
}
input FilterUpdateInput {
name: String
description: String
namespace: String
operation: OperationEnum
groups: [JSON]
groups: [GroupInput]
}
input FiltersListWhereInput {
Expand Down Expand Up @@ -76,13 +98,13 @@ export const filterSchema = new GraphQLSchemaPlugin<AcoContext>({
AcoQuery: {
getFilter: async (_, { id }, context) => {
return resolve(() => {
checkPermissions(context);
ensureAuthentication(context);
return context.aco.filter.get(id);
});
},
listFilters: async (_, args: any, context) => {
try {
checkPermissions(context);
ensureAuthentication(context);
const [entries, meta] = await context.aco.filter.list(args);
return new ListResponse(entries, meta);
} catch (e) {
Expand All @@ -93,19 +115,19 @@ export const filterSchema = new GraphQLSchemaPlugin<AcoContext>({
AcoMutation: {
createFilter: async (_, { data }, context) => {
return resolve(() => {
checkPermissions(context);
ensureAuthentication(context);
return context.aco.filter.create(data);
});
},
updateFilter: async (_, { id, data }, context) => {
return resolve(() => {
checkPermissions(context);
ensureAuthentication(context);
return context.aco.filter.update(id, data);
});
},
deleteFilter: async (_, { id }, context) => {
return resolve(() => {
checkPermissions(context);
ensureAuthentication(context);
return context.aco.filter.delete(id);
});
}
Expand Down
79 changes: 75 additions & 4 deletions packages/api-aco/src/filter/filter.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createModelField } from "~/utils/createModelField";
import { CmsPrivateModelFull } from "@webiny/api-headless-cms";
import { CmsModelField } from "@webiny/api-headless-cms/types";

export type FilterModelDefinition = Omit<CmsPrivateModelFull, "noValidate" | "group">;

Expand Down Expand Up @@ -63,23 +64,87 @@ const operation = () =>
]
});

const groups = () =>
const groups = (fields: CmsModelField[]) =>
createModelField({
label: "Groups",
fieldId: "groups",
type: "wby-aco-json",
type: "object",
multipleValues: true,
settings: {
fields,
layout: fields.map(field => [field.storageId])
},
listValidation: [
{
name: "minLength",
message: "At least one group is required.",
settings: {
value: "1"
}
}
]
});

const filters = (fields: CmsModelField[]) =>
createModelField({
label: "Filters",
fieldId: "filters",
type: "object",
multipleValues: true,
settings: {
fields,
layout: fields.map(field => [field.storageId])
},
listValidation: [
{
name: "minLength",
message: "Value is too short.",
message: "At least one filter is required.",
settings: {
value: "1"
}
}
]
});

const field = () =>
createModelField({
label: "Field",
fieldId: "field",
type: "text",
validation: [
{
name: "required",
message: "Value is required."
}
]
});

const condition = () =>
createModelField({
label: "Condition",
fieldId: "condition",
type: "text",
validation: [
{
name: "required",
message: "Value is required."
}
]
});

const value = () =>
createModelField({
label: "Value",
fieldId: "value",
type: "text",
validation: [
{
name: "required",
message: "Value is required."
}
]
});

export const FILTER_MODEL_ID = "acoFilter";

export const createFilterModelDefinition = (): FilterModelDefinition => {
Expand All @@ -88,7 +153,13 @@ export const createFilterModelDefinition = (): FilterModelDefinition => {
modelId: FILTER_MODEL_ID,
titleFieldId: "name",
layout: [["name"], ["description"], ["namespace"], ["operation"], ["groups"]],
fields: [name(), description(), namespace(), operation(), groups()],
fields: [
name(),
description(),
namespace(),
operation(),
groups([operation(), filters([field(), condition(), value()])])
],
description: "ACO - Filter content model",
isPrivate: true
};
Expand Down
Loading

0 comments on commit 414b41f

Please sign in to comment.