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
+115
@@ -0,0 +1,115 @@
|
||||
import { publishAuditEventViaOutbox } from "@hivegrid/db/audit-events.server";
|
||||
import type { OutboxPublisherTransaction } from "@hivegrid/db/outbox";
|
||||
import { CatalogError, foundationErrors, withTenantScope } from "@hivegrid/foundation";
|
||||
|
||||
import { chatErrors } from "./errors.js";
|
||||
import { CHAT_PERMISSIONS } from "./permissions.js";
|
||||
import { runChatRepositoryTransaction } from "./repository.server.js";
|
||||
import type { ChatNotificationPreference, ChatRouteRepositoryContext } from "./types.js";
|
||||
|
||||
const NOTIFICATION_PREFERENCES = new Set<ChatNotificationPreference>(["all_messages", "mentions_only", "muted"]);
|
||||
|
||||
export type SetChatMemberPreferencesInput = ChatRouteRepositoryContext &
|
||||
Readonly<{
|
||||
chatId: string;
|
||||
requestId: string;
|
||||
isPinned?: boolean;
|
||||
notificationPreference?: ChatNotificationPreference;
|
||||
}>;
|
||||
|
||||
export type ChatMemberPreferencesResult = Readonly<{
|
||||
chatId: string;
|
||||
isPinned: boolean;
|
||||
notificationPreference: ChatNotificationPreference;
|
||||
changed: boolean;
|
||||
}>;
|
||||
|
||||
/** Updates only the current member's personal channel preferences. The
|
||||
* repository boundary refreshes permissions and binds tenant/user RLS inside
|
||||
* the same transaction as the read and write. */
|
||||
export async function setChatMemberPreferences(input: SetChatMemberPreferencesInput): Promise<ChatMemberPreferencesResult> {
|
||||
const chatId = normalizeIdentifier(input.chatId);
|
||||
const requestId = normalizeIdentifier(input.requestId);
|
||||
const isPinned = normalizeOptionalBoolean(input.isPinned);
|
||||
const notificationPreference = normalizeOptionalNotificationPreference(input.notificationPreference);
|
||||
if (isPinned === undefined && notificationPreference === undefined) {
|
||||
throw new CatalogError(chatErrors.invalidInput);
|
||||
}
|
||||
|
||||
return runChatRepositoryTransaction(input, CHAT_PERMISSIONS.readOwnChannels, async (tx) => {
|
||||
const current = await tx.teamChatMember.findFirst({
|
||||
where: withTenantScope(input.tenantId, {
|
||||
chatId,
|
||||
userId: input.userId,
|
||||
leftAt: null,
|
||||
chat: { tenantId: input.tenantId },
|
||||
}),
|
||||
select: { id: true, isPinned: true, notificationPref: true },
|
||||
});
|
||||
if (!current) throw new CatalogError(foundationErrors.notFound, { chat: "not-visible" });
|
||||
|
||||
const nextPinned = isPinned ?? current.isPinned;
|
||||
const nextNotificationPreference = notificationPreference ?? current.notificationPref;
|
||||
const changed = nextPinned !== current.isPinned || nextNotificationPreference !== current.notificationPref;
|
||||
if (changed) {
|
||||
const updated = await tx.teamChatMember.updateMany({
|
||||
where: withTenantScope(input.tenantId, {
|
||||
id: current.id,
|
||||
chatId,
|
||||
userId: input.userId,
|
||||
leftAt: null,
|
||||
}),
|
||||
data: {
|
||||
isPinned: nextPinned,
|
||||
notificationPref: nextNotificationPreference,
|
||||
},
|
||||
});
|
||||
if (updated.count !== 1) throw new CatalogError(chatErrors.integrityFailure);
|
||||
await publishAuditEventViaOutbox(tx as unknown as OutboxPublisherTransaction, {
|
||||
tenantId: input.tenantId,
|
||||
actorId: input.userId,
|
||||
actorType: "user",
|
||||
action: "chat.member.preferences_updated",
|
||||
targetType: "TeamChat",
|
||||
targetId: chatId,
|
||||
requestId,
|
||||
sourceModule: "chat",
|
||||
outcome: "success",
|
||||
dataSensitivity: "internal",
|
||||
metadata: {
|
||||
isPinned: nextPinned,
|
||||
notificationPreference: nextNotificationPreference,
|
||||
},
|
||||
summaryText: "Persönliche Chat-Einstellungen wurden aktualisiert.",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
chatId,
|
||||
isPinned: nextPinned,
|
||||
notificationPreference: nextNotificationPreference,
|
||||
changed,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeIdentifier(value: unknown): string {
|
||||
if (typeof value !== "string") throw new CatalogError(chatErrors.invalidInput);
|
||||
const normalized = value.trim();
|
||||
if (!normalized || normalized.length > 191 || normalized.includes("\0")) throw new CatalogError(chatErrors.invalidInput);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function normalizeOptionalBoolean(value: unknown): boolean | undefined {
|
||||
if (value === undefined) return undefined;
|
||||
if (typeof value !== "boolean") throw new CatalogError(chatErrors.invalidInput);
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizeOptionalNotificationPreference(value: unknown): ChatNotificationPreference | undefined {
|
||||
if (value === undefined) return undefined;
|
||||
if (typeof value !== "string" || !NOTIFICATION_PREFERENCES.has(value as ChatNotificationPreference)) {
|
||||
throw new CatalogError(chatErrors.invalidInput);
|
||||
}
|
||||
return value as ChatNotificationPreference;
|
||||
}
|
||||
Reference in new issue
Block a user