All files / src/features/ChangeLog LogItem.tsx

92% Statements 69/75
81.13% Branches 86/106
100% Functions 12/12
91.78% Lines 67/73

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388                                                                                                  45x     45x   45x       45x   45x 45x 1x 2x     2x 2x       1x                   1x     1x 44x 2x 2x 2x   1x         2x                   2x 42x     45x                                               45x 45x 45x                                                                                         117x                 45x       32x 29x             3x 2x               1x 1x                     6x             6x   1x             5x   1x             4x   1x         3x   2x           1x                 1x   1x 1x                   5x             5x 2x     2x           3x   1x         2x   1x                         1x 1x                     2x 2x 2x         2x                     117x 31x           117x                     4x   4x 3x           1x              
import { Box, Stack } from "@mui/material";
import { ChangeItem, GroupType } from "@services/api/changelog/changelog";
import placeholder from "assets/images/Placeholder.png";
import { IconContainer, Label, LabelValue } from "./LogItem.styles";
import { Fragment, useMemo } from "react";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import {
  createCustomAction,
  formatFieldLabel,
  formatFieldValue,
} from "./utils/logItemUtils";
import { toFieldChangeList } from "./utils/normalizeFieldChanges";
import { MERCHANT_FILE_TEXT } from "@constants/stringConstants";
import GiveText from "@shared/Text/GiveText";
import { PencilSimpleIcon } from "@phosphor-icons/react";
import { useAppTheme } from "@theme/v2/Provider";
import GiveDivider from "@shared/GiveDivider/GiveDivider";
import GiveThumbnail from "@shared/Thumbnail/GiveThumbnail";
 
interface Props {
  date: number;
  group: GroupType;
  name: string;
  imageUrl?: string;
  item: ChangeItem[];
  resourceName?: string;
  email?: string;
  isFirst?: boolean;
  isLast?: boolean;
}
 
interface MappedItems extends ChangeItem {
  additionalField?: string | number;
  additionalTo?: string | number;
  additionalFrom?: string | number;
}
 
export default function LogItem({
  date,
  group,
  name,
  item,
  imageUrl,
  resourceName,
  email,
  isFirst,
  isLast,
}: Props) {
  const { palette } = useAppTheme();
  // `item` mirrors the API's free-form fieldChanges column, which is not
  // guaranteed to be a list for every audit row (GB-21601)
  const changes = useMemo(() => toFieldChangeList(item), [item]);
  const createdBank =
    group === "Bank Account" &&
    createCustomAction(changes[0]) === "add" &&
    changes[0]?.fieldName !== "Document";
  const createdBusinessOwner =
    group === "Business Owner" && createCustomAction(changes[0]) === "add";
 
  const usedItems: MappedItems[] = useMemo(() => {
    if (createdBank) {
      const bankItemsSorted = changes.sort((a, b) => {
        Iif (a.fieldName < b.fieldName) {
          return -1;
        }
        Eif (a.fieldName > b.fieldName) {
          return 1;
        }
        return 0;
      });
      const used = [
        {
          fieldName: bankItemsSorted[0].fieldName,
          newValue: bankItemsSorted[0].newValue,
          oldValue: bankItemsSorted[0].oldValue,
          additionalField: bankItemsSorted[0].fieldName,
          additionalTo: bankItemsSorted[0].newValue,
          operation: bankItemsSorted[0].operation,
        },
        ...bankItemsSorted.slice(2).map((ix) => {
          return { ...ix, additionalField: bankItemsSorted[0].newValue };
        }),
      ];
      return used;
    } else if (createdBusinessOwner) {
      const fullnameField = changes.find((xi) => xi.fieldName === "Full Name");
      const filteredItems = changes
        .filter((xb) => xb.fieldName !== "Full Name")
        .map((xb) => {
          return {
            ...xb,
            additionalField: fullnameField?.newValue,
          };
        });
      const used = fullnameField
        ? [
            {
              newValue: fullnameField.newValue,
              operation: fullnameField.operation,
              fieldName: "",
              additionalField: "",
            },
          ]
        : filteredItems;
      return used;
    } else return changes;
  }, [changes]);
 
  return (
    <Stack direction="row" gap="16px" mt={isFirst ? 0 : "20px"}>
      <Stack alignItems="center" width="24px">
        <IconContainer isFirst={isFirst}>
          <PencilSimpleIcon
            size={16}
            color={
              isFirst
                ? palette.primitive?.blue[100]
                : palette.icon?.["icon-primary"]
            }
          />
        </IconContainer>
        {!isLast && (
          <GiveDivider
            direction="vertical"
            bgType="tertiary"
            sx={{ flex: 1, mb: 0 }}
          />
        )}
      </Stack>
      <Stack width="100%" gap="8px">
        <GiveText variant="bodyS">{group}</GiveText>
        {usedItems.map((change, index) => {
          const action: Action = createCustomAction(change);
          const fieldName = change.fieldName?.toLowerCase();
          return (
            <Fragment
              key={`${change.fieldName ?? ""}-${
                change.operation ?? ""
              }-${index}`}
            >
              {buildDescription({
                action: action,
                to: formatFieldValue(change.newValue, fieldName),
                from: formatFieldValue(change.oldValue, fieldName),
                group: group,
                field: fieldName,
                additionalField: change?.additionalField ?? resourceName,
                additionalTo: change?.additionalTo,
              })}
            </Fragment>
          );
        })}
        <Label>{moment.unix(date).tz(PLATFORM_TIMEZONE).format("MMM DD, YYYY, HH:mm")}</Label>
        <Stack direction="row" gap="8px" alignItems="center">
          <GiveThumbnail
            imageUrl={imageUrl ? `${imageUrl}/thumb` : undefined}
            size="mini"
            shouldBeRounded
            type="avatar"
          />
          <Label>{name?.trim() ? name : email}</Label>
        </Stack>
      </Stack>
    </Stack>
  );
}
 
type Action = "update" | "add" | "remove" | "insert";
 
interface BuildDescriptionArgs {
  from?: string | number;
  to?: string | number;
  field: string | number;
  additionalField?: string | number;
  additionalTo?: string | number;
  group: GroupType;
  action: Action;
}
 
const buildDescription = ({
  from,
  to,
  group,
  field,
  action,
  additionalField,
  additionalTo,
}: BuildDescriptionArgs) => {
  switch (group) {
    case "Business Profile":
    case "Primary Account Holder":
    case MERCHANT_FILE_TEXT: {
      if (action === "update") {
        return (
          <ChangedLabel
            from={from}
            to={to}
            field={formatFieldLabel(field, group)}
          />
        );
      } else if (action === "insert") {
        return (
          <AddOrRemoveLabel
            to={to}
            field={field}
            switchOrder={group === "Business Profile"}
          />
        );
      } else {
        const useFrom = action !== "add";
        return (
          <AddOrRemoveLabel
            to={useFrom ? from : to}
            field={field}
            switchOrder={group === "Business Profile"}
            remove={action !== "add"}
          />
        );
      }
    }
    case "Bank Account": {
      Iif (action === "insert" && field === "account name") {
        return (
          <Label>
            Added <LabelValue>{to}</LabelValue> bank account
          </Label>
        );
      }
      if (action === "update") {
        //update a field of an existing bank account
        return (
          <ChangedLabel
            from={from}
            to={to}
            field={additionalField ? `${additionalField} ${field}` : field}
          />
        );
      } else if (action === "add" && additionalTo) {
        //creation of a bank account with 'name' and 'field'
        return (
          <Label>
            Added bank account with {additionalField}{" "}
            <LabelValue>{additionalTo}</LabelValue> and {field}{" "}
            <LabelValue>{to}</LabelValue>
          </Label>
        );
      } else if (action === "add" && !additionalTo && field !== "document") {
        //addition of a filed that wa previously empty to an existing bank account
        return (
          <Label>
            Added {additionalField} {field} <LabelValue>{to}</LabelValue>
          </Label>
        );
      } else if (action === "remove" && !to && field !== "document") {
        //complete removal of bank account with 'name' and 'field'
        return (
          <Label>
            Deleted bank account <LabelValue>{additionalField}</LabelValue> with
            number <LabelValue>{from}</LabelValue>
          </Label>
        );
      } else Iif (action === "remove" && field !== "document") {
        //deletion of data drom one field of a bank account
        return (
          <Label>
            Removed {additionalField} {field} <LabelValue>{to}</LabelValue>
          </Label>
        );
      } else {
        //document upload for bank account
        const addOrRemove = action === "remove" ? "Deleted" : "Added";
        const additionalText =
          field === "document" ? "in the bank account section" : "";
        return (
          <Label>
            {addOrRemove} <LabelValue>{to}</LabelValue>{" "}
            {formatFieldLabel(field, group)} {additionalText}
          </Label>
        );
      }
    }
    case "Business Owner": {
      //addition of business owner
      Iif (action === "insert" && field === "full name") {
        return (
          <Label>
            Added <LabelValue>{to}</LabelValue> business owner
          </Label>
        );
      }
      if (action === "add") {
        const startText = additionalField
          ? `Added ${additionalField} ${field} `
          : "Added ";
        return (
          <Label>
            {startText}
            <LabelValue>{to}</LabelValue> in business owner
          </Label>
        );
      } else if (action === "remove" && !to)
        //deletion of business owner
        return (
          <Label>
            Removed <LabelValue>{additionalField}</LabelValue> in business owner
          </Label>
        );
      else if (action === "update") {
        //update of a business owner
        return (
          <ChangedLabel
            field={
              !additionalField || field === "full name"
                ? field
                : `${additionalField} ${field}`
            }
            to={to}
            from={from}
          />
        );
      } else {
        //addition or removal of data from business owner field
        const isRemove = action === "remove";
        return (
          <AddOrRemoveLabel
            field={`${additionalField} ${field}`}
            remove={isRemove}
            switchOrder
            to={isRemove ? from : to}
          />
        );
      }
    }
    case "Documents": {
      const addOrDelete = action === "remove" ? "Deleted" : "Added";
      const value = action === "remove" ? from : to;
      const customSx = {
        sx: {
          wordBreak: "break-all",
        },
      };
      return (
        <Label {...customSx}>
          {addOrDelete} <LabelValue {...customSx}>{value}</LabelValue>
        </Label>
      );
    }
    default:
      return <></>;
  }
};
 
const ChangedLabel = ({ from, to, field }: Partial<BuildDescriptionArgs>) => (
  <Label>
    Changed {formatFieldLabel(field)} from <LabelValue>{from}</LabelValue> to{" "}
    <LabelValue to>{to}</LabelValue>
  </Label>
);
 
const AddOrRemoveLabel = ({
  to,
  field,
  switchOrder,
  remove,
  insert,
}: Partial<BuildDescriptionArgs> & {
  switchOrder?: boolean;
  remove?: boolean;
  insert?: boolean;
}) => {
  const addOrRemove = insert ? "Updated" : remove ? "Removed" : "Added";
 
  if (switchOrder) {
    return (
      <Label>
        {addOrRemove} <LabelValue>{to}</LabelValue> in {formatFieldLabel(field)}
      </Label>
    );
  } else
    return (
      <Label>
        {addOrRemove} {formatFieldLabel(field)}{" "}
        {!!to && <LabelValue>{to}</LabelValue>}
      </Label>
    );
};