Skip to content

Commit

Permalink
Merge branch 'main' into NDT-502-CCBC-Summary-Page-Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RRanath committed Aug 30, 2024
2 parents 4ebd880 + 3d2e88b commit 7491012
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [1.187.0](https://github.com/bcgov/CONN-CCBC-portal/compare/v1.186.4...v1.187.0) (2024-08-29)

### Features

- add toast for second review emails ([88b51d4](https://github.com/bcgov/CONN-CCBC-portal/commit/88b51d486927507285ac311a3e4755a77fd10b59))

## [1.186.4](https://github.com/bcgov/CONN-CCBC-portal/compare/v1.186.3...v1.186.4) (2024-08-29)

## [1.186.3](https://github.com/bcgov/CONN-CCBC-portal/compare/v1.186.2...v1.186.3) (2024-08-28)
Expand Down
84 changes: 59 additions & 25 deletions app/components/Analyst/Assessments/AssessmentsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useCreateAssessmentMutation } from 'schema/mutations/assessment/createA
import assessmentsUiSchema from 'formSchema/uiSchema/analyst/assessmentsUiSchema';
import { RJSFSchema } from '@rjsf/utils';
import * as Sentry from '@sentry/nextjs';
import { useToast } from 'components/AppProvider';

interface Props {
addedContext?: any;
Expand All @@ -34,6 +35,12 @@ const StyledFormBase = styled(FormBase)`
}
`;

const StyledNotifyButton = styled(Button)`
svg {
height: 18px;
}
`;

const AssessmentsForm: React.FC<Props> = ({
addedContext,
formData,
Expand All @@ -52,9 +59,14 @@ const AssessmentsForm: React.FC<Props> = ({
query.applicationByRowId
);

const { showToast, hideToast } = useToast();

const [createAssessment, isCreating] = useCreateAssessmentMutation();
const [newFormData, setNewFormData] = useState(formData);
const [isFormSaved, setIsFormSaved] = useState(true);
const [emailStatus, setEmailStatus] = useState<
'idle' | 'inProgress' | 'sent'
>('idle');

const handleSubmit = async (e: IChangeEvent<any>) => {
if (!isFormSaved) {
Expand All @@ -69,31 +81,6 @@ const AssessmentsForm: React.FC<Props> = ({
},
onCompleted: () => {
setIsFormSaved(true);
if (
e.formData?.nextStep === 'Needs 2nd review' &&
e.formData?.nextStep !== formData?.nextStep
) {
fetch('/api/email/notifySecondReviewRequest', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
applicationId: queryFragment.rowId,
host: window.location.origin,
ccbcNumber: addedContext?.ccbcNumber,
assessmentType: slug,
}),
}).then((response) => {
if (!response.ok) {
Sentry.captureException({
name: 'Email sending failed',
message: response,
});
}
return response.json();
});
}
},
optimisticResponse: {
jsonData: e.formData,
Expand All @@ -110,6 +97,42 @@ const AssessmentsForm: React.FC<Props> = ({
}
};

const notifyByEmail = async () => {
hideToast();
if (newFormData?.nextStep === 'Needs 2nd review') {
setEmailStatus('inProgress');
fetch('/api/email/notifySecondReviewRequest', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
applicationId: queryFragment.rowId,
host: window.location.origin,
ccbcNumber: addedContext?.ccbcNumber,
assessmentType: slug,
}),
}).then((response) => {
if (!response.ok) {
showToast(
'Email notification did not work, please try again',
'error',
5000
);
Sentry.captureException({
name: 'Email sending failed',
message: response,
});
setEmailStatus('idle');
} else {
showToast('Email notification sent successfully', 'success', 5000);
setEmailStatus('sent');
}
return response.json();
});
}
};

return (
<StyledFormBase
schema={schema}
Expand All @@ -130,6 +153,17 @@ const AssessmentsForm: React.FC<Props> = ({
<Button variant="primary" disabled={isCreating}>
{!isFormSaved ? 'Save' : 'Saved'}
</Button>
&nbsp;
<StyledNotifyButton
variant="primary"
disabled={
newFormData?.nextStep !== 'Needs 2nd review' || emailStatus !== 'idle'
}
title="Email notification of 2nd review needed will be sent to Mike and Karina"
onClick={notifyByEmail}
>
{emailStatus === 'inProgress' && <LoadingSpinner />} Notify by email
</StyledNotifyButton>
{isCreating && <LoadingSpinner />}
</StyledFormBase>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,22 @@ describe('The index page', () => {
});
});

it('Trigger email only when Next Step is Needs 2nd Review once save successful', async () => {
it('Trigger email when notify by email button clicked', async () => {
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: jest.fn() });
pageTestingHelper.loadQuery();
pageTestingHelper.renderPage();

const notifyByEmailButton = screen.getByRole('button', {
name: 'Notify by email',
});

expect(notifyByEmailButton).toBeDisabled();

await userEvent.click(screen.getByLabelText('Needs 2nd review'));

await userEvent.click(screen.getByRole('button', { name: 'Save' }));
expect(notifyByEmailButton).toBeEnabled();

await userEvent.click(notifyByEmailButton);

pageTestingHelper.expectMutationToBeCalled('createAssessmentMutation', {
input: {
Expand Down Expand Up @@ -225,6 +233,69 @@ describe('The index page', () => {
});

expect(global.fetch).toHaveBeenCalled();

expect(
screen.getByText('Email notification sent successfully')
).toBeVisible();

expect(notifyByEmailButton).toBeDisabled();
});

it('shows a toast when email notification fails', async () => {
global.fetch = jest.fn().mockResolvedValue({ ok: false, json: jest.fn() });
pageTestingHelper.loadQuery();
pageTestingHelper.renderPage();

const notifyByEmailButton = screen.getByRole('button', {
name: 'Notify by email',
});

expect(notifyByEmailButton).toBeDisabled();

await userEvent.click(screen.getByLabelText('Needs 2nd review'));

expect(notifyByEmailButton).toBeEnabled();

await userEvent.click(notifyByEmailButton);

pageTestingHelper.expectMutationToBeCalled('createAssessmentMutation', {
input: {
_applicationId: 1,
_jsonData: {
nextStep: 'Needs 2nd review',
decision: 'No decision',
contestingMap: [],
},
_assessmentType: 'screening',
},
connections: [],
});

await act(async () => {
pageTestingHelper.environment.mock.resolveMostRecentOperation({
data: {
createAssessmentForm: {
assessmentData: {
id: 'WyJhc3Nlc3NtZW50X2RhdGEiLDIxXQ==',
rowId: 1,
jsonData: {
nextStep: 'Needs 2nd review',
decision: 'No decision',
contestingMap: [],
},
},
},
},
});
});

expect(global.fetch).toHaveBeenCalled();

expect(
screen.getByText('Email notification did not work, please try again')
).toBeVisible();

expect(notifyByEmailButton).toBeEnabled();
});

it('Displays unavailable Assigned To value for lead', () => {
Expand Down
1 change: 1 addition & 0 deletions db/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,4 @@ mutations/edit_cbc_project_communities 2024-08-21T15:16:56Z Anthony Bushara <ant
tables/communities_source_data_001_service_account 2024-08-28T16:32:48Z Rafael Solorzano <61289255+rafasdc@users.noreply.github.com> # grants service account to communities source data table
@1.186.3 2024-08-28T17:01:51Z CCBC Service Account <ccbc@button.is> # release v1.186.3
@1.186.4 2024-08-29T17:49:49Z CCBC Service Account <ccbc@button.is> # release v1.186.4
@1.187.0 2024-08-29T20:15:20Z CCBC Service Account <ccbc@button.is> # release v1.187.0
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CONN-CCBC-portal",
"version": "1.186.4",
"version": "1.187.0",
"main": "index.js",
"repository": "https://github.com/bcgov/CONN-CCBC-portal.git",
"author": "Romer, Meherzad CITZ:EX <Meherzad.Romer@gov.bc.ca>",
Expand Down

0 comments on commit 7491012

Please sign in to comment.