From 56897f491a57321045c85f30e7deb834f09bfa59 Mon Sep 17 00:00:00 2001 From: David Rouyer Date: Sun, 10 Sep 2023 17:28:47 +0200 Subject: [PATCH] fix: send message --- .../src/components/messages/message-form.tsx | 9 ++++++--- .../src/hooks/useTicket/TicketProvider.tsx | 2 +- .../src/hooks/useTicket/TicketStorage.ts | 3 ++- apps/customer-service/src/hooks/useTicket/User.ts | 10 +++++++++- apps/customer-service/src/utils/session.ts | 15 ++++++++++++++- packages/auth/index.ts | 1 + 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/apps/customer-service/src/components/messages/message-form.tsx b/apps/customer-service/src/components/messages/message-form.tsx index 379d567e4..ae4795b52 100644 --- a/apps/customer-service/src/components/messages/message-form.tsx +++ b/apps/customer-service/src/components/messages/message-form.tsx @@ -32,10 +32,13 @@ export const MessageForm: FC = () => { status: MessageStatus.Pending, content: data.content, createdAt: new Date(), + senderId: currentUser?.contactId ?? 0, sender: { - id: currentUser?.id ?? '', - name: currentUser?.fullName, - avatarUrl: currentUser?.image, + id: currentUser?.contactId ?? 0, + name: currentUser?.name ?? '', + email: currentUser?.email ?? '', + avatarUrl: currentUser?.image ?? '', + createdAt: new Date(), }, }, }); diff --git a/apps/customer-service/src/hooks/useTicket/TicketProvider.tsx b/apps/customer-service/src/hooks/useTicket/TicketProvider.tsx index c06b9e4a5..ee5bc6351 100644 --- a/apps/customer-service/src/hooks/useTicket/TicketProvider.tsx +++ b/apps/customer-service/src/hooks/useTicket/TicketProvider.tsx @@ -19,7 +19,7 @@ import { User } from '~/hooks/useTicket/User'; import { api } from '~/utils/api'; export type SendMessageParams = { - message: Omit; + message: Omit & { senderId: number }; ticketId: TicketId; }; diff --git a/apps/customer-service/src/hooks/useTicket/TicketStorage.ts b/apps/customer-service/src/hooks/useTicket/TicketStorage.ts index 49d176a51..7089717b7 100644 --- a/apps/customer-service/src/hooks/useTicket/TicketStorage.ts +++ b/apps/customer-service/src/hooks/useTicket/TicketStorage.ts @@ -17,7 +17,8 @@ export class TicketStorage { user.id, user.name ?? '', user.email ?? '', - user.image ?? undefined + user.image ?? undefined, + user.contactId ); } diff --git a/apps/customer-service/src/hooks/useTicket/User.ts b/apps/customer-service/src/hooks/useTicket/User.ts index 13ded01d2..db6ea16e8 100644 --- a/apps/customer-service/src/hooks/useTicket/User.ts +++ b/apps/customer-service/src/hooks/useTicket/User.ts @@ -5,12 +5,20 @@ export class User { name: string; email: string; image?: string; + contactId?: number; - constructor(id: UserId, name: string, email: string, image?: string) { + constructor( + id: UserId, + name: string, + email: string, + image?: string, + contactId?: number + ) { this.id = id; this.name = name; this.email = email; this.image = image; + this.contactId = contactId; } get fullName() { diff --git a/apps/customer-service/src/utils/session.ts b/apps/customer-service/src/utils/session.ts index dc79983ca..8179ead9c 100644 --- a/apps/customer-service/src/utils/session.ts +++ b/apps/customer-service/src/utils/session.ts @@ -1,7 +1,20 @@ import { auth } from '@cs/auth'; +import { db, eq, schema } from '@cs/database'; export async function getCurrentUser() { const session = await auth(); - return session?.user; + if (!session) { + return null; + } + + // TODO: refactor when auth session callback works + const user = await db.query.users.findFirst({ + where: eq(schema.users.id, session.user.id), + }); + + return { + ...session?.user, + contactId: user?.contactId, + }; } diff --git a/packages/auth/index.ts b/packages/auth/index.ts index 30901cad6..dcc324876 100644 --- a/packages/auth/index.ts +++ b/packages/auth/index.ts @@ -16,6 +16,7 @@ declare module 'next-auth' { interface Session { user: { id: string; + contactId?: number; } & DefaultSession['user']; } }