All files / react/components/Header ImpersonatorBanner.jsx

15% Statements 3/20
0% Branches 0/6
0% Functions 0/4
15% Lines 3/20

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                  2x 2x                   2x                                                                                                                                    
import { parse } from "cookie";
import { CloseIcon } from "../../lib/SvgComponents";
import { TextButton } from "../Button";
import {
  deleteImpersonatedUser,
  revertQppHasAuthsCookie,
  revertApmPaymentCookie,
} from "../../session/logout";
 
const viewingToolUrl = "/user/helpdesk-viewing-tool";
const getViewType = (viewType) =>
  ({
    username: "HARP ID",
    npi: "NPI",
    tin: "TIN",
    apm_id: "APM Entity",
    cms_id: "Registry",
    vg_id: "Virtual Group",
  })[viewType];
 
const ImpersonatorBanner = () => {
  const {
    qpp_auth_token: token = null,
    qpp_impersonated_user: user = null,
    qpp_impersonated_type: viewType = null,
  } = parse(document.cookie);
 
  const className = [
    "qpp-u-display--flex",
    "qpp-u-justify-content--between",
    "qpp-u-fill--gold-20",
    "qpp-u-padding-x--40",
    "qpp-u-padding-y--24",
    "qpp-u-font-size--14",
    "qpp-u-color--gray-80",
  ].join(" ");
 
  const onClick = async () => {
    const fn = () => {
      deleteImpersonatedUser({
        qpp_impersonated_user: user,
        qpp_impersonated_type: viewType,
      });
 
      // Set user_has_apm_payments cookie back to impersonator's value
      revertApmPaymentCookie();
 
      // Set qpp_has_authorizations cookie back to impersonator's value
      revertQppHasAuthsCookie();
      window.location.href = viewingToolUrl;
    };
    try {
      const { origin } = window.location;
      const url = `${origin}/api/auth/helpdesk/view`;
      const options = {
        method: "DELETE",
        headers: {
          Accept: "application/vnd.qpp.cms.gov.v2+json",
          Authorization: `Bearer ${token}`,
        },
      };
      await fetch(url, options);
      fn();
    } catch (error) {
      fn();
    }
  };
 
  return (
    token &&
    user && (
      <div className={className}>
        <div>
          VIEW ONLY | You are currently viewing {getViewType(viewType)}:{" "}
          <strong>{user}</strong>
        </div>
        <TextButton onClick={onClick} className="qpp-u-color--gray-80">
          Exit View Mode
          <CloseIcon />
        </TextButton>
      </div>
    )
  );
};
 
export default ImpersonatorBanner;