Skip to content

Commit

Permalink
startChatFromOrder group
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Apr 1, 2024
1 parent 69ddbfa commit bedaea1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 8 additions & 9 deletions src/app/html/order/[slug]/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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({
Expand Down
5 changes: 5 additions & 0 deletions src/components/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ export type ChatConvo = {
name?: Maybe<Scalars['String']>;
owner?: Maybe<ChatUserType>;
public?: Maybe<Scalars['Boolean']>;
source?: Maybe<Scalars['JSON']>;
sourceId?: Maybe<Scalars['String']>;
sourceType?: Maybe<Scalars['String']>;
unread?: Maybe<Scalars['Float']>;
updatedAt?: Maybe<Scalars['DateTime']>;
};
Expand All @@ -253,6 +256,8 @@ export type ChatConvoInput = {
name?: InputMaybe<Scalars['String']>;
owner?: InputMaybe<Scalars['String']>;
public?: InputMaybe<Scalars['Boolean']>;
sourceId?: InputMaybe<Scalars['String']>;
sourceType?: InputMaybe<Scalars['String']>;
unread?: InputMaybe<Scalars['Float']>;
updatedAt?: InputMaybe<Scalars['DateTime']>;
};
Expand Down
42 changes: 27 additions & 15 deletions src/lib/hooksServer/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import pickBy from 'lodash/pickBy';

import type {
ChatConvo,
ChatConvoInput,
ChatConvoPagination,
ChatMessagePagination,
OrderTypeOutput,
Expand All @@ -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;
Expand Down Expand Up @@ -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<ChatConvo> => {
const { order, user } = props;
const owner = _get(user, 'id', '') || '';
args: StartChatFromOrder,
): Promise<ChatConvo | null> => {
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 {
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand Down

0 comments on commit bedaea1

Please sign in to comment.