import { getUserUuid } from '@/utils/getUserUuid' import { getBrowserUuid } from '@/utils/getBrowserUuid' import { userChatSettingsDbRemote } from '@/api/dbs' import type { UserChatSettingsDocument } from '@hereconnect/types' export const getUserChatSettingsDocument = ({ chat, qr_code_uri, user_chat_role, }: { chat: string qr_code_uri: string user_chat_role: UserChatSettingsDocument['user_chat_role'] }) => { return userChatSettingsDbRemote.get( `user_chat_settings:${qr_code_uri}:${chat}:${user_chat_role}:${getUserUuid()}`, ) } export const saveUserChatSettings = async ({ chat, qr_code_uri, user_chat_role, is_push_notification_enabled, }: { chat: string qr_code_uri: string user_chat_role: UserChatSettingsDocument['user_chat_role'] is_push_notification_enabled: boolean }) => { try { const user_uuid = getUserUuid() const browser_uuid = getBrowserUuid() const _id = `user_chat_settings:${qr_code_uri}:${chat}:${user_chat_role}:${user_uuid}` const result = await userChatSettingsDbRemote.upsert( _id, (doc): UserChatSettingsDocument | false => { const isNotChanged = doc.is_push_notification_enabled === is_push_notification_enabled if (isNotChanged) { return false } const updated_at = new Date().toISOString() return Object.assign(doc, { _id, type: 'user_chat_settings', qr_code_uri, browser_uuid, user_uuid, user_chat_role, chat, is_push_notification_enabled, created_at: doc.created_at ?? updated_at, updated_at, } satisfies UserChatSettingsDocument) }, ) console.log('Настройки успешно сохранены', result) } catch (error) { console.error('Ошибка сохранения настроек:', error) throw error } }