Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,429 @@
import { BULK_REINDEX_SUPPORTED_INDEX_NAMES, crmContactsIndex, kbArticlesIndex, rowsIndex, usersIndex } from "@toolctrl/search";
import type { SearchIndexerBulkPager, SearchIndexerDependencies } from "./search-indexer.js";
type KnowledgeBaseArticleRow = {
id: string;
knowledgeBaseId: string;
slug: string;
title: string;
description: string;
contentPublishedAsText: string;
language: string;
publishedAt: Date | string | null;
createdAt: Date | string;
updatedAt: Date | string | null;
knowledgeBase: { tenantId: string | null };
};
const SEARCHABLE_ROW_PROPERTY_TYPES = new Set([1, 8]); // TEXT, SELECT
type RowSearchProperty = {
id: string;
name: string;
type: number;
};
type RowSearchValue = {
propertyId: string;
textValue: string | null;
numberValue: { toString(): string } | number | string | null;
dateValue: Date | string | null;
booleanValue: boolean | null;
property: RowSearchProperty;
};
type SearchRow = {
id: string;
tenantId: string | null;
entityId: string;
folio: number;
createdByUserId: string | null;
createdAt: Date | string;
updatedAt: Date | string;
deletedAt: Date | string | null;
entity: {
name: string;
properties: RowSearchProperty[];
};
values: RowSearchValue[];
};
const TENANT_USER_STATUS_ACTIVE = 2;
type TenantUserSearchRow = {
id: string;
tenantId: string;
userId: string;
type: number;
status: number;
user: {
id: string;
createdAt: Date | string;
email: string;
firstName: string;
lastName: string;
active: boolean;
admin: { userId: string } | null;
logs: { createdAt: Date | string }[];
};
};
export type SearchIndexerBulkPagerPrisma = {
knowledgeBaseArticle: {
findMany(args: {
where: {
id?: { gt: string };
knowledgeBase: { tenantId: string };
};
orderBy: { id: "asc" };
take: number;
select: {
id: true;
knowledgeBaseId: true;
slug: true;
title: true;
description: true;
contentPublishedAsText: true;
language: true;
publishedAt: true;
createdAt: true;
updatedAt: true;
knowledgeBase: { select: { tenantId: true } };
};
}): Promise<KnowledgeBaseArticleRow[]>;
};
row: {
findMany(args: {
where: {
id?: { gt: string };
tenantId: string;
deletedAt: null;
entity?: { name: string };
};
orderBy: { id: "asc" };
take: number;
select: {
id: true;
tenantId: true;
entityId: true;
folio: true;
createdByUserId: true;
createdAt: true;
updatedAt: true;
deletedAt: true;
entity: {
select: {
name: true;
properties: {
select: {
id: true;
name: true;
type: true;
};
};
};
};
values: {
select: {
propertyId: true;
textValue: true;
numberValue: true;
dateValue: true;
booleanValue: true;
property: {
select: {
id: true;
name: true;
type: true;
};
};
};
};
};
}): Promise<SearchRow[]>;
};
tenantUser: {
findMany(args: {
where: {
id?: { gt: string };
tenantId: string;
};
orderBy: { id: "asc" };
take: number;
select: {
id: true;
tenantId: true;
userId: true;
type: true;
status: true;
user: {
select: {
id: true;
createdAt: true;
email: true;
firstName: true;
lastName: true;
active: true;
admin: { select: { userId: true } };
logs: {
where: { action: "Login" };
orderBy: { createdAt: "desc" };
take: 1;
select: { createdAt: true };
};
};
};
};
}): Promise<TenantUserSearchRow[]>;
};
};
export function createSearchIndexerBulkPagers(input: { prisma: SearchIndexerBulkPagerPrisma }): NonNullable<SearchIndexerDependencies["bulkPagers"]> {
return {
[kbArticlesIndex.name]: createKbArticlesPager(input.prisma),
[crmContactsIndex.name]: createCrmContactsPager(input.prisma),
[rowsIndex.name]: createRowsPager(input.prisma),
[usersIndex.name]: createUsersPager(input.prisma),
};
}
export const registeredSearchIndexerBulkPagerNames: readonly string[] = BULK_REINDEX_SUPPORTED_INDEX_NAMES;
function createKbArticlesPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager {
return async ({ tenantId, cursor, pageSize }) => {
const rows = await prisma.knowledgeBaseArticle.findMany({
where: {
...(cursor ? { id: { gt: cursor } } : {}),
knowledgeBase: { tenantId },
},
orderBy: { id: "asc" },
take: pageSize,
select: {
id: true,
knowledgeBaseId: true,
slug: true,
title: true,
description: true,
contentPublishedAsText: true,
language: true,
publishedAt: true,
createdAt: true,
updatedAt: true,
knowledgeBase: { select: { tenantId: true } },
},
});
return {
documents: rows
.filter((row) => row.knowledgeBase.tenantId === tenantId)
.map((row) => ({
id: row.id,
tenantId,
knowledgeBaseId: row.knowledgeBaseId,
slug: row.slug,
title: row.title,
summary: row.description,
content: row.contentPublishedAsText,
tags: [],
language: row.language,
isPublished: row.publishedAt !== null,
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
publishedAt: serializeDate(row.publishedAt),
})),
nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null,
};
};
}
function createCrmContactsPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager {
return async ({ tenantId, cursor, pageSize }) => {
const rows = await findSearchRows(prisma, {
tenantId,
cursor,
pageSize,
entityName: "contact",
});
return {
documents: rows.filter((row) => row.tenantId === tenantId && row.entity.name === "contact").map((row) => rowToContactDocument(row, tenantId)),
nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null,
};
};
}
function createRowsPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager {
return async ({ tenantId, cursor, pageSize }) => {
const rows = await findSearchRows(prisma, { tenantId, cursor, pageSize });
return {
documents: rows.filter((row) => row.tenantId === tenantId).map((row) => rowToRowsDocument(row, tenantId)),
nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null,
};
};
}
function createUsersPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager {
return async ({ tenantId, cursor, pageSize }) => {
const rows = await prisma.tenantUser.findMany({
where: {
...(cursor ? { id: { gt: cursor } } : {}),
tenantId,
},
orderBy: { id: "asc" },
take: pageSize,
select: {
id: true,
tenantId: true,
userId: true,
type: true,
status: true,
user: {
select: {
id: true,
createdAt: true,
email: true,
firstName: true,
lastName: true,
active: true,
admin: { select: { userId: true } },
logs: {
where: { action: "Login" },
orderBy: { createdAt: "desc" },
take: 1,
select: { createdAt: true },
},
},
},
},
});
return {
documents: rows.filter((row) => row.tenantId === tenantId).map((row) => tenantUserToUserDocument(row, tenantId)),
nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null,
};
};
}
function findSearchRows(
prisma: SearchIndexerBulkPagerPrisma,
input: { tenantId: string; cursor: string | null; pageSize: number; entityName?: string }
) {
return prisma.row.findMany({
where: {
...(input.cursor ? { id: { gt: input.cursor } } : {}),
tenantId: input.tenantId,
deletedAt: null,
...(input.entityName ? { entity: { name: input.entityName } } : {}),
},
orderBy: { id: "asc" },
take: input.pageSize,
select: {
id: true,
tenantId: true,
entityId: true,
folio: true,
createdByUserId: true,
createdAt: true,
updatedAt: true,
deletedAt: true,
entity: {
select: {
name: true,
properties: {
select: {
id: true,
name: true,
type: true,
},
},
},
},
values: {
select: {
propertyId: true,
textValue: true,
numberValue: true,
dateValue: true,
booleanValue: true,
property: {
select: {
id: true,
name: true,
type: true,
},
},
},
},
},
});
}
function tenantUserToUserDocument(row: TenantUserSearchRow, tenantId: string): Record<string, unknown> & { id: string; tenantId: string } {
return {
id: row.user.id,
tenantId,
firstName: row.user.firstName,
lastName: row.user.lastName,
email: row.user.email,
type: row.type,
admin: row.user.admin !== null,
active: row.user.active && row.status === TENANT_USER_STATUS_ACTIVE,
createdAt: serializeDate(row.user.createdAt),
lastLoginAt: serializeDate(row.user.logs[0]?.createdAt ?? null),
};
}
function rowToContactDocument(row: SearchRow, tenantId: string): Record<string, unknown> & { id: string; tenantId: string } {
return {
id: row.id,
tenantId,
firstName: getTextValue(row, "firstName"),
lastName: getTextValue(row, "lastName"),
email: getTextValue(row, "email"),
company: getTextValue(row, "company"),
jobTitle: getTextValue(row, "jobTitle"),
status: getTextValue(row, "status"),
ownerId: row.createdByUserId ?? "",
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
};
}
function rowToRowsDocument(row: SearchRow, tenantId: string): Record<string, unknown> & { id: string; tenantId: string } {
return {
id: row.id,
tenantId,
entityId: row.entityId,
entityName: row.entity.name,
folio: typeof row.folio === "number" ? row.folio : 0,
searchable: buildSearchableBlob(row),
createdById: row.createdByUserId ?? "",
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
};
}
function buildSearchableBlob(row: SearchRow): string {
return row.entity.properties
.filter((property) => SEARCHABLE_ROW_PROPERTY_TYPES.has(property.type))
.map((property) => getTextValue(row, property.name))
.filter((value) => value.length > 0)
.join(" \n ");
}
function getTextValue(row: SearchRow, propertyName: string): string {
const property = row.entity.properties.find((item) => item.name === propertyName);
const value = property ? row.values.find((item) => item.propertyId === property.id) : undefined;
return normalizeRowValue(value).trim();
}
function normalizeRowValue(value: RowSearchValue | undefined): string {
if (!value) return "";
if (value.textValue !== null) return value.textValue;
if (value.numberValue !== null) return value.numberValue.toString();
if (value.dateValue !== null) return serializeDate(value.dateValue) ?? "";
if (value.booleanValue !== null) return String(value.booleanValue);
return "";
}
function serializeDate(value: Date | string | null): string | null {
if (value === null) return null;
return value instanceof Date ? value.toISOString() : value;
}