82 lines
2.9 KiB
TypeScript
82 lines
2.9 KiB
TypeScript
import type { DbTransactionClient } from "@hivegrid/db/transactions.server";
|
|
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
|
import { withTenantScope } from "@hivegrid/foundation";
|
|
|
|
import { CHAT_PERMISSIONS } from "./permissions.js";
|
|
import { runChatRepositoryTransaction } from "./repository.server.js";
|
|
import type { ChatRouteRepositoryContext } from "./types.js";
|
|
|
|
export type FindMemberProjectChatLinkInput = ChatRouteRepositoryContext & {
|
|
readonly projectId: string;
|
|
};
|
|
|
|
export type MemberProjectChatLink = Readonly<{
|
|
chatId: string;
|
|
archivedAtIso: string | null;
|
|
canWrite: boolean;
|
|
}>;
|
|
|
|
const PROJECT_CHAT_LINK_SELECT = {
|
|
id: true,
|
|
archivedAt: true,
|
|
members: {
|
|
select: { writeRevokedAt: true },
|
|
},
|
|
} as const satisfies Prisma.TeamChatSelect;
|
|
|
|
/** Returns only a project chat the current member may already read. */
|
|
export async function findMemberProjectChatLink(input: FindMemberProjectChatLinkInput): Promise<MemberProjectChatLink | null> {
|
|
const projectId = normalizeIdentifier(input.projectId);
|
|
return runChatRepositoryTransaction(input, CHAT_PERMISSIONS.readOwnChannels, async (tx) => {
|
|
const active = await findProjectChat(tx, input, projectId, false);
|
|
if (active) return mapProjectChatLink(active);
|
|
const archived = await findProjectChat(tx, input, projectId, true);
|
|
return archived ? mapProjectChatLink(archived) : null;
|
|
});
|
|
}
|
|
|
|
type ProjectChatLinkRow = Readonly<{
|
|
id: string;
|
|
archivedAt: Date | null;
|
|
members: readonly Readonly<{ writeRevokedAt: Date | null }>[];
|
|
}>;
|
|
|
|
async function findProjectChat(
|
|
tx: DbTransactionClient,
|
|
context: ChatRouteRepositoryContext,
|
|
projectId: string,
|
|
archived: boolean
|
|
): Promise<ProjectChatLinkRow | null> {
|
|
const memberScope = withTenantScope(context.tenantId, { userId: context.userId, leftAt: null });
|
|
return tx.teamChat.findFirst({
|
|
where: withTenantScope(context.tenantId, {
|
|
kind: "project" as const,
|
|
projektId: projectId,
|
|
archivedAt: archived ? { not: null } : null,
|
|
members: { some: memberScope },
|
|
}),
|
|
orderBy: [{ createdAt: "desc" }, { id: "desc" }],
|
|
select: {
|
|
...PROJECT_CHAT_LINK_SELECT,
|
|
members: { ...PROJECT_CHAT_LINK_SELECT.members, where: memberScope, take: 1 },
|
|
},
|
|
});
|
|
}
|
|
|
|
function mapProjectChatLink(row: ProjectChatLinkRow): MemberProjectChatLink {
|
|
const membership = row.members[0];
|
|
if (!membership) throw new Error("Project chat link lost its tenant-scoped membership");
|
|
return {
|
|
chatId: row.id,
|
|
archivedAtIso: row.archivedAt?.toISOString() ?? null,
|
|
canWrite: row.archivedAt === null && membership.writeRevokedAt === null,
|
|
};
|
|
}
|
|
|
|
function normalizeIdentifier(value: string): string {
|
|
if (typeof value !== "string") throw new Error("projectId must be a string");
|
|
const normalized = value.trim();
|
|
if (!normalized || normalized.length > 500 || normalized.includes("\0")) throw new Error("projectId is invalid");
|
|
return normalized;
|
|
}
|