Skip to content

Commit

Permalink
Merge pull request #67 from awell-health/et-367-prevent-duplicate-api…
Browse files Browse the repository at this point in the history
…-calls-in-the-scheduling-front-end

chore(): minor improvements
  • Loading branch information
nckhell authored Nov 14, 2024
2 parents 34966e2 + 52a16d8 commit 6710de7
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@awell-health/sol-scheduling",
"version": "0.3.16",
"version": "0.3.17",
"packageManager": "[email protected]",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/SolApiProvider/SolApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ export const SolApiProvider: FC<ContextProps> = ({
preparePreferencesForSalesforce(preferences);
completeActivity(slot, parsedPreferencesForSalesforce);
})
.catch(() => {
console.error('Error booking appointment');
.catch((error) => {
console.error('Error booking appointment', error);
onError();
})
.finally(() => setIsBooking(false));
Expand Down
15 changes: 6 additions & 9 deletions src/hostedPages/SchedulingActivity.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { SchedulingActivity as SchedulingActivityComponent } from './SchedulingActivity';
import { fn } from '@storybook/test';
import {
mockFetchProvidersFn,
mockProviderAvailabilityResponse,
mockProviderResponse,
mockProvidersResponse
Expand All @@ -24,10 +23,6 @@ import {
} from '../lib/api';
import { some } from 'lodash-es';
import { type SalesforcePreferencesType } from '@/lib/utils/preferences';
import {
fetchAvailabilityMock,
fetchProviderMock
} from '@/molecules/Scheduler/__mocks__/scheduler.mocks';

const meta: Meta<typeof SchedulingActivityComponent> = {
title: 'HostedPages/SchedulingActivity/Full',
Expand All @@ -37,12 +32,12 @@ const meta: Meta<typeof SchedulingActivityComponent> = {
},
args: {
onCompleteActivity: fn(),
fetchProvider: fetchProviderMock,
fetchProviders: mockFetchProvidersFn,
fetchAvailability: fetchAvailabilityMock,
fetchProvider: fn(),
fetchProviders: fn(),
fetchAvailability: fn(),
onBooking: fn(),
providerPreferences: {
age: '18-65',
age: '18',
clinicalFocus: []
} satisfies GetProvidersInputType
},
Expand Down Expand Up @@ -127,6 +122,7 @@ export const Full: Story = {
);

const fetchAvailabilityFn = useCallback(async (_providerId: string) => {
// Spy on the action, useful for debugging
args.fetchAvailability(_providerId);

const data = (await new Promise((resolve) =>
Expand All @@ -141,6 +137,7 @@ export const Full: Story = {

const bookAppointmentFn = useCallback(
async (_slot: SlotWithConfirmedLocation) => {
// Spy on the action, useful for debugging
args.onBooking(_slot);

const data = new Promise((resolve) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const SkipProvider: Story = {
);

const fetchAvailabilityFn = useCallback(async (_providerId: string) => {
// Spy on the action, useful for debugging
args.fetchAvailability(_providerId);

const data = (await new Promise((resolve) =>
Expand All @@ -133,6 +134,7 @@ export const SkipProvider: Story = {

const bookAppointmentFn = useCallback(
async (_slot: SlotWithConfirmedLocation) => {
// Spy on the action, useful for debugging
args.onBooking(_slot);

const data = new Promise((resolve) =>
Expand Down
45 changes: 30 additions & 15 deletions src/lib/api/__mocks__/GetProviderAvailability.mock.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// import { addDays, isSaturday, isSunday } from 'date-fns';
import { addDays, isSaturday, isSunday, setHours } from 'date-fns';
import { GetAvailabilitiesResponseType } from '..';
import * as schema from '../schema/GetProviderAvailability.schema';

// const getNextWeekday = (date: Date): Date => {
// if (isSaturday(date)) {
// return addDays(date, 2); // If Saturday, move to Monday
// }
// if (isSunday(date)) {
// return addDays(date, 1); // If Sunday, move to Monday
// }
// return date; // Otherwise, return the same date
// };
const getNextWeekday = (date: Date): Date => {
if (isSaturday(date)) {
return addDays(date, 2); // If Saturday, move to Monday
}
if (isSunday(date)) {
return addDays(date, 1); // If Sunday, move to Monday
}
return date; // Otherwise, return the same date
};

export const mockProviderAvailabilityResponse: (
pid: string
Expand All @@ -19,7 +19,10 @@ export const mockProviderAvailabilityResponse: (
[pid]: [
{
eventId: 't68403en62hji9lad095mv2srk',
slotstart: '2024-10-17T21:00:00Z',
slotstart: setHours(
addDays(getNextWeekday(new Date()), 0).toISOString(),
10
),
provider: {
location: {
facility: '',
Expand All @@ -35,7 +38,10 @@ export const mockProviderAvailabilityResponse: (
},
{
eventId: 'jnkmeiuv63uimngvjdb6ja5iro',
slotstart: '2024-10-18T14:00:00Z',
slotstart: setHours(
addDays(getNextWeekday(new Date()), 0).toISOString(),
11
),
provider: {
location: {
facility: '',
Expand All @@ -51,7 +57,10 @@ export const mockProviderAvailabilityResponse: (
},
{
eventId: '981t7pvlkc96vjntovihr16h9g',
slotstart: '2024-10-21T18:00:00Z',
slotstart: setHours(
addDays(getNextWeekday(new Date()), 1).toISOString(),
15
),
provider: {
location: {
facility: '',
Expand All @@ -67,7 +76,10 @@ export const mockProviderAvailabilityResponse: (
},
{
eventId: 'u2dtb7c5vff90jv5s46b3hake0',
slotstart: '2024-10-21T19:00:00Z',
slotstart: setHours(
addDays(getNextWeekday(new Date()), 2).toISOString(),
18
),
provider: {
location: {
facility: '',
Expand All @@ -83,7 +95,10 @@ export const mockProviderAvailabilityResponse: (
},
{
eventId: 'isber21m91efnlqduai88kt1h8',
slotstart: '2024-10-22T15:00:00Z',
slotstart: setHours(
addDays(getNextWeekday(new Date()), 2).toISOString(),
16
),
provider: {
location: {
facility: '',
Expand Down
97 changes: 92 additions & 5 deletions src/molecules/Scheduler/__mocks__/scheduler.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import {
type GetProviderResponseType,
type SlotWithConfirmedLocation
} from '@/lib/api';
import {
mockProviderAvailabilityResponse,
mockProviderResponse
} from '@/lib/api/__mocks__';
import { mockProviderResponse } from '@/lib/api/__mocks__';
import { SalesforcePreferencesType } from '@/lib/utils/preferences';
import { action } from '@storybook/addon-actions';
import { fn } from '@storybook/test';
import * as schema from '../../../lib/api/schema';

export const fetchProviderMock = fn(async (pid: string) => {
// Track the call using Storybook's action
Expand All @@ -26,7 +24,96 @@ export const fetchAvailabilityMock = fn(async (pid: string) => {
// Track the call using Storybook's action
action('fetchAvailability')(pid);

return Promise.resolve(mockProviderAvailabilityResponse(pid));
/**
* Returning hardcoded data for the sake of the storybook and to avoid flaky tests.
* The current date is mocked to be 2024-10-14
*/
return Promise.resolve({
data: {
[pid]: [
{
eventId: 't68403en62hji9lad095mv2srk',
slotstart: '2024-10-17T21:00:00Z',
provider: {
location: {
facility: '',
state: ''
}
},
providerId: '1717',
date: '2024-10-17',
duration: 60,
booked: false,
facility: 'NY - Brooklyn Heights',
location: 'Telehealth'
},
{
eventId: 'jnkmeiuv63uimngvjdb6ja5iro',
slotstart: '2024-10-18T14:00:00Z',
provider: {
location: {
facility: '',
state: ''
}
},
providerId: '1717',
date: '2024-10-18',
duration: 60,
booked: false,
facility: 'NY - Brooklyn Heights',
location: 'In-Person'
},
{
eventId: '981t7pvlkc96vjntovihr16h9g',
slotstart: '2024-10-21T18:00:00Z',
provider: {
location: {
facility: '',
state: ''
}
},
providerId: '1717',
date: '2024-10-21',
duration: 60,
booked: false,
facility: 'NY - Brooklyn Heights',
location: 'Telehealth'
},
{
eventId: 'u2dtb7c5vff90jv5s46b3hake0',
slotstart: '2024-10-21T19:00:00Z',
provider: {
location: {
facility: '',
state: ''
}
},
providerId: '1717',
date: '2024-10-21',
duration: 60,
booked: false,
facility: 'NY - Brooklyn Heights',
location: 'In-Person'
},
{
eventId: 'isber21m91efnlqduai88kt1h8',
slotstart: '2024-10-22T15:00:00Z',
provider: {
location: {
facility: '',
state: ''
}
},
providerId: '1717',
date: '2024-10-22',
duration: 60,
booked: false,
facility: 'NY - Brooklyn Heights',
location: 'Telehealth'
}
].map((e) => schema.Event.parse(e))
}
});
});

export const bookAppointmentMock = fn(
Expand Down

0 comments on commit 6710de7

Please sign in to comment.