0
\$\begingroup\$

In a Node app where there is a messaging service, I've added a method to add a conversation and a member. The method functionality is that when a new request hit an API for messaging, that calls a method called JoinConversation. In the request there are some inputs from a client and based on those inputs the method do a check based on some conditions

  1. There is a first check if a conversations of the same already exist in the DB. If that exists then checks if the member already exist. If the member input info are presents in DB for that conversations it is returning the conversation related but if the member not preset then is creating a new member for that conversation.
  2. If above is false means the conversation does not exist then a new conversation is created and also a new member is created also

The method is doing fine the job but the way I wrote it sounds redundant to me and was seeking a better way to write it and in less lines if that would be possible.

The code of it and ignore DAO is a security external plugin I cannot explain. The method uses KnexJS and Apollo.

      joinConversation({ input }) {
    const { ApolloError } = this.errors;
    return this.withDAO(async ({ conversation, member }) => {
      const { originId, memberId, memberType, name, originType } = input;

      const existingConversation = await conversation.selectFirst({
        originId,
        originType,
      });

      if (existingConversation) {
        const existingMemberPerDocument = await member.selectFirst({
          memberId,
        });

        if (!isEmpty(existingMemberPerDocument)) {
          await conversation.selectFirst({
            conversationId: existingConversation.conversationId,
            originType: existingConversation.originType,
          });

          return existingConversation;
        }

        await member.insert({
          conversationId: existingConversation.id,
          memberId,
          memberType,
          name,
        });

        if (isEmpty(member)) {
          throw new ApolloError('Error while Adding new conversation member!');
        }

        return existingMemberPerDocument;
      }

      const createdConversation = await conversation.insert({
        ...input,
      });

      if (isEmpty(createdConversation)) {
        throw new ApolloError('Error while Adding new conversation!');
      }

      await member.insert({
        conversationId: createdConversation.id,
        memberId,
        memberType,
        name,
      });

      return createdConversation;
    });
  }
        
        
\$\endgroup\$
2
  • \$\begingroup\$ Hi Jakub, looks like the end of the code is not valid JS. Did something happen during copy pasting? \$\endgroup\$ – konijn Mar 9 at 12:00
  • \$\begingroup\$ The last part action is not part of it I don't know why that happen i will edit and paste again to be sure about it is right thanks for catch \$\endgroup\$ – Jakub Mar 9 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.