send formatter recipient phone number in conversations
This commit is contained in:
		@@ -6,6 +6,12 @@ import db, { Direction, Message, Prisma } from "../../../db";
 | 
			
		||||
import { decrypt } from "../../../db/_encryption";
 | 
			
		||||
import { enforceSuperAdminIfNotCurrentOrganization, setDefaultOrganizationId } from "../../core/utils";
 | 
			
		||||
 | 
			
		||||
type Conversation = {
 | 
			
		||||
	recipient: string;
 | 
			
		||||
	formattedPhoneNumber: string;
 | 
			
		||||
	messages: Message[];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default resolver.pipe(
 | 
			
		||||
	resolver.zod(z.object({ organizationId: z.string().optional() })),
 | 
			
		||||
	resolver.authorize(),
 | 
			
		||||
@@ -26,7 +32,7 @@ export default resolver.pipe(
 | 
			
		||||
			orderBy: { sentAt: Prisma.SortOrder.asc },
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		let conversations: Record<string, Message[]> = {};
 | 
			
		||||
		let conversations: Record<string, Conversation> = {};
 | 
			
		||||
		for (const message of messages) {
 | 
			
		||||
			let recipient: string;
 | 
			
		||||
			if (message.direction === Direction.Outbound) {
 | 
			
		||||
@@ -34,23 +40,28 @@ export default resolver.pipe(
 | 
			
		||||
			} else {
 | 
			
		||||
				recipient = message.from;
 | 
			
		||||
			}
 | 
			
		||||
			const parsedPhoneNumber = new PhoneNumber(recipient);
 | 
			
		||||
			recipient = parsedPhoneNumber.getNumber("international");
 | 
			
		||||
			const formattedPhoneNumber = new PhoneNumber(recipient).getNumber("international");
 | 
			
		||||
 | 
			
		||||
			if (!conversations[recipient]) {
 | 
			
		||||
				conversations[recipient] = [];
 | 
			
		||||
				conversations[recipient] = {
 | 
			
		||||
					recipient,
 | 
			
		||||
					formattedPhoneNumber,
 | 
			
		||||
					messages: [],
 | 
			
		||||
				};
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			conversations[recipient]!.push({
 | 
			
		||||
			conversations[recipient]!.messages.push({
 | 
			
		||||
				...message,
 | 
			
		||||
				content: decrypt(message.content, organization.encryptionKey),
 | 
			
		||||
			});
 | 
			
		||||
 | 
			
		||||
			conversations[recipient]!.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
 | 
			
		||||
			conversations[recipient]!.messages.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
 | 
			
		||||
		}
 | 
			
		||||
		conversations = Object.fromEntries(
 | 
			
		||||
			Object.entries(conversations).sort(
 | 
			
		||||
				([, a], [, b]) => b[b.length - 1]!.sentAt.getTime() - a[a.length - 1]!.sentAt.getTime(),
 | 
			
		||||
				([, a], [, b]) =>
 | 
			
		||||
					b.messages[b.messages.length - 1]!.sentAt.getTime() -
 | 
			
		||||
					a.messages[a.messages.length - 1]!.sentAt.getTime(),
 | 
			
		||||
			),
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user