Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,17 @@
// @vitest-environment node
import { describe, expect, test } from "vitest";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
const source = readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), "CrmService.ts"), "utf8");
describe("CrmService env boundary", () => {
test("uses typed web config for Formspree and stays free of US newsletter providers", () => {
expect(source).toContain("loadWebConfig().config.contactFormspreeEndpoint");
expect(source).not.toContain("process.env.INTEGRATIONS_CONTACT_FORMSPREE");
// EU-Sterilisation (R-P1a): ConvertKit-Provider vollständig entfernt.
expect(source).not.toContain("process.env.CONVERTKIT_");
expect(source).not.toContain("api.convertkit.com");
});
});
@@ -0,0 +1,68 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
/**
* PII-protection regression test for CrmService.
*
* Newsletter + ConvertKit code paths historically logged raw `response`
* objects (containing subscriber email) and raw exceptions. We tightened
* those to log only the operation tag, an ok-boolean, and a sanitised
* `err`. Never log: email, firstName, lastName, phone, response body,
* api key, contact object.
*/
describe("CrmService PII discipline", () => {
const SOURCE = readFileSync(resolve(__dirname, "CrmService.ts"), "utf8");
it("contains no live console.* call sites", () => {
const live = SOURCE.split("\n").filter((line, idx, all) => {
if (!/console\.(log|error|warn|info|debug)\s*\(/.test(line)) return false;
const trimmed = line.trimStart();
if (trimmed.startsWith("//") || trimmed.startsWith("*")) return false;
const prev = idx > 0 ? all[idx - 1].trim() : "";
if (/eslint-disable-next-line[^\n]*no-console/.test(prev)) return false;
return true;
});
expect(live).toEqual([]);
});
it("never passes contact PII keys as own crmLogger fields", () => {
const pattern = /crmLogger\.(info|warn|error|debug)\s*\(\s*\{([\s\S]*?)\}/g;
const forbiddenKeys = [
"email",
"firstName",
"lastName",
"phone",
"contact",
"response",
"responseBody",
"apiKey",
"API_KEY",
"api_key",
"api_secret",
];
const offenders: string[] = [];
let match: RegExpExecArray | null;
while ((match = pattern.exec(SOURCE)) !== null) {
const objContent = match[2];
for (const key of forbiddenKeys) {
const keyRe = new RegExp(`(^|[\\s,{])${key}\\s*:`, "m");
if (keyRe.test(objContent)) offenders.push(`key "${key}" in ${match[0].slice(0, 80)}...`);
}
}
expect(offenders, `crmLogger must not log PII / secret fields: ${offenders.join("; ")}`).toEqual([]);
});
it("uses createScopedLogger('crm')", () => {
expect(SOURCE).toMatch(/createScopedLogger\(\s*["']crm["']\s*\)/);
});
it("does not expose CRM template or entity details in configuration errors", () => {
expect(SOURCE).toContain("class CrmConfigurationError");
expect(SOURCE).not.toContain("CRM is not configured:");
expect(SOURCE).not.toContain('Entity "');
expect(SOURCE).not.toContain("load the template");
expect(SOURCE).not.toContain('must be "All" or');
expect(SOURCE).not.toContain("ConvertKit error:");
});
});
@@ -0,0 +1,550 @@
import { loadWebConfig } from "@toolctrl/config";
import { DefaultEntityTypes } from "~/application/dtos/shared/DefaultEntityTypes";
import { Colors } from "~/application/enums/shared/Colors";
import { RowRelationshipsApi } from "~/utils/api/.server/RowRelationshipsApi";
import { RowsApi } from "~/utils/api/.server/RowsApi";
import { dbRead } from "~/utils/db.server";
import { EntityWithDetails, findEntityByName, getEntitiesByName, getEntityByName } from "~/utils/db/entities/entities.db.server";
import { RowWithDetails, countRows, getRowById, getRows, getRowsInIds } from "~/utils/db/entities/rows.db.server";
import RowValueService, { RowValueUpdateDto } from "~/utils/helpers/.server/RowValueService";
import RowHelper from "~/utils/helpers/RowHelper";
import RowValueHelper from "~/utils/helpers/RowValueHelper";
import { t as i18nT } from "i18next";
import { createScopedLogger } from "~/utils/logger.server";
import { ensureProductCoreCrmEntities } from "./ProductCoreCrmEntities.server";
// EU-Sterilisation (R-P1a): ConvertKit (US-Newsletter-Provider) entfernt. Das CRM bleibt
// (Contacts/Companies/Opportunities/Submissions); Newsletter laufen ausschließlich über das
// eigene CRM. ConvertKit-Subscriber-Sync, -Fetch und -Config sind gestrichen.
const crmLogger = createScopedLogger("crm");
export class CrmConfigurationError extends Error {
readonly code = "CRM_CONFIGURATION_INVALID";
constructor() {
super("CRM_CONFIGURATION_INVALID");
this.name = "CrmConfigurationError";
}
}
export class CrmProviderError extends Error {
readonly code = "CRM_PROVIDER_UNAVAILABLE";
constructor() {
super("CRM_PROVIDER_UNAVAILABLE");
this.name = "CrmProviderError";
}
}
async function validate(tenantId: string | null = null) {
if (tenantId !== null) {
await ensureProductCoreCrmEntities();
}
const crmEntities = ["company", "opportunity", "contact"];
if (tenantId === null) {
crmEntities.push("submission");
}
const entities = await getEntitiesByName(crmEntities);
await Promise.all(
crmEntities.map(async (crmEntity) => {
const entity = entities.find((i) => i.name === crmEntity);
if (!entity) {
throw new CrmConfigurationError();
}
if (tenantId === null && ![DefaultEntityTypes.All, DefaultEntityTypes.AdminOnly].find((f) => f.toString() === entity.type)) {
throw new CrmConfigurationError();
} else if (tenantId !== null && ![DefaultEntityTypes.All, DefaultEntityTypes.AppOnly].find((f) => f.toString() === entity.type)) {
throw new CrmConfigurationError();
}
})
);
}
async function getContactsInRowIds(rowIds: string[], tenantId: string | null): Promise<ContactDto[]> {
const contactsEntity = await getEntityByName({ tenantId, name: "contact" });
const rows = await getRowsInIds(rowIds, tenantId);
return await Promise.all(
rowIds
.filter((f) => f)
.map(async (rowId) => {
const row = rows.find((i) => i.id === rowId);
if (!row) {
throw new Error("Contact not found: " + rowId);
}
const email = RowValueHelper.getText({ entity: contactsEntity, row, name: "email" }) ?? "";
const firstName = RowValueHelper.getText({ entity: contactsEntity, row, name: "firstName" }) ?? "";
const lastName = RowValueHelper.getText({ entity: contactsEntity, row, name: "lastName" }) ?? "";
const jobTitle = RowValueHelper.getText({ entity: contactsEntity, row, name: "jobTitle" }) ?? "";
const marketingSubscriber = RowValueHelper.getBoolean({ entity: contactsEntity, row, name: "marketingSubscriber" }) ?? false;
const contact: ContactDto = {
id: row.id,
email,
firstName,
lastName,
jobTitle,
marketingSubscriber,
};
return contact;
})
);
}
export type ContactDto = {
id: string;
email: string;
firstName: string;
lastName: string;
jobTitle: string;
marketingSubscriber: boolean;
company?: {
name: string;
};
};
async function getContact(rowId: string, tenantId: string | null): Promise<ContactDto | null> {
const contactsEntity = await getEntityByName({ tenantId, name: "contact" });
const row = await getRowById(rowId, { entityId: contactsEntity.id, tenantId });
if (!row) {
return null;
}
return rowToContact({ row, entity: contactsEntity });
}
async function getContacts(tenantId: string | null): Promise<ContactDto[]> {
const contactsEntity = await getEntityByName({ tenantId, name: "contact" });
const rows = await getRows({ entityName: "contact", tenantId });
return rows.map((row) => rowToContact({ row, entity: contactsEntity }));
}
export type ContactFormSettings = { crm: boolean; actionUrl?: string; error?: string };
async function getContactFormSettings() {
const settings: ContactFormSettings = {
crm: false,
actionUrl: undefined,
error: undefined,
};
try {
await validate();
settings.crm = true;
} catch {
const formspreeEndpoint = loadWebConfig().config.contactFormspreeEndpoint;
if (formspreeEndpoint) {
settings.actionUrl = formspreeEndpoint;
} else {
settings.error = i18nT("crm.contact.errors.formNotConfigured");
}
}
return settings;
}
export type NewsletterFormSettings = {
crm: boolean;
error?: string;
};
async function getNewsletterFormSettings() {
const settings: NewsletterFormSettings = {
crm: false,
error: undefined,
};
try {
await validate();
settings.crm = true;
} catch {
//
}
if (!settings.crm) {
settings.error = i18nT("crm.newsletter.errors.formNotConfigured");
}
return settings;
}
export type CrmSummaryDto = {
companies: number;
contacts: number;
opportunities: { value: number; count: number };
submissions: number;
data: {
openOpportunities: RowWithDetails[];
submissions: RowWithDetails[];
};
};
async function getSummary(tenantId: string | null): Promise<CrmSummaryDto> {
const openOpportunities = await getRows({
tenantId,
entityName: "opportunity",
});
const opportunities = { value: 0, count: 0 };
const opportunitiesEntity = await getEntityByName({ tenantId, name: "opportunity" });
openOpportunities.forEach((row) => {
const value = RowValueHelper.getNumber({ entity: opportunitiesEntity, row, name: "value" });
opportunities.value += value ?? 0;
if ((value ?? 0) > 0) {
opportunities.count++;
}
});
const submissionsEntity = await findEntityByName({ name: "submission", tenantId });
const submissions = submissionsEntity ? await getRows({ entityName: "submission", tenantId }) : [];
return {
companies: await countRows({ entityName: "company", tenantId }),
contacts: await countRows({ entityName: "contact", tenantId }),
opportunities,
submissions: submissions.length,
data: {
openOpportunities,
submissions,
},
};
}
async function createCompany({ tenantId, name, request }: { tenantId: string | null; name: string; request: Request }) {
const entity = await getEntityByName({ tenantId, name: "company" });
const rowValues = RowHelper.getRowPropertiesFromForm({
entity,
values: [{ name: "name", value: name }],
});
const row = await RowsApi.create({
entity,
tenantId,
rowValues,
request,
});
return row;
}
async function createContact({
tenantId,
firstName,
lastName,
email,
jobTitle,
status,
marketingSubscriber,
options,
request,
}: {
tenantId: string | null;
firstName: string;
lastName: string;
email: string;
jobTitle?: string;
status: string;
marketingSubscriber: boolean;
options?: {
checkUsage: boolean;
createLog: boolean;
createEvent: boolean;
storeMedia: boolean;
reportUsage: boolean;
};
request: Request;
}) {
const entity = await getEntityByName({ tenantId, name: "contact" });
const rowValues = RowHelper.getRowPropertiesFromForm({
entity,
values: [
{ name: "firstName", value: firstName },
{ name: "lastName", value: lastName },
{ name: "email", value: email },
{ name: "jobTitle", value: jobTitle ?? "" },
{ name: "status", value: status },
{ name: "marketingSubscriber", value: marketingSubscriber ? "true" : "false" },
],
options: {
skipValidation: true,
},
});
return await RowsApi.create({
entity,
tenantId,
rowValues,
options,
request,
});
}
async function createSubmission({ tenantId, users, message, request }: { tenantId: string | null; users: string; message: string; request: Request }) {
const entity = await getEntityByName({ tenantId, name: "submission" });
const rowValues = RowHelper.getRowPropertiesFromForm({
entity,
values: [
{ name: "users", value: users },
{ name: "message", value: message },
],
});
const row = await RowsApi.create({
entity,
tenantId,
rowValues,
request,
});
return row;
}
async function createContactSubmission(
submission: {
firstName: string;
lastName: string;
email: string;
jobTitle?: string;
company?: string;
users: string;
message: string;
},
request: Request
) {
let contact = await RowsApi.find({
entity: { name: "contact" },
tenantId: null,
properties: [{ name: "email", value: submission.email }],
});
if (!contact) {
contact = await createContact({
tenantId: null,
firstName: submission.firstName,
lastName: submission.lastName,
email: submission.email,
jobTitle: submission.jobTitle,
status: "contact",
marketingSubscriber: false,
request,
});
await RowsApi.addTag({ row: contact, tag: { value: "form", color: Colors.BLUE } });
}
const companyEntity = await getEntityByName({ tenantId: null, name: "company" });
const companies = contact.parentRows.filter((f) => f.parent.entityId === companyEntity.id);
if (companies.length === 0 && submission.company) {
const company = await createCompany({
tenantId: null,
name: submission.company,
request,
});
await RowRelationshipsApi.createRelationship({
parent: company,
child: contact,
tenantId: null,
});
}
const newSubmission = await createSubmission({
tenantId: null,
users: submission.users,
message: submission.message,
request,
});
await RowRelationshipsApi.createRelationship({
parent: contact,
child: newSubmission,
tenantId: null,
});
return contact;
}
async function updateContact(
rowId: string,
data: {
marketingSubscriber?: boolean;
firstName?: string;
lastName?: string;
jobTitle?: string;
},
options?: {
createEvent?: boolean;
tenantId?: string | null;
}
) {
const tenantId = options?.tenantId ?? null;
const contactsEntity = await getEntityByName({ tenantId, name: "contact" });
const row = await getRowById(rowId, { entityId: contactsEntity.id, tenantId });
if (!row) {
return null;
}
const contact = rowToContact({ row, entity: contactsEntity });
let changes: RowValueUpdateDto[] = [];
if (data.firstName !== undefined && data.firstName !== contact.firstName) {
changes.push({ name: "firstName", textValue: data.firstName });
} else if (data.lastName !== undefined && data.lastName !== contact.lastName) {
changes.push({ name: "lastName", textValue: data.lastName });
} else if (data.jobTitle !== undefined && data.jobTitle !== contact.jobTitle) {
changes.push({ name: "jobTitle", textValue: data.jobTitle });
} else if (data.marketingSubscriber !== undefined && data.marketingSubscriber !== contact.marketingSubscriber) {
changes.push({ name: "marketingSubscriber", booleanValue: data.marketingSubscriber });
}
if (changes.length === 0) {
return [];
}
await RowValueService.update({
entity: contactsEntity,
row,
values: changes,
session: { tenantId: row.tenantId },
checkPermissions: false,
options: {
createEvent: options?.createEvent,
},
});
return changes;
}
async function subscribeToNewsletter({
firstName,
lastName,
email,
source,
request,
}: {
firstName: string;
lastName: string;
email: string;
source?: string;
request: Request;
}) {
const settings = await getNewsletterFormSettings();
if (!settings.crm) {
return { error: i18nT("crm.newsletter.errors.integrationMissing") };
}
if (settings.crm) {
// Note: never log raw email/firstName/lastName here — those are PII.
crmLogger.info({ op: "newsletter.createContact", hasSource: Boolean(source) }, "Creating newsletter contact in CRM");
let contact = await RowsApi.find({
entity: { name: "contact" },
tenantId: null,
properties: [{ name: "email", value: email }],
});
if (!contact) {
contact = await createContact({
tenantId: null,
firstName,
lastName,
email: email,
jobTitle: "",
status: "contact",
marketingSubscriber: true,
request,
});
if (source) {
await RowsApi.addTag({ row: contact, tag: { value: source, color: Colors.ORANGE } });
} else {
await RowsApi.addTag({ row: contact, tag: { value: "newsletter", color: Colors.ORANGE } });
}
} else {
await updateContact(contact.id, { marketingSubscriber: true });
}
}
return { success: i18nT("crm.newsletter.subscribed") };
}
function rowToContact({ row, entity }: { row: RowWithDetails; entity: EntityWithDetails }) {
const email = RowValueHelper.getText({ entity, row, name: "email" }) ?? "";
const firstName = RowValueHelper.getText({ entity, row, name: "firstName" }) ?? "";
const lastName = RowValueHelper.getText({ entity, row, name: "lastName" }) ?? "";
const jobTitle = RowValueHelper.getText({ entity, row, name: "jobTitle" }) ?? "";
const marketingSubscriber = RowValueHelper.getBoolean({ entity, row, name: "marketingSubscriber" }) ?? false;
const companyRow = row.parentRows.find((f) => f.parent.entityId === entity.id)?.parent;
let company: { name: string } | undefined = undefined;
if (companyRow) {
company = {
name: RowValueHelper.getText({ entity, row: companyRow, name: "name" }) ?? "",
};
}
return {
id: row.id,
email,
firstName,
lastName,
jobTitle,
marketingSubscriber,
company,
};
}
export type UserInCrmDto = {
status: "synced" | "to-create" | "to-update";
email: string;
source: "user";
id: string;
firstName: string;
lastName: string;
isContact: boolean;
contact: ContactDto | null;
updatedAt: Date | null;
};
// EU-Sterilisation (R-P1a): invalidateCache wurde nur vom ConvertKit-Sync genutzt und bleibt als
// no-op-Parameter in der Signatur, damit Aufrufer (Admin-CRM-Sync) unverändert funktionieren.
async function getUsersInCrm(_options: { invalidateCache?: boolean } = {}): Promise<UserInCrmDto[]> {
const contacts = await getContacts(null);
let usersInCrm: UserInCrmDto[] = [];
usersInCrm.push(...(await loadFromUsers({ contacts })));
// filter by not contacts first, then by updated date desc
usersInCrm = usersInCrm.sort((a, b) => {
if (a.isContact && !b.isContact) {
return 1;
} else if (!a.isContact && b.isContact) {
return -1;
} else if (a.updatedAt && b.updatedAt) {
return b.updatedAt.getTime() - a.updatedAt.getTime();
} else if (a.updatedAt) {
return -1;
} else if (b.updatedAt) {
return 1;
}
return 0;
});
return usersInCrm;
}
async function loadFromUsers({ contacts }: { contacts: ContactDto[] }) {
let usersInCrm: UserInCrmDto[] = [];
// Read-only enumeration of users for CRM sync display — eventually-consistent
// is fine here. The sync action itself runs in a separate request that uses
// the write client.
const users = await dbRead.user.findMany({});
users.forEach((user) => {
const contact = contacts.find((f) => f.email === user.email) ?? null;
let status: "synced" | "to-create" | "to-update" = "synced";
if (!contact) {
status = "to-create";
} else if (contact.firstName !== user.firstName || contact.lastName !== user.lastName) {
status = "to-update";
}
const userInCrm: UserInCrmDto = {
status,
email: user.email,
source: "user",
id: user.id,
firstName: user.firstName,
lastName: user.lastName,
updatedAt: user.updatedAt,
isContact: !!contact,
contact,
};
usersInCrm.push(userInCrm);
});
return usersInCrm;
}
export default {
validate,
getContactsInRowIds,
getContact,
getContacts,
getContactFormSettings,
getNewsletterFormSettings,
getSummary,
createCompany,
createContact,
createSubmission,
createContactSubmission,
updateContact,
subscribeToNewsletter,
getUsersInCrm,
};
@@ -0,0 +1,280 @@
import { DefaultEntityTypes } from "~/application/dtos/shared/DefaultEntityTypes";
import { PropertyType } from "~/application/enums/entities/PropertyType";
import type { TemplateEntityPropertyTypeDto } from "~/modules/templates/EntityTemplateDto";
import { CRM_ENTITIES_TEMPLATE } from "~/modules/templates/defaultEntityTemplates";
import { db } from "~/utils/db.server";
import { createEntity } from "~/utils/db/entities/entities.db.server";
import { createProperties, type CreatePropertyDto } from "~/utils/db/entities/properties.db.server";
import { DefaultAdminRoles } from "~/application/dtos/shared/DefaultAdminRoles";
import { DefaultAppRoles } from "~/application/dtos/shared/DefaultAppRoles";
import { DefaultPermission } from "~/application/dtos/shared/DefaultPermissions";
import { getEntityPermissions } from "~/utils/helpers/PermissionsHelper";
export const productCoreCrmEntityNames = ["company", "contact", "opportunity"] as const;
export async function ensureProductCoreCrmEntities(options: { ensurePermissions?: boolean; ensureViews?: boolean } = {}) {
assertProductCoreCrmTemplateLanguage();
const ensurePermissions = options.ensurePermissions ?? false;
const ensureViews = options.ensureViews ?? false;
const existingEntities = await db.entity.findMany({
where: { name: { in: [...productCoreCrmEntityNames] } },
select: { id: true, name: true, type: true },
});
const existingNames = new Set(existingEntities.map((entity) => entity.name));
const missingNames = productCoreCrmEntityNames.filter((name) => !existingNames.has(name));
if (missingNames.length > 0) {
for (const entityTemplate of CRM_ENTITIES_TEMPLATE.entities.filter((entity) => missingNames.includes(entity.name as (typeof productCoreCrmEntityNames)[number]))) {
const entity = await createEntity({
name: entityTemplate.name,
slug: entityTemplate.slug,
prefix: entityTemplate.prefix,
title: entityTemplate.title,
titlePlural: entityTemplate.titlePlural,
isAutogenerated: entityTemplate.isAutogenerated ?? true,
hasApi: entityTemplate.hasApi ?? true,
icon: entityTemplate.icon ?? "",
active: entityTemplate.active ?? true,
type: DefaultEntityTypes.All,
showInSidebar: false,
hasTags: entityTemplate.hasTags ?? true,
hasComments: entityTemplate.hasComments ?? true,
hasTasks: entityTemplate.hasTasks ?? false,
hasActivity: entityTemplate.hasActivity ?? true,
hasBulkDelete: entityTemplate.hasBulkDelete ?? false,
hasViews: entityTemplate.hasViews ?? true,
defaultVisibility: entityTemplate.defaultVisibility ?? "tenant",
onCreated: entityTemplate.onCreated ?? "redirectToOverview",
onEdit: entityTemplate.onEdit ?? "editRoute",
});
await createProperties(
entity.id,
entityTemplate.properties.map((property): CreatePropertyDto => ({
name: property.name,
title: property.title,
type: toProductCorePropertyType(property.type),
subtype: property.subtype ?? null,
isRequired: property.isRequired,
isDisplay: property.isDisplay,
isReadOnly: property.isReadOnly,
canUpdate: property.canUpdate,
showInCreate: property.showInCreate,
tenantId: property.tenantId ?? null,
options: property.options?.map((option, index) => ({
order: index + 1,
value: option.value,
name: option.name,
color: option.color,
})),
attributes: property.attributes,
}))
);
}
}
await db.entity.updateMany({
where: { name: { in: [...productCoreCrmEntityNames] }, type: DefaultEntityTypes.AdminOnly },
data: {
type: DefaultEntityTypes.All,
active: true,
showInSidebar: false,
},
});
if (ensureViews) {
await ensureProductCoreCrmViewsAndRelationships();
}
if (ensurePermissions) {
await ensureProductCoreCrmPermissions();
}
}
function assertProductCoreCrmTemplateLanguage() {
if (CRM_ENTITIES_TEMPLATE.defaultLanguage !== "en" || !CRM_ENTITIES_TEMPLATE.languages.includes("en")) {
throw new Error("Product core CRM defaults are language-scoped English seed data.");
}
}
async function ensureProductCoreCrmViewsAndRelationships() {
const entities = await db.entity.findMany({
where: { name: { in: [...productCoreCrmEntityNames] }, active: true },
include: {
properties: true,
views: {
where: { tenantId: null, userId: null },
include: { properties: true },
orderBy: { order: "asc" },
},
},
});
const entityByName = new Map(entities.map((entity) => [entity.name, entity]));
for (const entity of entities) {
const entityTemplate = CRM_ENTITIES_TEMPLATE.entities.find((template) => template.name === entity.name);
if (!entityTemplate) {
continue;
}
const existingViewNames = new Set(entity.views.map((view) => view.name));
const hasDefaultView = entity.views.some((view) => view.isDefault);
const viewTemplates =
entityTemplate.views && entityTemplate.views.length > 0
? entityTemplate.views
: [
{
layout: "table" as const,
name: "all",
title: entity.titlePlural,
isDefault: true,
properties: getDefaultViewPropertyNames(entityTemplate),
},
];
let nextOrder = entity.views.length + 1;
for (const [index, viewTemplate] of viewTemplates.entries()) {
if (existingViewNames.has(viewTemplate.name)) {
continue;
}
const groupByPropertyId = viewTemplate.groupByProperty
? (entity.properties.find((property) => property.name === viewTemplate.groupByProperty)?.id ?? null)
: null;
const view = await db.entityView.create({
data: {
entityId: entity.id,
tenantId: null,
userId: null,
createdByUserId: null,
layout: viewTemplate.layout,
order: viewTemplate.order ?? nextOrder++,
name: viewTemplate.name,
title: viewTemplate.title,
isDefault: viewTemplate.isDefault ?? (!hasDefaultView && index === 0),
isSystem: viewTemplate.isSystem ?? false,
pageSize: viewTemplate.pageSize ?? 100,
groupByPropertyId,
},
});
await Promise.all(
viewTemplate.properties.map((propertyName, propertyIndex) =>
db.entityViewProperty.create({
data: {
entityViewId: view.id,
propertyId: entity.properties.find((property) => property.name === propertyName)?.id ?? null,
name: propertyName,
order: propertyIndex + 1,
},
})
)
);
await Promise.all(
(viewTemplate.filters ?? []).map((filter) =>
db.entityViewFilter.create({
data: {
entityViewId: view.id,
name: filter.name,
condition: filter.condition,
value: filter.value,
match: filter.match ?? "and",
},
})
)
);
await Promise.all(
(viewTemplate.sort ?? []).map((sort, sortIndex) =>
db.entityViewSort.create({
data: {
entityViewId: view.id,
name: sort.name,
asc: sort.asc,
order: sortIndex + 1,
},
})
)
);
}
}
for (const relationship of CRM_ENTITIES_TEMPLATE.relationships) {
const parent = entityByName.get(relationship.parent);
const child = entityByName.get(relationship.child);
if (!parent || !child) {
continue;
}
const existing = await db.entityRelationship.findFirst({
where: { parentId: parent.id, childId: child.id, title: relationship.title ?? null },
select: { id: true },
});
if (existing) {
continue;
}
await db.entityRelationship.create({
data: {
parentId: parent.id,
childId: child.id,
order: relationship.order,
title: relationship.title ?? null,
type: relationship.type,
required: relationship.required,
cascade: relationship.cascade ?? false,
readOnly: relationship.readOnly ?? false,
hiddenIfEmpty: relationship.hiddenIfEmpty ?? false,
},
});
}
}
function getDefaultViewPropertyNames(entityTemplate: (typeof CRM_ENTITIES_TEMPLATE.entities)[number]) {
const displayProperties = entityTemplate.properties.filter((property) => property.isDisplay).map((property) => property.name);
if (displayProperties.length > 0) {
return displayProperties;
}
return entityTemplate.properties.slice(0, 4).map((property) => property.name);
}
async function ensureProductCoreCrmPermissions() {
const { createPermissions } = await import("~/utils/db/permissions/permissions.db.server");
const entities = await db.entity.findMany({
where: { name: { in: [...productCoreCrmEntityNames] }, active: true },
});
let order = 0;
for (const entity of entities) {
const entityPermissions = getEntityPermissions(entity);
await createPermissions(
entityPermissions.map((permission) => ({
inRoles: [DefaultAdminRoles.SuperAdmin, DefaultAppRoles.SuperUser, DefaultAppRoles.Admin, DefaultAppRoles.User].map((role) => role.toString()),
name: permission.name as DefaultPermission,
description: permission.description,
type: entity.type,
})),
order
);
order += entityPermissions.length;
}
}
function toProductCorePropertyType(type: TemplateEntityPropertyTypeDto): PropertyType {
switch (type) {
case "string":
return PropertyType.TEXT;
case "number":
return PropertyType.NUMBER;
case "boolean":
return PropertyType.BOOLEAN;
case "date":
return PropertyType.DATE;
case "media":
return PropertyType.MEDIA;
case "select":
return PropertyType.SELECT;
case "multiSelect":
return PropertyType.MULTI_SELECT;
case "multiText":
return PropertyType.MULTI_TEXT;
case "rangeNumber":
return PropertyType.RANGE_NUMBER;
case "rangeDate":
return PropertyType.RANGE_DATE;
case "formula":
return PropertyType.FORMULA;
}
}
@@ -0,0 +1,163 @@
/**
* CRM-contacts public search — hot-path migration to Meilisearch.
*
* Wraps `searchWithFallback({ index: 'crm-contacts', tenantId, query,
* fallback })` so consumers of the contact list / contact filter can
* adopt the typed client without further refactors. When MEILI_URL is
* unset, or SEARCH_BACKEND=sql is forced, or Meilisearch raises a
* connectivity error, the helper transparently falls back to a
* SQL ILIKE-against-RowValue scan over the contact entity.
*
* Contacts are stored as rows with `entityName = "contact"`; the SQL
* fallback therefore reuses the legacy row-search code-path so the
* shape returned from both backends is the same set of `Row` IDs that
* the caller can hydrate via the existing rows.db.server helpers.
*/
import {
createSearchClient,
crmContactsIndex,
searchWithFallback,
type SearchClient,
type SearchFallback,
type SearchHits,
} from "@toolctrl/search";
import { db } from "~/utils/db.server";
let cachedClient: SearchClient | undefined;
let cachedUrl: string | undefined;
function getSearchClient(): SearchClient | undefined {
const url = process.env.MEILI_URL;
if (!url || process.env.SEARCH_BACKEND === "sql") return undefined;
if (cachedClient && cachedUrl === url) return cachedClient;
cachedClient = createSearchClient({ url, apiKey: process.env.MEILI_API_KEY ?? undefined });
cachedUrl = url;
return cachedClient;
}
export interface SearchCrmContactsInput {
/** Tenant scope — REQUIRED for the Meilisearch path. The SQL
* fallback also scopes by tenantId. */
readonly tenantId: string;
/** User-supplied query string. Empty string returns the first page. */
readonly query: string;
/** Optional cap on result count. Defaults to 50. */
readonly limit?: number;
}
export interface CrmContactSearchHit {
readonly id: string;
readonly firstName: string;
readonly lastName: string;
readonly email: string;
readonly company: string;
readonly jobTitle: string;
}
export interface CrmContactSearchResult {
readonly hits: readonly CrmContactSearchHit[];
readonly total: number;
readonly fellBack: boolean;
}
/**
* SQL fallback — uses RowValue text-search joined to the `contact`
* entity. Returns the same hit-shape Meilisearch returns so the caller
* doesn't have to branch.
*/
async function sqlFallback(input: SearchCrmContactsInput): Promise<SearchHits> {
const limit = input.limit ?? 50;
const q = input.query.trim();
const where: Record<string, unknown> = {
tenantId: input.tenantId,
deletedAt: null,
entity: { name: "contact" },
};
if (q.length > 0) {
where.values = {
some: {
textValue: { contains: q, mode: "insensitive" },
},
};
}
const rows = await db.row.findMany({
where: where as never,
take: limit,
orderBy: [{ createdAt: "desc" }],
select: {
id: true,
values: {
select: {
textValue: true,
property: { select: { name: true } },
},
},
},
});
return {
hits: rows.map((row) => {
const get = (name: string): string => {
const v = row.values.find((vv) => vv.property?.name === name);
return v?.textValue ?? "";
};
return {
document: {
id: row.id,
tenantId: input.tenantId,
firstName: get("firstName"),
lastName: get("lastName"),
email: get("email"),
company: get("company"),
jobTitle: get("jobTitle"),
} as Record<string, unknown> & { id: string; tenantId: string },
};
}),
estimatedTotalHits: rows.length,
processingTimeMs: 0,
};
}
export async function searchCrmContacts(input: SearchCrmContactsInput): Promise<CrmContactSearchResult> {
const fallback: SearchFallback = async () => sqlFallback(input);
const client = getSearchClient();
if (!client) {
const sql = await sqlFallback(input);
return {
hits: sql.hits.map((hit) => toContactHit(hit.document)),
total: sql.estimatedTotalHits ?? sql.hits.length,
fellBack: true,
};
}
let fellBack = false;
const hits = await searchWithFallback({
client,
schema: crmContactsIndex,
tenantId: input.tenantId,
query: input.query,
fallback,
onFallback: () => {
fellBack = true;
},
options: { limit: input.limit ?? 50 },
});
return {
hits: hits.hits.map((hit) => toContactHit(hit.document)),
total: hits.estimatedTotalHits ?? hits.hits.length,
fellBack,
};
}
function toContactHit(document: Record<string, unknown>): CrmContactSearchHit {
return {
id: String(document.id ?? ""),
firstName: typeof document.firstName === "string" ? document.firstName : "",
lastName: typeof document.lastName === "string" ? document.lastName : "",
email: typeof document.email === "string" ? document.email : "",
company: typeof document.company === "string" ? document.company : "",
jobTitle: typeof document.jobTitle === "string" ? document.jobTitle : "",
};
}
@@ -0,0 +1,112 @@
/**
* Unit tests for searchCrmContacts — the public CRM-contacts search
* entry-point. These tests cover the SQL-fallback path (when MEILI_URL
* is unset) so the legacy SQL contract is exercised without requiring
* a live Meilisearch container.
*/
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("~/utils/db.server", () => ({
db: {
row: {
findMany: vi.fn(),
},
},
}));
import { db } from "~/utils/db.server";
import { searchCrmContacts } from "./crmContactSearch.server";
const dbMock = db as unknown as {
row: {
findMany: ReturnType<typeof vi.fn>;
};
};
describe("searchCrmContacts (sql fallback)", () => {
const originalMeili = process.env.MEILI_URL;
const originalBackend = process.env.SEARCH_BACKEND;
beforeEach(() => {
delete process.env.MEILI_URL;
delete process.env.SEARCH_BACKEND;
dbMock.row.findMany.mockReset();
});
afterEachRestore(originalMeili, originalBackend);
it("scopes by tenant + entity=contact and maps row values to contact-hit fields", async () => {
dbMock.row.findMany.mockResolvedValueOnce([
{
id: "row-1",
values: [
{ textValue: "Alice", property: { name: "firstName" } },
{ textValue: "Anderson", property: { name: "lastName" } },
{ textValue: "alice@example.com", property: { name: "email" } },
{ textValue: "Acme", property: { name: "company" } },
{ textValue: "CTO", property: { name: "jobTitle" } },
],
},
]);
const result = await searchCrmContacts({ tenantId: "tenant-1", query: "alice" });
expect(dbMock.row.findMany).toHaveBeenCalledTimes(1);
const where = dbMock.row.findMany.mock.calls[0][0].where as Record<string, unknown>;
expect(where.tenantId).toBe("tenant-1");
expect((where.entity as { name: string }).name).toBe("contact");
expect(where.deletedAt).toBeNull();
expect(where.values).toBeDefined();
expect(result.fellBack).toBe(true);
expect(result.hits).toHaveLength(1);
expect(result.hits[0]).toEqual({
id: "row-1",
firstName: "Alice",
lastName: "Anderson",
email: "alice@example.com",
company: "Acme",
jobTitle: "CTO",
});
});
it("omits the value-text predicate when the query is empty", async () => {
dbMock.row.findMany.mockResolvedValueOnce([]);
await searchCrmContacts({ tenantId: "tenant-1", query: "" });
const where = dbMock.row.findMany.mock.calls[0][0].where as Record<string, unknown>;
expect(where.values).toBeUndefined();
});
it("honours the limit option", async () => {
dbMock.row.findMany.mockResolvedValueOnce([]);
await searchCrmContacts({ tenantId: "tenant-1", query: "x", limit: 5 });
expect(dbMock.row.findMany.mock.calls[0][0].take).toBe(5);
});
it("returns total = number of fallback hits when Meilisearch is unavailable", async () => {
dbMock.row.findMany.mockResolvedValueOnce([
{ id: "r1", values: [] },
{ id: "r2", values: [] },
{ id: "r3", values: [] },
]);
const result = await searchCrmContacts({ tenantId: "tenant-1", query: "" });
expect(result.total).toBe(3);
expect(result.hits).toHaveLength(3);
});
});
function afterEachRestore(originalMeili: string | undefined, originalBackend: string | undefined) {
// restore env vars after the file completes (vitest doesn't gather them
// via beforeEach, so we centralize the cleanup here)
if (typeof afterAll === "function") {
afterAll(() => {
if (originalMeili === undefined) delete process.env.MEILI_URL;
else process.env.MEILI_URL = originalMeili;
if (originalBackend === undefined) delete process.env.SEARCH_BACKEND;
else process.env.SEARCH_BACKEND = originalBackend;
});
}
}