From bedaea1a73113f1bc9229bb7365dd8bc81e516cc Mon Sep 17 00:00:00 2001 From: ceddybi Date: Mon, 1 Apr 2024 15:41:28 -0400 Subject: [PATCH] startChatFromOrder group --- package.json | 4 +-- src/app/html/order/[slug]/chat/page.tsx | 17 +++++----- src/components/types.generated.ts | 5 +++ src/lib/hooksServer/chat.ts | 42 ++++++++++++++++--------- yarn.lock | 16 +++++----- 5 files changed, 50 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index a68d7b1..22613e0 100644 --- a/package.json +++ b/package.json @@ -34,10 +34,10 @@ "@hookform/resolvers": "^3.3.4", "@logtail/pino": "^0.4.19", "@roadmanjs/auth-client": "^0.0.32", - "@roadmanjs/chat-client": "^0.0.62", + "@roadmanjs/chat-client": "^0.0.66", "@roadmanjs/firebase-client": "^0.0.28", "@roadmanjs/social-client": "^0.0.28", - "@roadmanjs/wallet-client": "^0.0.65", + "@roadmanjs/wallet-client": "^0.0.74", "@sentry/nextjs": "^7.104.0", "@spotlightjs/spotlight": "^1.2.13", "@stoqey/client-graphql": "^1.1.13", diff --git a/src/app/html/order/[slug]/chat/page.tsx b/src/app/html/order/[slug]/chat/page.tsx index 64abb23..56e0233 100644 --- a/src/app/html/order/[slug]/chat/page.tsx +++ b/src/app/html/order/[slug]/chat/page.tsx @@ -3,7 +3,7 @@ import { cookies } from 'next/headers'; import type { OrderTypeOutput, UserType } from '@/components/types.generated'; import { getClient } from '@/lib/apollo-wrapper.server'; import { GET_ORDER } from '@/lib/gql'; -import { fetchChatMessages, startChatFromUsers } from '@/lib/hooksServer/chat'; +import { fetchChatMessages, startChatFromOrder } from '@/lib/hooksServer/chat'; import { getMe } from '@/lib/hooksServer/user'; import { getVendor } from '@/lib/hooksServer/vendor'; @@ -31,16 +31,15 @@ const OrderViewPage = async ({ params }: { params: { slug: string } }) => { if (order && user) { vendor = await getVendor(); - // TODO after group - // convo = await startChatFromOrder({ - // order, - // user, - // }); - - convo = await startChatFromUsers({ - friends: [order.seller as UserType], + convo = await startChatFromOrder({ + order, user, }); + + // convo = await startChatFromUsers({ + // friends: [order.seller as UserType], + // user, + // }); const messagesPage = convo ? await fetchChatMessages({ diff --git a/src/components/types.generated.ts b/src/components/types.generated.ts index 700937d..fa74fe8 100644 --- a/src/components/types.generated.ts +++ b/src/components/types.generated.ts @@ -238,6 +238,9 @@ export type ChatConvo = { name?: Maybe; owner?: Maybe; public?: Maybe; + source?: Maybe; + sourceId?: Maybe; + sourceType?: Maybe; unread?: Maybe; updatedAt?: Maybe; }; @@ -253,6 +256,8 @@ export type ChatConvoInput = { name?: InputMaybe; owner?: InputMaybe; public?: InputMaybe; + sourceId?: InputMaybe; + sourceType?: InputMaybe; unread?: InputMaybe; updatedAt?: InputMaybe; }; diff --git a/src/lib/hooksServer/chat.ts b/src/lib/hooksServer/chat.ts index e586d44..0633003 100644 --- a/src/lib/hooksServer/chat.ts +++ b/src/lib/hooksServer/chat.ts @@ -13,6 +13,7 @@ import pickBy from 'lodash/pickBy'; import type { ChatConvo, + ChatConvoInput, ChatConvoPagination, ChatMessagePagination, OrderTypeOutput, @@ -22,6 +23,7 @@ import type { import { getClient } from '../apollo-wrapper.server'; import type { ResType } from '../gql'; import type { CreateChatConvoType } from '../hooks/apiChat'; +import { logger } from '../Logger'; interface ChatConvoArgs { userId: string; @@ -118,45 +120,55 @@ export const fetchChatMessages = async ( return undefined; }; -export interface StartChatFromOrderProps { - order: OrderTypeOutput; +interface StartChatFromOrder { user: UserType; + order: OrderTypeOutput; } /** - * @param order - * @param client + * Start chat from order + * @param {user, order} * @returns */ export const startChatFromOrder = async ( - props: StartChatFromOrderProps, -): Promise => { - const { order, user } = props; - const owner = _get(user, 'id', '') || ''; + args: StartChatFromOrder, +): Promise => { + const { user, order } = args; + const owner = _get(user, 'id', '') as any; const members: string[] = uniq( compact([order.owner?.id, order.seller?.id, owner]), ); - const item: CreateChatConvoType = { + const item: ChatConvoInput = { name: `Order #${order.id}`, // use 4 letters from the order.id // avatar: ad.photos ? ad.photos[0] : "", owner, group: true, members, + sourceType: 'order', + sourceId: order.id, }; - const { data } = await getClient().mutate<{ data: ResType }>({ - mutation: START_CHAT_CONVO_MUTATIONS, - variables: { args: item }, - fetchPolicy: 'no-cache', - }); + const [errorData, resData] = await awaitTo( + getClient().mutate<{ data: ResType }>({ + mutation: START_CHAT_CONVO_MUTATIONS, + variables: { args: item }, + fetchPolicy: 'no-cache', + }), + ); + if (errorData) { + logger.error(errorData); + return null; + } + const data = resData?.data; if (data && data.data) { const chatConvo = data.data.data; return chatConvo; } - throw new Error('error getting start convo'); + logger.error(new Error('error getting start convo')); + return null; }; interface StartChatFromUser { diff --git a/yarn.lock b/yarn.lock index 0aed873..efac76a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4604,10 +4604,10 @@ dependencies: graphql-tag "^2.12.5" -"@roadmanjs/chat-client@^0.0.62": - version "0.0.62" - resolved "https://registry.yarnpkg.com/@roadmanjs/chat-client/-/chat-client-0.0.62.tgz#ba6f80af10988ed5da036a288d94198b16aa8bfe" - integrity sha512-XS83zSWTc7ctAbHXVlJKp6l7ZQFuw6BuQmJQOBvIDyJRzbGZyyqe8blv9czgTX8cHCWz1CTgi0Y7sjFmrPFzMA== +"@roadmanjs/chat-client@^0.0.66": + version "0.0.66" + resolved "https://registry.yarnpkg.com/@roadmanjs/chat-client/-/chat-client-0.0.66.tgz#9feda89901912d3384bdf8ca8619f5076b8e7748" + integrity sha512-NrSYxeExenZDNLj2CRxB18rEEEk6HlbmxQCPHxV0IgAuV9vnitcKwkeNP6+38GiIxtihya7/j4P56Ub3fl4gNA== dependencies: graphql-tag "^2.12.5" @@ -4625,10 +4625,10 @@ dependencies: graphql-tag "^2.12.5" -"@roadmanjs/wallet-client@^0.0.65": - version "0.0.65" - resolved "https://registry.yarnpkg.com/@roadmanjs/wallet-client/-/wallet-client-0.0.65.tgz#bcb870cc37d4e06ce57146182c0365c231083d81" - integrity sha512-REP9D7PaO/xl+eQs7iKoDwENsNLXSqvoxnw0UlG/1Wq4ZayCbplV3kIpMq2gxXW8CFXD5ucwfpfO+BtAEZB3Zw== +"@roadmanjs/wallet-client@^0.0.74": + version "0.0.74" + resolved "https://registry.yarnpkg.com/@roadmanjs/wallet-client/-/wallet-client-0.0.74.tgz#f00395950d3acc7b3b99dad0e89038d2e1df43c2" + integrity sha512-xgCD2Lo18UD/TR1THbE9pB5RuJqoyWlB7rxsOw1a8T2oB2rKVmKdnSR0PaidLIfTxVWVC0KgK7fn95ByzpQGuA== dependencies: graphql-tag "^2.12.5"