From b1ceeceafb8ceb37e5c8d28d3680bc806f27fa84 Mon Sep 17 00:00:00 2001 From: nichenqin Date: Wed, 30 Oct 2024 13:27:50 +0800 Subject: [PATCH] feat: support formula field aggregate --- .../modules/schema/fields/field.aggregate.ts | 2 -- .../formula-field/formula-field.aggregate.ts | 34 ++++++++++++------- .../formula-field.specification.ts | 2 +- .../formula-field/formula-field.vo.ts | 6 ++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/table/src/modules/schema/fields/field.aggregate.ts b/packages/table/src/modules/schema/fields/field.aggregate.ts index 7e6839378..574242369 100644 --- a/packages/table/src/modules/schema/fields/field.aggregate.ts +++ b/packages/table/src/modules/schema/fields/field.aggregate.ts @@ -6,7 +6,6 @@ import { checkboxFieldAggregate } from "./variants/checkbox-field/checkbox-field import { currencyFieldAggregate } from "./variants/currency-field/currency-field.aggregate" import { durationFieldAggregate } from "./variants/duration-field/duration-field.aggregate" import { emailFieldAggregate } from "./variants/email-field/email-field.aggregate" -import { formulaFieldAggregate } from "./variants/formula-field/formula-field.aggregate" import { jsonFieldAggregate } from "./variants/json-field/json-field.aggregate" import { longTextFieldAggregate } from "./variants/long-text-field/long-text-field.aggregate" import { percentageFieldAggregate } from "./variants/percentage-field/percentage-field.aggregate" @@ -31,6 +30,5 @@ export const fieldAggregate = stringFieldAggregate .or(currencyFieldAggregate) .or(durationFieldAggregate) .or(percentageFieldAggregate) - .or(formulaFieldAggregate) export type IFieldAggregate = z.infer diff --git a/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.aggregate.ts b/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.aggregate.ts index 38fa66c76..dd6b87734 100644 --- a/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.aggregate.ts +++ b/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.aggregate.ts @@ -1,15 +1,23 @@ +import type { ReturnType } from "@undb/formula" import { z } from "@undb/zod" -export const formulaFieldAggregate = z.enum([ - // - // "sum", - // "avg", - // "min", - // "max", - "count_empty", - "count_uniq", - "count_not_empty", - "percent_empty", - "percent_not_empty", - "percent_uniq", -]) +export const createFormulaFieldAggregate = (returnType: ReturnType) => { + if (returnType === "boolean") { + return z.enum(["count_true", "count_false"]) + } else if (returnType === "number") { + return z.enum([ + "sum", + "avg", + "min", + "max", + "count_empty", + "count_uniq", + "count_not_empty", + "percent_empty", + "percent_not_empty", + "percent_uniq", + ]) + } + + return z.enum(["count_empty", "count_uniq", "count_not_empty", "percent_empty", "percent_not_empty", "percent_uniq"]) +} diff --git a/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.specification.ts b/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.specification.ts index c9efaee11..84ae0b54d 100644 --- a/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.specification.ts +++ b/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.specification.ts @@ -7,7 +7,7 @@ import { FormulaFieldValue } from "./formula-field-value.vo" export class FormulaEqual extends RecordComositeSpecification { constructor( - readonly value: number | null, + readonly value: number | null | boolean, readonly fieldId: FieldId, ) { super(fieldId) diff --git a/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.vo.ts b/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.vo.ts index e6eaecadc..dc1c4f15f 100644 --- a/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.vo.ts +++ b/packages/table/src/modules/schema/fields/variants/formula-field/formula-field.vo.ts @@ -9,7 +9,7 @@ import type { IFieldVisitor } from "../../field.visitor" import { AbstractField, baseFieldDTO, createBaseFieldDTO } from "../abstract-field.vo" import { StringEmpty } from "../string-field" import { FormulaFieldValue } from "./formula-field-value.vo" -import { formulaFieldAggregate } from "./formula-field.aggregate" +import { createFormulaFieldAggregate } from "./formula-field.aggregate" import { createFormulaFieldCondition, type IFormulaFieldCondition, @@ -138,6 +138,8 @@ export class FormulaField extends AbstractField new FormulaLTE(value, this.id)) .with({ op: "is_empty" }, () => new StringEmpty(this.id)) .with({ op: "is_not_empty" }, () => new StringEmpty(this.id).not()) + .with({ op: "is_true" }, () => new FormulaEqual(true, this.id)) + .with({ op: "is_false" }, () => new FormulaEqual(false, this.id).not()) .exhaustive() return Option(spec) @@ -152,7 +154,7 @@ export class FormulaField extends AbstractField