{document ? (
@@ -54,6 +58,27 @@ function Insights() {
justify="space-between"
>
+ {hasDataAttributes && (
+
+ {t("Data attributes")}
+
+ {dataAttributes.orderedData.map((dataAttribute) => (
+
+ {dataAttribute.name}:{" "}
+ {
+ document.dataAttributes?.find(
+ (da) => da.dataAttributeId === dataAttribute.id
+ )?.value
+ }
+
+ ))}
+
+ )}
{document.sourceMetadata && (
<>
diff --git a/app/utils/FeatureFlags.ts b/app/utils/FeatureFlags.ts
index 18ce3741ff..2e4ce0ccc5 100644
--- a/app/utils/FeatureFlags.ts
+++ b/app/utils/FeatureFlags.ts
@@ -4,11 +4,14 @@ import Storage from "@shared/utils/Storage";
export enum Feature {
/** New collection permissions UI */
newCollectionSharing = "newCollectionSharing",
+ /** Document data attributes */
+ dataAttributes = "dataAttributes",
}
/** Default values for feature flags */
const FeatureDefaults: Record = {
[Feature.newCollectionSharing]: true,
+ [Feature.dataAttributes]: false,
};
/**
diff --git a/server/migrations/20240611010054-create-data-attributes.js b/server/migrations/20240611010054-create-data-attributes.js
index 9f249c8461..d491e70fd6 100644
--- a/server/migrations/20240611010054-create-data-attributes.js
+++ b/server/migrations/20240611010054-create-data-attributes.js
@@ -109,7 +109,15 @@ module.exports = {
value: {
type: Sequelize.STRING,
allowNull: false,
- }
+ },
+ createdAt: {
+ type: Sequelize.DATE,
+ allowNull: false,
+ },
+ updatedAt: {
+ type: Sequelize.DATE,
+ allowNull: false,
+ },
}, {
transaction
});
diff --git a/server/models/DataAttribute.ts b/server/models/DataAttribute.ts
index cd26d426cb..a994798066 100644
--- a/server/models/DataAttribute.ts
+++ b/server/models/DataAttribute.ts
@@ -69,7 +69,7 @@ class DataAttribute extends ParanoidModel<
// hooks
@BeforeCreate
static async checkMaximumAttributes(model: DataAttribute) {
- const count = await DataAttribute.count({
+ const count = await this.count({
where: {
teamId: model.teamId,
},
diff --git a/server/models/DocumentDataAttribute.ts b/server/models/DocumentDataAttribute.ts
index a2c74dd5bb..5f9e54e278 100644
--- a/server/models/DocumentDataAttribute.ts
+++ b/server/models/DocumentDataAttribute.ts
@@ -9,7 +9,7 @@ import {
import DataAttribute from "./DataAttribute";
import Document from "./Document";
import User from "./User";
-import Model from "./base/Model";
+import IdModel from "./base/IdModel";
import Fix from "./decorators/Fix";
@Table({
@@ -17,7 +17,7 @@ import Fix from "./decorators/Fix";
modelName: "document_data_attribute",
})
@Fix
-class DocumentDataAttribute extends Model<
+class DocumentDataAttribute extends IdModel<
InferAttributes,
Partial>
> {
diff --git a/server/presenters/dataAttribute.ts b/server/presenters/dataAttribute.ts
index f1126151c3..f66d81643e 100644
--- a/server/presenters/dataAttribute.ts
+++ b/server/presenters/dataAttribute.ts
@@ -1,14 +1,15 @@
-import DocumentDataAttribute from "@server/models/DocumentDataAttribute";
+import DataAttribute from "@server/models/DataAttribute";
-export default function presentDocumentDataAttribute(
- dataAttribute: DocumentDataAttribute
-) {
+export default function presentDataAttribute(dataAttribute: DataAttribute) {
return {
id: dataAttribute.id,
- value: dataAttribute.value,
- dataAttributeId: dataAttribute.dataAttributeId,
- documentId: dataAttribute.documentId,
+ name: dataAttribute.name,
+ description: dataAttribute.description,
+ dataType: dataAttribute.dataType,
+ options: dataAttribute.options,
+ pinned: dataAttribute.pinned,
createdAt: dataAttribute.createdAt,
updatedAt: dataAttribute.updatedAt,
+ deletedAt: dataAttribute.deletedAt,
};
}
diff --git a/server/presenters/document.ts b/server/presenters/document.ts
index 892a801abd..2f3ae325c6 100644
--- a/server/presenters/document.ts
+++ b/server/presenters/document.ts
@@ -3,7 +3,7 @@ import { Document } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { TextHelper } from "@server/models/helpers/TextHelper";
import { APIContext } from "@server/types";
-import presentDocumentDataAttribute from "./dataAttribute";
+import presentDocumentDataAttribute from "./documentDataAttribute";
import presentUser from "./user";
type Options = {
@@ -77,9 +77,8 @@ async function presentDocument(
if (!options.isPublic) {
const source = await document.$get("import");
- data.dataAttributes = document.dataAttributes?.map(
- presentDocumentDataAttribute(dataAttribute)
- ) ?? [];
+ data.dataAttributes =
+ document.dataAttributes?.map(presentDocumentDataAttribute) ?? [];
data.isCollectionDeleted = await document.isCollectionDeleted();
data.collectionId = document.collectionId;
data.parentDocumentId = document.parentDocumentId;
diff --git a/server/presenters/documentDataAttribute.ts b/server/presenters/documentDataAttribute.ts
index f66d81643e..4924cc31f3 100644
--- a/server/presenters/documentDataAttribute.ts
+++ b/server/presenters/documentDataAttribute.ts
@@ -1,15 +1,14 @@
-import DataAttribute from "@server/models/DataAttribute";
+import type DocumentDataAttribute from "@server/models/DocumentDataAttribute";
-export default function presentDataAttribute(dataAttribute: DataAttribute) {
+export default function presentDocumentDataAttribute(
+ dataAttribute: DocumentDataAttribute
+) {
return {
id: dataAttribute.id,
- name: dataAttribute.name,
- description: dataAttribute.description,
- dataType: dataAttribute.dataType,
- options: dataAttribute.options,
- pinned: dataAttribute.pinned,
+ value: dataAttribute.value,
+ dataAttributeId: dataAttribute.dataAttributeId,
+ documentId: dataAttribute.documentId,
createdAt: dataAttribute.createdAt,
updatedAt: dataAttribute.updatedAt,
- deletedAt: dataAttribute.deletedAt,
};
}
diff --git a/server/routes/api/dataAttributes/schema.ts b/server/routes/api/dataAttributes/schema.ts
index 0cc5c3f569..dbe31a5e28 100644
--- a/server/routes/api/dataAttributes/schema.ts
+++ b/server/routes/api/dataAttributes/schema.ts
@@ -1,5 +1,5 @@
import z from "zod";
-import { DataAttributeDataType } from "@server/models/DataAttribute";
+import { DataAttributeDataType } from "@shared/models/types";
import { BaseSchema } from "../schema";
const BaseIdSchema = z.object({
@@ -34,24 +34,32 @@ export const DataAttributesListSchema = BaseSchema.extend({
export type DataAttributesListReq = z.infer;
export const DataAttributesCreateSchema = BaseSchema.extend({
- body: z.object({
- /** Name of the data attribute */
- name: z.string(),
+ body: z
+ .object({
+ /** Name of the data attribute */
+ name: z.string(),
- /** Description of the data attribute */
- description: z.string().optional(),
+ /** Description of the data attribute */
+ description: z.string().optional(),
- /** Type of the data attribute */
- dataType: z.nativeEnum(DataAttributeDataType),
+ /** Type of the data attribute */
+ dataType: z.nativeEnum(DataAttributeDataType),
- /** Options for the data attribute (if list type) */
- options: z.object({
- options: z.array(z.string()),
- }),
+ /** Options for the data attribute (if list type) */
+ options: z
+ .object({
+ options: z.array(z.string()),
+ })
+ .optional(),
- /** Whether the data attribute is pinned to the top of document */
- pinned: z.boolean().optional(),
- }),
+ /** Whether the data attribute is pinned to the top of document */
+ pinned: z.boolean().optional(),
+ })
+ .refine(
+ (val) =>
+ val.dataType !== DataAttributeDataType.List ||
+ (val.dataType === DataAttributeDataType.List && val.options)
+ ),
});
export type DataAttributesCreateReq = z.infer<
diff --git a/shared/validations.ts b/shared/validations.ts
index cc4974681b..bc26a4b1f5 100644
--- a/shared/validations.ts
+++ b/shared/validations.ts
@@ -34,8 +34,17 @@ export const DataAttributeValidation = {
/** The maximum length of the name */
maxNameLength: 255,
+ /** The maximum length of the description */
+ maxDescriptionLength: 1000,
+
/** The maximum number of data attributes */
max: 25,
+
+ /** The maximum number of data attributes that can be pinned */
+ maxPinned: 3,
+
+ /** The maximum number of options for a single attribute */
+ maxOptions: 10,
};
export const CollectionValidation = {