conversation.repository.ts92 lines, 3.1 KB
| 1 | import { randomUUID } from 'node:crypto'; |
| 2 | import { ObjectId } from 'mongodb'; |
| 3 | import type { WithId, Document } from 'mongodb'; |
| 4 | |
| 5 | import type { |
| 6 | Conversation, |
| 7 | ConversationFilter, |
| 8 | ConversationSummary, |
| 9 | CreateConversationInput, |
| 10 | } from '@/types/conversation'; |
| 11 | |
| 12 | import { getMongoClient } from '@/clients/mongo.client'; |
| 13 | |
| 14 | const CONVERSATIONS_COLLECTION = 'conversations'; |
| 15 | const MESSAGES_COLLECTION = 'messages'; |
| 16 | |
| 17 | const toConversation = (doc: WithId<Document>): Conversation => ({ |
| 18 | id: String(doc._id), |
| 19 | title: typeof doc.title === 'string' ? doc.title : null, |
| 20 | participantIds: Array.isArray(doc.participantIds) ? (doc.participantIds as string[]) : [], |
| 21 | createdAt: new Date(doc.createdAt as string | number | Date), |
| 22 | updatedAt: new Date(doc.updatedAt as string | number | Date), |
| 23 | lastMessageAt: doc.lastMessageAt ? new Date(doc.lastMessageAt as string | number | Date) : null, |
| 24 | lastMessagePreview: typeof doc.lastMessagePreview === 'string' ? doc.lastMessagePreview : null, |
| 25 | }); |
| 26 | |
| 27 | const toConversationSummary = (doc: WithId<Document>): ConversationSummary => toConversation(doc); |
| 28 | |
| 29 | export const conversationRepository = { |
| 30 | async create(input: CreateConversationInput): Promise<Conversation> { |
| 31 | const client = await getMongoClient(); |
| 32 | const db = client.db(); |
| 33 | const collection = db.collection(CONVERSATIONS_COLLECTION); |
| 34 | const now = new Date(); |
| 35 | const document = { |
| 36 | _id: randomUUID(), |
| 37 | title: input.title ?? null, |
| 38 | participantIds: input.participantIds, |
| 39 | createdAt: now, |
| 40 | updatedAt: now, |
| 41 | lastMessageAt: null, |
| 42 | lastMessagePreview: null, |
| 43 | }; |
| 44 | |
| 45 | await collection.insertOne(document as unknown as Document); |
| 46 | return toConversation(document as unknown as WithId<Document>); |
| 47 | }, |
| 48 | |
| 49 | async findById(id: string): Promise<Conversation | null> { |
| 50 | const client = await getMongoClient(); |
| 51 | const db = client.db(); |
| 52 | const doc = await db |
| 53 | .collection(CONVERSATIONS_COLLECTION) |
| 54 | .findOne({ _id: id as unknown as ObjectId }); |
| 55 | return doc ? toConversation(doc) : null; |
| 56 | }, |
| 57 | |
| 58 | async findSummaries(filter: ConversationFilter): Promise<ConversationSummary[]> { |
| 59 | const client = await getMongoClient(); |
| 60 | const db = client.db(); |
| 61 | const cursor = db |
| 62 | .collection(CONVERSATIONS_COLLECTION) |
| 63 | .find({ participantIds: filter.participantId }) |
| 64 | .sort({ lastMessageAt: -1, updatedAt: -1 }); |
| 65 | const results = await cursor.toArray(); |
| 66 | return results.map((doc) => toConversationSummary(doc)); |
| 67 | }, |
| 68 | |
| 69 | async touchConversation(conversationId: string, preview: string): Promise<void> { |
| 70 | const client = await getMongoClient(); |
| 71 | const db = client.db(); |
| 72 | await db.collection(CONVERSATIONS_COLLECTION).updateOne( |
| 73 | { _id: conversationId as unknown as ObjectId }, |
| 74 | { |
| 75 | $set: { |
| 76 | lastMessageAt: new Date(), |
| 77 | lastMessagePreview: preview, |
| 78 | updatedAt: new Date(), |
| 79 | }, |
| 80 | }, |
| 81 | ); |
| 82 | }, |
| 83 | |
| 84 | async removeAll(): Promise<void> { |
| 85 | const client = await getMongoClient(); |
| 86 | const db = client.db(); |
| 87 | await Promise.all([ |
| 88 | db.collection(CONVERSATIONS_COLLECTION).deleteMany({}), |
| 89 | db.collection(MESSAGES_COLLECTION).deleteMany({}), |
| 90 | ]); |
| 91 | }, |
| 92 | }; |