Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPDX-7417 - fix phone numbers - confirm all #992

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 66 additions & 22 deletions src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import { ApolloCache, InMemoryCache } from '@apollo/client';
import { ThemeProvider } from '@mui/material/styles';
import userEvent from '@testing-library/user-event';
import { ErgonoMockShape } from 'graphql-ergonomock';
import { SnackbarProvider } from 'notistack';
import TestRouter from '__tests__/util/TestRouter';
import TestWrapper from '__tests__/util/TestWrapper';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
Expand Down Expand Up @@ -73,30 +75,46 @@ const testData: ErgonoMockShape[] = [
},
];

const Components: React.FC<{ data?: ErgonoMockShape[] }> = ({
data = testData,
}) => (
const mockEnqueue = jest.fn();
jest.mock('notistack', () => ({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...jest.requireActual('notistack'),
useSnackbar: () => {
return {
enqueueSnackbar: mockEnqueue,
};
},
}));

const Components: React.FC<{
data?: ErgonoMockShape[];
cache?: ApolloCache<object>;
}> = ({ data = testData, cache }) => (
<ThemeProvider theme={theme}>
<TestRouter router={router}>
<TestWrapper>
<GqlMockedProvider<{
GetInvalidPhoneNumbers: GetInvalidPhoneNumbersQuery;
}>
mocks={{
GetInvalidPhoneNumbers: {
people: {
nodes: data,
<SnackbarProvider>
<TestRouter router={router}>
<TestWrapper>
<GqlMockedProvider<{
GetInvalidPhoneNumbers: GetInvalidPhoneNumbersQuery;
}>
mocks={{
GetInvalidPhoneNumbers: {
people: {
nodes: data,
},
},
},
}}
>
<FixPhoneNumbers
accountListId={accountListId}
setContactFocus={setContactFocus}
/>
</GqlMockedProvider>
</TestWrapper>
</TestRouter>
}}
cache={cache}
>
<FixPhoneNumbers
accountListId={accountListId}
setContactFocus={setContactFocus}
/>
</GqlMockedProvider>
</TestWrapper>
</TestRouter>
</SnackbarProvider>
</ThemeProvider>
);

Expand Down Expand Up @@ -214,4 +232,30 @@ describe('FixPhoneNumbers-Home', () => {
).not.toBeInTheDocument();
});
});
it('should bulk confirm all phone numbers', async () => {
const cache = new InMemoryCache();

const { getByTestId, queryByTestId, getByText } = render(
<Components cache={cache} />,
);
await waitFor(() => {
expect(queryByTestId('loading')).not.toBeInTheDocument();
expect(getByTestId('starOutlineIcon-testid-1')).toBeInTheDocument();
});

userEvent.click(getByTestId(`starOutlineIcon-testid-1`));

const confirmAllButton = getByTestId('source-button');
userEvent.click(confirmAllButton);

await waitFor(() => {
expect(mockEnqueue).toHaveBeenCalledWith(`Phone numbers updated!`, {
variant: 'success',
autoHideDuration: 7000,
});
expect(
getByText('No people with phone numbers need attention'),
).toBeVisible();
});
});
});
95 changes: 67 additions & 28 deletions src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useApolloClient } from '@apollo/client';
import { mdiCheckboxMarkedCircle } from '@mdi/js';
import Icon from '@mdi/react';
import {
Expand All @@ -15,17 +14,16 @@
import { Trans, useTranslation } from 'react-i18next';
import { makeStyles } from 'tss-react/mui';
import { SetContactFocus } from 'pages/accountLists/[accountListId]/tools/useToolsHelper';
import { PersonPhoneNumberInput } from 'src/graphql/types.generated';
import {
PersonPhoneNumberInput,
PersonUpdateInput,
} from 'src/graphql/types.generated';
import theme from '../../../theme';
import NoData from '../NoData';
import { StyledInput } from '../StyledInput';
import Contact from './Contact';
import DeleteModal from './DeleteModal';
import {
GetInvalidPhoneNumbersDocument,
GetInvalidPhoneNumbersQuery,
useGetInvalidPhoneNumbersQuery,
} from './GetInvalidPhoneNumbers.generated';
import { useGetInvalidPhoneNumbersQuery } from './GetInvalidPhoneNumbers.generated';
import { useUpdateInvalidPhoneNumbersMutation } from './UpdateInvalidPhoneNumbers.generated';

const useStyles = makeStyles()(() => ({
Expand Down Expand Up @@ -131,7 +129,6 @@
}: Props) => {
const { classes } = useStyles();
const { enqueueSnackbar } = useSnackbar();
const client = useApolloClient();
const [defaultSource, setDefaultSource] = useState('MPDX');
const [deleteModalState, setDeleteModalState] = useState<ModalState>(
defaultDeleteModalState,
Expand Down Expand Up @@ -173,6 +170,34 @@
[loading],
);

const determineBulkDataToSend = (
dataState: { [key: string]: PersonPhoneNumbers },
defaultSource: string,
): PersonUpdateInput[] => {
const dataToSend = [] as PersonUpdateInput[];

Object.entries(dataState).forEach((value) => {
const primaryNumber = value[1].phoneNumbers.find(
(number) => number.source === defaultSource,
);
if (primaryNumber) {
dataToSend.push({
id: value[0],
phoneNumbers: value[1].phoneNumbers.map(
(number) =>
({
id: number.id,
primary: number.id === primaryNumber.id,
number: number.number,
validValues: true,
} as PersonPhoneNumberInput),
),
});
}
});
return dataToSend;
};

const handleDeleteModalOpen = (
personId: string,
numberIndex: number,
Expand Down Expand Up @@ -256,6 +281,7 @@
id: phoneNumber.id,
primary: phoneNumber.primary,
number: phoneNumber.number,
validValues: true,
})),
id: personId,
},
Expand All @@ -267,6 +293,9 @@
attributes,
},
},
update: (cache) => {
cache.evict({ id: `Person:${personId}` });
},
onError() {
enqueueSnackbar(t(`Error updating ${name}'s phone numbers`), {
variant: 'error',
Expand All @@ -278,33 +307,42 @@
variant: 'success',
autoHideDuration: 7000,
});
hideContactFromView(personId);
},
});
};

const hideContactFromView = (hideId: string): void => {
const query = {
query: GetInvalidPhoneNumbersDocument,
variables: {
accountListId,
},
};
const handleBulkConfirm = async () => {
const dataToSend = determineBulkDataToSend(dataState, defaultSource ?? '');

const dataFromCache = client.readQuery<GetInvalidPhoneNumbersQuery>(query);
if (!dataToSend.length) {
return;

Check warning on line 318 in src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx#L318

Added line #L318 was not covered by tests
}

if (dataFromCache) {
const data = {
...dataFromCache,
people: {
...dataFromCache.people,
nodes: dataFromCache.people.nodes.filter(
(person) => person.id !== hideId,
),
await updateInvalidPhoneNumbers({
variables: {
input: {
accountListId,
attributes: dataToSend,
},
};
client.writeQuery({ ...query, data });
}
},
update: (cache) => {
data?.people.nodes.forEach((person) => {
cache.evict({ id: `Person:${person.id}` });
});
},
onError: () => {
enqueueSnackbar(t(`Error updating phone numbers`), {

Check warning on line 334 in src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/FixPhoneNumbers/FixPhoneNumbers.tsx#L333-L334

Added lines #L333 - L334 were not covered by tests
variant: 'error',
autoHideDuration: 7000,
});
},
onCompleted: () => {
enqueueSnackbar(t(`Phone numbers updated!`), {
variant: 'success',
autoHideDuration: 7000,
});
},
});
};

return (
Expand Down Expand Up @@ -361,6 +399,7 @@
</NativeSelect>
<Button
className={classes.buttonBlue}
onClick={handleBulkConfirm}
data-testid="source-button"
>
<Icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ mutation UpdateInvalidPhoneNumbers($input: PeopleUpdateMutationInput!) {
people {
phoneNumbers {
nodes {
source
number
source
id
primary
validValues
}
}
}
Expand Down
Loading