From adc4a8af8b7b4e08ecba940ca668be88f9d074b8 Mon Sep 17 00:00:00 2001 From: Khac Vy Date: Mon, 1 Jun 2020 17:51:38 +0700 Subject: [PATCH] chore: #1222 Update env variables for appointment planner (#1469) * chore: #1222 Update env variables for appointment planner --- packages/web-components/scripts/start-dev.js | 12 +++- .../api/__tests__/get-appointment-slots.ts | 59 +++---------------- .../server/api/get-appointment-slots.ts | 23 +------- .../server/serverless.yml | 2 + 4 files changed, 22 insertions(+), 74 deletions(-) diff --git a/packages/web-components/scripts/start-dev.js b/packages/web-components/scripts/start-dev.js index 8e680d0b8f..6cf966f315 100644 --- a/packages/web-components/scripts/start-dev.js +++ b/packages/web-components/scripts/start-dev.js @@ -28,6 +28,16 @@ const getStartClientServer = packageName => { } } +const getServerConfigFile = packageName => { + switch (packageName) { + case 'book-valuation-widget': + case 'book-viewing-widget': + return 'src/appointment-planner-component/server/serverless.yml' + default: + return `src/${packageName}/server/serverless.yml` + } +} + return (() => { const { execSync } = require('child_process') const [, , ...args] = process.argv @@ -42,7 +52,7 @@ return (() => { } // eslint-disable-next-line max-len const clientScript = `rollup -w -c './scripts/rollup.config.${packageName}.js' --environment APP_ENV:local` - const serverConfigFile = `src/${packageName}/server/serverless.yml` + const serverConfigFile = getServerConfigFile(packageName) const hasServer = fs.existsSync(serverConfigFile) // need to pass the apiKey into serverless offline for it to work locally const { WEB_COMPONENT_API_KEY_SEARCH_WIDGET } = require(path.resolve(__dirname, '../config.json')) diff --git a/packages/web-components/src/appointment-planner-component/server/api/__tests__/get-appointment-slots.ts b/packages/web-components/src/appointment-planner-component/server/api/__tests__/get-appointment-slots.ts index 64ad60a0ad..45b12afdc9 100644 --- a/packages/web-components/src/appointment-planner-component/server/api/__tests__/get-appointment-slots.ts +++ b/packages/web-components/src/appointment-planner-component/server/api/__tests__/get-appointment-slots.ts @@ -1,65 +1,20 @@ -import { fetcher } from '../../../../common/utils/fetcher-server' -import { appointmentsDataStub } from '../__stubs__/appointments' -import { getAppointmentSlots } from '../get-appointment-slots' -import { getServerHeaders } from '../../../../common/utils/get-server-headers' import { Request, Response } from 'express' -import { PACKAGE_SUFFIXES } from '../../../../common/utils/constants' -import { errorHandler } from '../../../../common/utils/error-handler' - -jest.mock('../../../../common/utils/fetcher-server') -jest.mock('../../../../common/utils/error-handler') - -describe('getAppointmentSlots server API', () => { - it('should correctly call the fetcher for getAppointmentSlots', async () => { - process.env.PLATFORM_API_BASE_URL = 'http://localhost:3000' - ;(fetcher as jest.Mock).mockImplementation(() => appointmentsDataStub) - - const req = { - url: '/appointments', - } as Request - - const res = ({ - status: jest.fn(), - json: jest.fn(), - end: jest.fn(), - } as unknown) as Response - - const headers = await getServerHeaders(req, PACKAGE_SUFFIXES.APPOINTMENT_PLANNER) - - await getAppointmentSlots(req, res) - - expect(fetcher).toHaveBeenCalledWith({ - url: `${process.env.PLATFORM_API_BASE_URL}${req.url}`, - headers, - }) - expect(res.status).toHaveBeenCalledWith(200) - expect(res.json).toHaveBeenCalledWith(appointmentsDataStub) - expect(res.end).toHaveBeenCalledTimes(1) - }) - - it('should correctly catch an error', async () => { - process.env.PLATFORM_API_BASE_URL = 'http://localhost:3000' - const error = new Error('Something went wrong') - ;(fetcher as jest.Mock).mockImplementation(() => { - throw error - }) +import { getAppointmentSlots } from '../get-appointment-slots' +describe('getAppointmentSlots endpoint', () => { + it('getAppointmentSlots', () => { const req = { url: '/appointments', } as Request - const res = ({ + const response = ({ status: jest.fn(), - json: jest.fn(), end: jest.fn(), } as unknown) as Response - await getAppointmentSlots(req, res) - - expect(errorHandler).toHaveBeenCalledWith(error, res) - }) + getAppointmentSlots(req, response) - afterEach(() => { - jest.resetAllMocks() + expect(response.status).toHaveBeenCalledWith(200) + expect(response.end).toHaveBeenCalledTimes(1) }) }) diff --git a/packages/web-components/src/appointment-planner-component/server/api/get-appointment-slots.ts b/packages/web-components/src/appointment-planner-component/server/api/get-appointment-slots.ts index 8210c3eeb9..d475ab37b9 100644 --- a/packages/web-components/src/appointment-planner-component/server/api/get-appointment-slots.ts +++ b/packages/web-components/src/appointment-planner-component/server/api/get-appointment-slots.ts @@ -1,25 +1,6 @@ import { Request, Response } from 'express' -import { fetcher } from '../../../common/utils/fetcher-server' -import { PagedResultAppointmentModel_ } from '@reapit/foundations-ts-definitions' -import { errorHandler } from '../../../common/utils/error-handler' -import { getServerHeaders } from '../../../common/utils/get-server-headers' -import { PACKAGE_SUFFIXES } from '../../../common/utils/constants' export const getAppointmentSlots = async (req: Request, res: Response) => { - try { - const headers = await getServerHeaders(req, PACKAGE_SUFFIXES.APPOINTMENT_PLANNER) - const url = new URL(`${process.env.PLATFORM_API_BASE_URL}${req.url}`) - const response = await fetcher({ - url: String(url), - headers, - }) - - if (response) { - res.status(200) - res.json(response) - res.end() - } - } catch (err) { - errorHandler(err, res) - } + res.status(200) + res.end() } diff --git a/packages/web-components/src/appointment-planner-component/server/serverless.yml b/packages/web-components/src/appointment-planner-component/server/serverless.yml index b31cf2faf5..67ce1dc846 100644 --- a/packages/web-components/src/appointment-planner-component/server/serverless.yml +++ b/packages/web-components/src/appointment-planner-component/server/serverless.yml @@ -40,6 +40,8 @@ provider: APP_ENV: ${self:custom.env.APP_ENV} PLATFORM_API_BASE_URL: ${self:custom.env.PLATFORM_API_BASE_URL} COGNITO_OAUTH_URL: ${self:custom.env.COGNITO_OAUTH_URL} + COGNITO_CLIENT_SECRET_APPOINTMENT_PLANNER: ${self:custom.env.COGNITO_CLIENT_SECRET_APPOINTMENT_PLANNER} + COGNITO_CLIENT_ID_APPOINTMENT_PLANNER: ${self:custom.env.COGNITO_CLIENT_ID_APPOINTMENT_PLANNER} package: individually: true include: