Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

view row color on insert / update

Fendy Heryanto46b26e32a15c
diff --git a/packages/nocodb-sdk-v2/src/lib/Api.ts b/packages/nocodb-sdk-v2/src/lib/Api.ts
index d996264043..039fc3274f 100644
--- a/packages/nocodb-sdk-v2/src/lib/Api.ts
+++ b/packages/nocodb-sdk-v2/src/lib/Api.ts
@@ -516,6 +516,17 @@ export interface ViewOptionsForm {
};
}
+export interface ViewRowColorCreate {
+ mode?: 'filter' | 'select';
+ field_id?: string;
+ apply_as_row_background?: boolean;
+ conditions?: {
+ apply_as_row_background?: boolean;
+ color?: string;
+ filters?: FilterCreate;
+ }[];
+}
+
export interface FieldBase {
/** Unique identifier for the field. */
id?: string;
diff --git a/packages/nocodb-sdk/src/lib/arrayHelpers.spec.ts b/packages/nocodb-sdk/src/lib/arrayHelpers.spec.ts
index 2de38d4877..e33b465ac7 100644
--- a/packages/nocodb-sdk/src/lib/arrayHelpers.spec.ts
+++ b/packages/nocodb-sdk/src/lib/arrayHelpers.spec.ts
@@ -1,4 +1,8 @@
-import { arrDetailedDiff, arrIntersection } from './arrayHelpers';
+import {
+ arrDetailedDiff,
+ arrFlattenChildren,
+ arrIntersection,
+} from './arrayHelpers';
describe('arrayHelpers', () => {
describe('arrIntersection', () => {
@@ -21,4 +25,50 @@ describe('arrayHelpers', () => {
});
});
});
+
+ describe('arrFlattenChildren', () => {
+ it('will flatten children', () => {
+ const payload = [
+ {
+ name: 'foo',
+ children: [
+ {
+ name: 'bar',
+ },
+ {
+ name: 'baz',
+ },
+ ],
+ },
+ {
+ name: 'foo2',
+ children: [
+ {
+ name: 'bar2',
+ },
+ {
+ name: 'baz2',
+ children: [
+ {
+ name: 'buz2',
+ },
+ ],
+ },
+ ],
+ },
+ {
+ name: 'foo3',
+ },
+ ];
+ const result = arrFlattenChildren<any, any>({
+ payload,
+ childHandle: (each) => {
+ return each.children;
+ },
+ });
+ expect(result.map((k) => k.name).join(',')).toBe(
+ 'foo,bar,baz,foo2,bar2,baz2,buz2,foo3'
+ );
+ });
+ });
});
diff --git a/packages/nocodb-sdk/src/lib/arrayHelpers.ts b/packages/nocodb-sdk/src/lib/arrayHelpers.ts
index c4d327c6f9..dd571315ff 100644
--- a/packages/nocodb-sdk/src/lib/arrayHelpers.ts
+++ b/packages/nocodb-sdk/src/lib/arrayHelpers.ts
@@ -23,3 +23,29 @@ export const arrGetDuplicate = (data: string | number[]) => {
}
return undefined;
};
+
+export const arrFlattenChildren = <
+ T extends Record<string, any>,
+ V extends Record<string, any>
+>(
+ param: {
+ payload: T[];
+ childHandle: (t: T) => (T & Partial<V>)[];
+ },
+ acc: (T & Partial<V>)[] = []
+): (T & Partial<V>)[] => {
+ for (const each of param.payload) {
+ acc.push(each as T & Partial<V>);
+ const children = param.childHandle(each);
+ if (children && children.length > 0) {
+ arrFlattenChildren(
+ {
+ payload: children,
+ childHandle: param.childHandle,
+ },
+ acc
+ );
+ }
+ }
+ return acc;
+};
diff --git a/packages/nocodb/src/modules/noco.module.ts b/packages/nocodb/src/modules/noco.module.ts
index a43842b828..b198fcb7a0 100644
--- a/packages/nocodb/src/modules/noco.module.ts
+++ b/packages/nocodb/src/modules/noco.module.ts
@@ -111,6 +111,7 @@ import { McpTokenService } from '~/services/mcp.service';
import { McpService } from '~/mcp/mcp.service';
import { McpController } from '~/mcp/mcp.controller';
import { InternalController } from '~/controllers/internal.controller';
+import { ViewRowColorV3Service } from '~/services/v3/view-row-color-v3.service';
/* Datas */
import { BulkDataAliasController } from '~/controllers/bulk-data-alias.controller';
@@ -315,6 +316,7 @@ export const nocoModuleMetadata = {
McpTokenService,
McpService,
ViewRowColorService,
+ ViewRowColorV3Service,
/* Datas */
DataTableService,
@@ -372,6 +374,7 @@ export const nocoModuleMetadata = {
IntegrationsService,
NocoJobsService,
ViewRowColorService,
+ ViewRowColorV3Service,
/* Datas */
DatasService,
diff --git a/packages/nocodb/src/schema/swagger-v3.json b/packages/nocodb/src/schema/swagger-v3.json
index bdcfc6bbc4..e513d05544 100644
--- a/packages/nocodb/src/schema/swagger-v3.json
+++ b/packages/nocodb/src/schema/swagger-v3.json
@@ -5261,6 +5261,9 @@
"$ref": "#/components/schemas/SortCreate"
},
"description": "Sort options for the view."
+ },
+ "row_coloring": {
+ "$ref": "#/components/schemas/ViewRowColorCreate"
}
}
},
@@ -5713,6 +5716,41 @@
}
}
},
+ "ViewRowColorCreate": {
+ "type": "object",
+ "properties": {
+ "mode": {
+ "type": "string",
+ "enum": [
+ "filter",
+ "select"
+ ]
+ },
+ "field_id": {
+ "type": "string"
+ },
+ "apply_as_row_background": {
+ "type": "boolean"
+ },
+ "conditions": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "apply_as_row_background": {
+ "type": "boolean"
+ },
+ "color": {
+ "type": "string"
+ },
+ "filters": {
+ "$ref": "#/components/schemas/FilterCreate"
+ }
+ }
+ }
+ }
+ }
+ },
"FieldBase": {
"type": "object",
"properties": {
diff --git a/packages/nocodb/src/services/v3/filters-v3.service.ts b/packages/nocodb/src/services/v3/filters-v3.service.ts
index d296100612..fa9d75f46a 100644
--- a/packages/nocodb/src/services/v3/filters-v3.service.ts
+++ b/packages/nocodb/src/services/v3/filters-v3.service.ts
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
+import RowColorCondition from 'src/models/RowColorCondition';
import type {
FilterCreateV3Type,
FilterGroupV3Type,
@@ -131,7 +132,11 @@ export class FiltersV3Service {
ncMeta,
}: {
context: any;
- param: { viewId: string } | { hookId: string } | { linkColumnId: string };
+ param:
+ | { viewId: string }
+ | { hookId: string }
+ | { linkColumnId: string }
+ | { rowColorConditionId: string };
groupOrFilter: FilterCreateV3Type;
parentId?: string | null;
logicalOp?: 'AND' | 'OR' | null;
@@ -299,8 +304,10 @@ export class FiltersV3Service {
| { hookId: string }
| {
linkColumnId: string;
- },
+ }
+ | { rowColorConditionId: string },
context: NcContext,
+ ncMeta?: MetaService,
) {
let additionalProps = {};
let additionalAuditProps = {};
@@ -318,6 +325,24 @@ export class FiltersV3Service {
additionalAuditProps = {
hook,
};
+ } else if ('rowColorConditionId' in param && param.rowColorConditionId) {
+ const rowColorCondition = await RowColorCondition.getById(
+ context,
+ param.rowColorConditionId,
+ ncMeta,
+ );
+ if (!rowColorCondition) {
+ // TODO: better error
+ NcError.get(context).invalidRequestBody(
+ `Row color condition not found`,
+ );
+ }
+ additionalProps = {
+ fk_row_color_condition_id: param.rowColorConditionId,
+ };
+ additionalAuditProps = {
+ rowColorCondition,
+ };
} else if ('viewId' in param && param.viewId) {
const view = await View.get(context, param.viewId);
diff --git a/packages/nocodb/src/services/v3/view-row-color-v3.service.ts b/packages/nocodb/src/services/v3/view-row-color-v3.service.ts
new file mode 100644
index 0000000000..52476f940e
--- /dev/null
+++ b/packages/nocodb/src/services/v3/view-row-color-v3.service.ts
@@ -0,0 +1,20 @@
+import { Injectable } from '@nestjs/common';
+import type {
+ NcContext,
+ NcRequest,
+ ViewRowColorCreateV3Type,
+} from 'nocodb-sdk';
+import type { MetaService } from '~/meta/meta.service';
+
+@Injectable()
+export class ViewRowColorV3Service {
+ async replace(
+ _context: NcContext,
+ _params: {
+ viewId: string;
+ body: ViewRowColorCreateV3Type;
+ req: NcRequest;
+ },
+ _ncMeta?: MetaService,
+ ) {}
+}
diff --git a/packages/nocodb/src/services/v3/views-v3.service.ts b/packages/nocodb/src/services/v3/views-v3.service.ts
index 23c8496458..311ee365a7 100644
--- a/packages/nocodb/src/services/v3/views-v3.service.ts
+++ b/packages/nocodb/src/services/v3/views-v3.service.ts
@@ -7,7 +7,6 @@ import {
viewTypeAlias,
ViewTypes,
} from 'nocodb-sdk';
-import { handleFieldsRequestBody } from './view-v3/fields.helper';
import type {
RowColoringInfo,
ViewCreateV3Type,
@@ -17,6 +16,8 @@ import type { MetaService } from '~/meta/meta.service';
import type { ApiV3DataTransformationBuilder } from '~/utils/data-transformation.builder';
import type { NcContext, NcRequest } from '~/interface/config';
import type { Column, GridViewColumn } from '~/models';
+import { handleFieldsRequestBody } from '~/services/v3/view-v3/fields.helper';
+import { ViewRowColorV3Service } from '~/services/v3/view-row-color-v3.service';
import { GridsService } from '~/services/grids.service';
import { CalendarsService } from '~/services/calendars.service';
import { KanbansService } from '~/services/kanbans.service';
@@ -71,6 +72,7 @@ export class ViewsV3Service {
constructor(
protected readonly viewsService: ViewsService,
protected readonly viewColumnsService: ViewColumnsService,
+ protected readonly viewRowColorV3Service: ViewRowColorV3Service,
protected filtersV3Service: FiltersV3Service,
protected sortsV3Service: SortsV3Service,
protected viewRowColorService: ViewRowColorService,
@@ -790,6 +792,17 @@ export class ViewsV3Service {
trxNcMeta,
);
}
+ if (body.row_coloring) {
+ await this.viewRowColorV3Service.replace(
+ context,
+ {
+ viewId: insertedV2View.id,
+ body: body.row_coloring,
+ req,
+ },
+ trxNcMeta,
+ );
+ }
if (requestBody.filters) {
await this.filtersV3Service.insertFilterGroup({
context,
@@ -1253,6 +1266,17 @@ export class ViewsV3Service {
viewId: existingView.id,
});
}
+ if ('row_coloring' in body) {
+ await this.viewRowColorV3Service.replace(
+ context,
+ {
+ viewId: existingView.id,
+ body: body.row_coloring,
+ req,
+ },
+ trxNcMeta,
+ );
+ }
// if sort is empty array, we clear sort
if (
![ViewTypes.FORM].includes(existingView.type) &&