({ chatId, userIds }: { chatId: string, userIds: string[] })
| 346 | * Shares the chat with a list of users. |
| 347 | */ |
| 348 | export const shareChatWithUsers = async ({ chatId, userIds }: { chatId: string, userIds: string[] }) => sew(() => |
| 349 | withAuth(async ({ org, user, prisma }) => { |
| 350 | const askError = await checkAskEntitlement(); |
| 351 | if (askError) { |
| 352 | return askError; |
| 353 | } |
| 354 | |
| 355 | const chat = await prisma.chat.findUnique({ |
| 356 | where: { |
| 357 | id: chatId, |
| 358 | orgId: org.id, |
| 359 | }, |
| 360 | }); |
| 361 | |
| 362 | if (!chat) { |
| 363 | return notFound(); |
| 364 | } |
| 365 | |
| 366 | // Only the creator can share |
| 367 | if (chat.createdById !== user.id) { |
| 368 | return notFound(); |
| 369 | } |
| 370 | |
| 371 | |
| 372 | const memberships = await prisma.userToOrg.findMany({ |
| 373 | where: { |
| 374 | orgId: org.id, |
| 375 | userId: { |
| 376 | in: userIds, |
| 377 | }, |
| 378 | }, |
| 379 | }); |
| 380 | |
| 381 | if (memberships.length !== userIds.length) { |
| 382 | return notFound(); |
| 383 | } |
| 384 | |
| 385 | await prisma.chatAccess.createMany({ |
| 386 | data: userIds.map((userId) => ({ |
| 387 | chatId, |
| 388 | userId, |
| 389 | })), |
| 390 | skipDuplicates: true, |
| 391 | }); |
| 392 | |
| 393 | await createAudit({ |
| 394 | action: "chat.shared_with_users", |
| 395 | actor: { id: user.id, type: "user" }, |
| 396 | target: { id: chatId, type: "chat" }, |
| 397 | orgId: org.id, |
| 398 | metadata: { message: userIds.join(", ") }, |
| 399 | }); |
| 400 | |
| 401 | return { success: true }; |
| 402 | }) |
| 403 | ); |
| 404 | |
| 405 | /** |
no test coverage detected