Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,98 @@
|
||||
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
||||
import { CatalogError, withTenantScope } from "@hivegrid/foundation";
|
||||
|
||||
import { chatErrors } from "./errors.js";
|
||||
import { CHAT_PERMISSIONS } from "./permissions.js";
|
||||
import { runChatRepositoryTransaction } from "./repository.server.js";
|
||||
import type { ChatRouteRepositoryContext } from "./types.js";
|
||||
|
||||
const DEFAULT_DISCOVER_LIMIT = 25;
|
||||
const MAX_DISCOVER_LIMIT = 50;
|
||||
const MAX_DISCOVER_QUERY_LENGTH = 200;
|
||||
|
||||
export type DiscoverPublicGroupChatsInput = ChatRouteRepositoryContext & {
|
||||
readonly query?: string;
|
||||
readonly limit?: number;
|
||||
};
|
||||
|
||||
export type DiscoverablePublicGroupChat = {
|
||||
readonly id: string;
|
||||
readonly name: string;
|
||||
readonly description: string | null;
|
||||
readonly memberCount: number;
|
||||
};
|
||||
|
||||
export type DiscoverPublicGroupChatsResult = {
|
||||
readonly channels: readonly DiscoverablePublicGroupChat[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Lists active public group channels that the current tenant user has not yet
|
||||
* joined. The shared transaction boundary binds RLS and re-checks tenant,
|
||||
* module, and permission access in the same transaction as this read.
|
||||
*/
|
||||
export async function listDiscoverablePublicGroupChats(
|
||||
input: DiscoverPublicGroupChatsInput
|
||||
): Promise<DiscoverPublicGroupChatsResult> {
|
||||
const normalized = normalizeInput(input);
|
||||
|
||||
return runChatRepositoryTransaction(input, CHAT_PERMISSIONS.readOwnChannels, async (tx) => {
|
||||
const activeViewerMembership = withTenantScope(input.tenantId, {
|
||||
userId: input.userId,
|
||||
leftAt: null,
|
||||
});
|
||||
const activeMember = withTenantScope(input.tenantId, { leftAt: null });
|
||||
const textFilter: Prisma.TeamChatWhereInput = normalized.query
|
||||
? {
|
||||
OR: [
|
||||
{ name: { contains: normalized.query, mode: "insensitive" } },
|
||||
{ description: { contains: normalized.query, mode: "insensitive" } },
|
||||
],
|
||||
}
|
||||
: {};
|
||||
const rows = await tx.teamChat.findMany({
|
||||
where: withTenantScope(input.tenantId, {
|
||||
kind: "group" as const,
|
||||
isPrivate: false,
|
||||
archivedAt: null,
|
||||
name: { not: null },
|
||||
members: { none: activeViewerMembership },
|
||||
...textFilter,
|
||||
}),
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
_count: { select: { members: { where: activeMember } } },
|
||||
},
|
||||
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||
take: normalized.limit,
|
||||
});
|
||||
|
||||
return {
|
||||
channels: rows.map((row) => {
|
||||
const name = row.name?.trim();
|
||||
if (!name) throw new CatalogError(chatErrors.integrityFailure);
|
||||
return {
|
||||
id: row.id,
|
||||
name,
|
||||
description: row.description?.trim() || null,
|
||||
memberCount: Math.max(0, row._count.members),
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeInput(input: Pick<DiscoverPublicGroupChatsInput, "query" | "limit">): {
|
||||
readonly query: string;
|
||||
readonly limit: number;
|
||||
} {
|
||||
const query = input.query?.trim() ?? "";
|
||||
if (query.length > MAX_DISCOVER_QUERY_LENGTH) throw new CatalogError(chatErrors.invalidInput);
|
||||
const limit = input.limit ?? DEFAULT_DISCOVER_LIMIT;
|
||||
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_DISCOVER_LIMIT) {
|
||||
throw new CatalogError(chatErrors.invalidInput);
|
||||
}
|
||||
return { query, limit };
|
||||
}
|
||||
Reference in new issue
Block a user