Skip to content

Commit

Permalink
feat #1369: The Web Components Config API should store config by appId
Browse files Browse the repository at this point in the history
  • Loading branch information
undefined committed May 28, 2020
1 parent 317d47d commit 3f66d73
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validateGetById, validateCreate, validateUpdate, validateDelete, valida

describe('validateGetById', () => {
it('should return correctly', () => {
const result = validateGetById({ customerId: 'id1' })
const result = validateGetById({ customerId: 'id1', appId: 'id1' })
expect(result).toBe(true)
})

Expand Down Expand Up @@ -38,6 +38,7 @@ describe('validateCreate', () => {
it('should return correctly', () => {
const result = validateCreate({
customerId: 'id1',
appId: 'id1',
appointmentLength: 10,
appointmentTimeGap: 15,
appointmentTypes: ['type1', 'type2'],
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('validateCreate', () => {
expect(() => {
validateCreate({
customerId: 'id1',
appId: 'id1',
appointmentLength: 10,
appointmentTimeGap: 15,
appointmentTypes: ['type1', 'type2'],
Expand All @@ -95,7 +97,7 @@ describe('validateCreate', () => {

describe('validateUpdate', () => {
it('should return correctly', () => {
const result = validateUpdate({ customerId: 'id1', appointmentLength: 20 })
const result = validateUpdate({ customerId: 'id1', appId: 'id1', appointmentLength: 20 })
expect(result).toBe(true)
})

Expand All @@ -122,14 +124,14 @@ describe('validateUpdate', () => {
error.message = 'Invalid daysOfWeek.'
error.code = '400'
expect(() => {
validateUpdate({ customerId: 'id1', appointmentLength: 20, daysOfWeek: [9] })
validateUpdate({ customerId: 'id1', appId: 'id1', appointmentLength: 20, daysOfWeek: [9] })
}).toThrowError(error)
})
})

describe('validateDelete', () => {
it('should return correctly', () => {
const result = validateDelete({ customerId: 'id1' })
const result = validateDelete({ customerId: 'id1', appId: 'id1' })
expect(result).toBe(true)
})

Expand All @@ -150,14 +152,6 @@ describe('validateDelete', () => {
validateDelete({ customerId: 'id1', invalidParam: 'param' })
}).toThrowError(error)
})
it('should throw error with invalid schema', () => {
const error: NodeJS.ErrnoException = new Error()
error.message = 'Invalid daysOfWeek.'
error.code = '400'
expect(() => {
validateDelete({ customerId: 'id1', daysOfWeek: [9] })
}).toThrowError(error)
})
})

describe('validateFollowSchema', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AppRequest, AppResponse } from '../../../app'
import {
webComponentsConfigGetByIdHandler,
webComponentsConfigPutHandler,
webComponentsConfigPatchHandler,
webComponentsConfigDeleteHandler,
} from '../web-components-config'
Expand Down Expand Up @@ -42,31 +41,6 @@ describe('web-components-config', () => {
})
})

describe('webComponentsConfigPutHandler', () => {
it('should run correctly and return service is running', async done => {
const mockRequest = ({
traceId: 'mockTraceId',
headers: {},
body: webComponentConfig,
params: {
clientId: '1',
traceId: 'mockUUID',
},
} as unknown) as AppRequest
const mockResponse = ({
send: jest.fn().mockReturnValue({}),
status: jest.fn().mockReturnValue(200),
sendStatus: jest.fn().mockReturnValue(200),
json: jest.fn().mockReturnValue({}),
} as unknown) as AppResponse
await webComponentsConfigPutHandler(mockRequest, mockResponse)
setTimeout(() => {
expect(mockResponse.send).toBeCalledWith(webComponentConfig)
done()
}, 1000)
})
})

describe('webComponentsConfigPatchHandler', () => {
it('should run correctly and return service is running', async done => {
const mockRequest = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const getConfigByClientId = async ({ traceId, data }: GetByClientIdParams
export const createConfig = async ({ traceId, data }: CreateParams): Promise<WebComponentConfig> => {
try {
logger.info('Creating config...', { traceId, data })

const itemToCreate = generateSchemaItem(data)
const result = await dynamoDBMapper.put(itemToCreate)
logger.info('Create config successfully', { traceId, result })
Expand Down Expand Up @@ -48,7 +49,7 @@ export const updateConfig = async ({ traceId, data }: UpdateParams): Promise<Web
export const deleteConfig = async ({ traceId, data }: DeleteParams): Promise<WebComponentConfig> => {
try {
logger.info('Deleting config...', { traceId, data })
const itemToDelete = generateSchemaItem(data)
const itemToDelete = generateSchemaItem({ appId: data.customerId })
const result = await dynamoDBMapper.delete(itemToDelete)
logger.info('Delete config successfully', { traceId, result })
return result
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
export const ALL_PARAMS = [
'customerId',
'appId',
'appointmentLength',
'appointmentTimeGap',
'appointmentTypes',
'negotiatorIds',
'daysOfWeek',
]
export const GET_BY_ID_REQUIRED_PARAMS = ['customerId']
export const GET_BY_ID_REQUIRED_PARAMS = ['customerId', 'appId']
export const CREATE_REQUIRED_PARAMS = [
'customerId',
'appId',
'appointmentLength',
'appointmentTimeGap',
'appointmentTypes',
'negotiatorIds',
'daysOfWeek',
]
export const UPDATE_REQUIRED_PARAMS = ['customerId']
export const DELETE_REQUIRED_PARAMS = ['customerId']
export const UPDATE_REQUIRED_PARAMS = ['customerId', 'appId']
export const DELETE_REQUIRED_PARAMS = ['customerId', 'appId']

/**
* Used to validate if the data object follow our schema type
Expand Down Expand Up @@ -124,21 +126,13 @@ export const validateDelete = (data: { [key: string]: any }) => {
// check if param keys are valid
const isParamsValid =
dataKeys.some(key => GET_BY_ID_REQUIRED_PARAMS.includes(key)) && dataKeys.every(key => ALL_PARAMS.includes(key))
// check if param is a valid schema item
const errorMessage = validateFollowSchema(data)

if (!isParamsValid) {
const error: NodeJS.ErrnoException = new Error()
error.message = 'Invalid params'
error.code = '400'
throw error
}
if (errorMessage) {
const error: NodeJS.ErrnoException = new Error()
error.message = errorMessage
error.code = '400'
throw error
}

return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,10 @@ export const webComponentsConfigGetByIdHandler = async (req: AppRequest, res: Ap
}
}

export const webComponentsConfigPutHandler = async (req: AppRequest, res: AppResponse) => {
try {
const params = { traceId: req.traceId, data: req.body }
const { data } = params
validateCreate(data)
const result = await createConfig(params)
return res.send(result)
} catch (err) {
logger.error('webComponentsConfig.put', { traceId: req.traceId, error: JSON.stringify(err) })
return res.send({ message: err.message, code: err.code, traceId: req.traceId })
}
}

export const webComponentsConfigPatchHandler = async (req: AppRequest, res: AppResponse) => {
try {
const params = {
data: { customerId: req.params.customerId, ...req.body },
data: { customerId: req.params.customerId, appId: req.params.appId, ...req.body },
traceId: req.traceId,
}
const { data } = params
Expand All @@ -54,7 +41,7 @@ export const webComponentsConfigPatchHandler = async (req: AppRequest, res: AppR
export const webComponentsConfigDeleteHandler = async (req: AppRequest, res: AppResponse) => {
try {
const params = {
data: { customerId: req.params.customerId },
data: { customerId: req.params.customerId, appId: req.params.appId },
traceId: req.traceId,
}
const { data } = params
Expand All @@ -67,9 +54,26 @@ export const webComponentsConfigDeleteHandler = async (req: AppRequest, res: App
}
}

webComponentsConfig.get('/:customerId', webComponentsConfigGetByIdHandler)
webComponentsConfig.put('/', webComponentsConfigPutHandler)
webComponentsConfig.patch('/:customerId', webComponentsConfigPatchHandler)
webComponentsConfig.delete('/:customerId', webComponentsConfigDeleteHandler)
export const webComponentsConfigPostHandler = async (req: AppRequest, res: AppResponse) => {
try {
const params = {
data: { ...req.body, customerId: req.params.customerId, appId: req.params.appId },
traceId: req.traceId,
}
const { data } = params

validateCreate(data)
const result = await createConfig(params)
return res.send(result)
} catch (err) {
// logger.error('webComponentsConfig.create', { traceId: req.traceId, error: JSON.stringify(err) })
return res.send({ message: err.message, code: err.code, traceId: req.traceId })
}
}

webComponentsConfig.post('/:customerId/:appId', webComponentsConfigPostHandler)
webComponentsConfig.get('/:customerId/:appId', webComponentsConfigGetByIdHandler)
webComponentsConfig.delete('/:customerId/:appId', webComponentsConfigDeleteHandler)
webComponentsConfig.patch('/:customerId/:appId', webComponentsConfigPatchHandler)

export default webComponentsConfig
3 changes: 3 additions & 0 deletions packages/web-components-config-server/src/schemas/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class WebComponentConfig {
@hashKey()
customerId: string

@hashKey()
appId: string

@attribute()
appointmentLength: number

Expand Down

0 comments on commit 3f66d73

Please sign in to comment.