({ chatId, newName }: { chatId: string, newName: string })
| 254 | * The new chat will be owned by the current user (authenticated or anonymous). |
| 255 | */ |
| 256 | export const duplicateChat = async ({ chatId, newName }: { chatId: string, newName: string }) => sew(() => |
| 257 | withOptionalAuth(async ({ org, user, prisma }) => { |
| 258 | const askError = await checkAskEntitlement(); |
| 259 | if (askError) { |
| 260 | return askError; |
| 261 | } |
| 262 | |
| 263 | const originalChat = await prisma.chat.findUnique({ |
| 264 | where: { |
| 265 | id: chatId, |
| 266 | orgId: org.id, |
| 267 | }, |
| 268 | }); |
| 269 | |
| 270 | if (!originalChat) { |
| 271 | return notFound(); |
| 272 | } |
| 273 | |
| 274 | // Check if user can access the chat (owner, shared, or public) |
| 275 | const isOwner = await isOwnerOfChat(originalChat, user); |
| 276 | const isSharedWithUser = await isChatSharedWithUser({ prisma, chatId, userId: user?.id }); |
| 277 | if (originalChat.visibility === ChatVisibility.PRIVATE && !isOwner && !isSharedWithUser) { |
| 278 | return notFound(); |
| 279 | } |
| 280 | |
| 281 | const isGuestUser = user === undefined; |
| 282 | const anonymousCreatorId = isGuestUser ? await getOrCreateAnonymousId() : undefined; |
| 283 | |
| 284 | const newChat = await prisma.chat.create({ |
| 285 | data: { |
| 286 | orgId: org.id, |
| 287 | name: newName, |
| 288 | messages: originalChat.messages as unknown as Prisma.InputJsonValue, |
| 289 | createdById: user?.id, |
| 290 | anonymousCreatorId, |
| 291 | visibility: isGuestUser ? ChatVisibility.PUBLIC : ChatVisibility.PRIVATE, |
| 292 | }, |
| 293 | }); |
| 294 | |
| 295 | return { |
| 296 | id: newChat.id, |
| 297 | }; |
| 298 | }) |
| 299 | ); |
| 300 | |
| 301 | /** |
| 302 | * Returns the users that have been explicitly shared access to a chat. |
no test coverage detected