feat: make the support require nothing

This commit is contained in:
Nathnael
2026-07-08 05:33:02 +00:00
parent 9cd3ac42b4
commit a817976db8
11 changed files with 237 additions and 508 deletions

View File

@@ -113,6 +113,61 @@ export class SupportService {
return this.firstMessage(conversation, input.initialMessage);
}
// ---- customer: device-scoped single thread (portal) -------------------
/** The device's single conversation + its messages ({conversation:null} if none). */
async getDeviceThread(deviceId: string): Promise<T.PassengerSupportThreadDto> {
if (!deviceId) return { conversation: null, messages: [] };
const c = (await this.prisma.supportConversation.findFirst({
where: { guestId: deviceId },
orderBy: { createdAt: 'asc' },
})) as ConversationRow | null;
if (!c) return { conversation: null, messages: [] };
const rows = await this.prisma.supportMessage.findMany({
where: { conversationId: c.id },
orderBy: { createdAt: 'asc' },
});
const unread = await this.computeUnread([c], 'USER');
return {
conversation: this.toConversationDto(c, unread.get(c.id) ?? 0),
messages: rows.map((m) => this.toMessageDto(m)),
};
}
/** Append a message to the device's thread, creating it on first message. */
async sendDeviceMessage(
deviceId: string,
text: string,
): Promise<T.PassengerSupportMessageDto> {
let c = (await this.prisma.supportConversation.findFirst({
where: { guestId: deviceId },
orderBy: { createdAt: 'asc' },
})) as ConversationRow | null;
if (!c) {
c = (await this.prisma.supportConversation.create({
data: { guestId: deviceId, subject: 'Support chat', status: 'OPEN' },
})) as ConversationRow;
}
const updated = await this.appendMessage(c, 'USER', text);
const last = updated.messages[updated.messages.length - 1];
return this.toMessageDto(last);
}
/** Mark the device's thread read (customer side). */
async markDeviceRead(deviceId: string): Promise<{ unreadCount: number }> {
const c = await this.prisma.supportConversation.findFirst({
where: { guestId: deviceId },
orderBy: { createdAt: 'asc' },
});
if (c) {
await this.prisma.supportConversation.update({
where: { id: c.id },
data: { userLastReadAt: new Date() },
});
}
return this.unreadCount('USER', { guestId: deviceId });
}
async listForCustomer(
owner: CustomerOwner,
query: ListQuery,