Skip to content

Commit

Permalink
chore: #1222 Update env variables for appointment planner (#1469)
Browse files Browse the repository at this point in the history
* chore: #1222 Update env variables for appointment planner
trankhacvy authored Jun 1, 2020
1 parent 4d22d2b commit adc4a8a
Showing 4 changed files with 22 additions and 74 deletions.
12 changes: 11 additions & 1 deletion packages/web-components/scripts/start-dev.js
Original file line number Diff line number Diff line change
@@ -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'))
Original file line number Diff line number Diff line change
@@ -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)
})
})
Original file line number Diff line number Diff line change
@@ -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<PagedResultAppointmentModel_, undefined>({
url: String(url),
headers,
})

if (response) {
res.status(200)
res.json(response)
res.end()
}
} catch (err) {
errorHandler(err, res)
}
res.status(200)
res.end()
}
Original file line number Diff line number Diff line change
@@ -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:

0 comments on commit adc4a8a

Please sign in to comment.