All files / src/pages/AcquirerPortal/LegalDocuments/api useLegalDocumentMutations.ts

100% Statements 38/38
50% Branches 2/4
100% Functions 15/15
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111              8x 898x 898x             8x 290x 290x 290x   10x           9x 9x           1x 1x 1x             8x 290x 290x 290x   6x             6x 6x           8x 28x 28x 28x           5x             4x 4x 4x           8x 290x 290x 290x   4x             3x 3x 3x     1x 1x            
import { useMutation, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { showMessage } from "@common/Toast";
import { LEGAL_DOC_KEYS } from "./keys";
import type { DocumentVersion } from "../types";
 
const useAccID = () => {
  const { data } = useGetMerchantById();
  return data?.accID as number | string | undefined;
};
 
/**
 * Opens a fresh editable draft from the currently-published document.
 * The documentID is supplied at call time: `useOpenDraft().mutate(documentID)`.
 */
export const useOpenDraft = () => {
  const accID = useAccID();
  const qc = useQueryClient();
  return useMutation<DocumentVersion, unknown, number | string>(
    (documentID) =>
      customInstance({
        url: `/merchants/${accID}/legal-documents/${documentID}/draft`,
        method: "POST",
      }),
    {
      onSuccess: () => {
        qc.invalidateQueries(LEGAL_DOC_KEYS.list);
        qc.invalidateQueries(LEGAL_DOC_KEYS.detail);
      },
      onError: (err: any) => {
        // Case 1: another user grabbed the edit lock first. The lock poll hasn't
        // caught up yet, so refetch it now to disable the Edit button, and tell
        // the user why. (409 is not auto-toasted by the interceptor.)
        Eif (err?.response?.status === 409) {
          showMessage("Error", err?.response?.data?.message);
          qc.invalidateQueries(LEGAL_DOC_KEYS.lock);
        }
      },
    },
  );
};
 
export const useSaveDraft = (documentID: number | string) => {
  const accID = useAccID();
  const qc = useQueryClient();
  return useMutation<DocumentVersion, unknown, { content: unknown }>(
    (body) =>
      customInstance({
        url: `/merchants/${accID}/legal-documents/${documentID}/draft`,
        method: "PATCH",
        data: body,
      }),
    {
      onSuccess: () => {
        qc.invalidateQueries(LEGAL_DOC_KEYS.detail);
        qc.invalidateQueries(LEGAL_DOC_KEYS.list);
      },
    },
  );
};
 
export const usePublishDocument = (documentID: number | string) => {
  const accID = useAccID();
  const qc = useQueryClient();
  return useMutation<
    DocumentVersion,
    unknown,
    { version: string; publishedDate: string; notify: boolean }
  >(
    (body) =>
      customInstance({
        url: `/merchants/${accID}/legal-documents/${documentID}/publish`,
        method: "POST",
        data: body,
      }),
    {
      onSuccess: () => {
        qc.invalidateQueries(LEGAL_DOC_KEYS.list);
        qc.invalidateQueries(LEGAL_DOC_KEYS.detail);
        qc.invalidateQueries(LEGAL_DOC_KEYS.history);
      },
    },
  );
};
 
export const useRestoreVersion = (documentID: number | string) => {
  const accID = useAccID();
  const qc = useQueryClient();
  return useMutation<DocumentVersion, unknown, { versionID: number | string }>(
    (body) =>
      customInstance({
        url: `/merchants/${accID}/legal-documents/${documentID}/restore`,
        method: "POST",
        data: body,
      }),
    {
      onSuccess: () => {
        qc.invalidateQueries(LEGAL_DOC_KEYS.list);
        qc.invalidateQueries(LEGAL_DOC_KEYS.detail);
        qc.invalidateQueries(LEGAL_DOC_KEYS.history);
      },
      onError: (err: any) => {
        Eif (err?.response?.status === 409) {
          showMessage("Error", err?.response?.data?.message);
        }
      },
    },
  );
};