-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🚧 implement /tasks routes * 🚧 WIP * ✨ add route for creating follow up task * add logging * add task.controller tests --------- Co-authored-by: hafemann <[email protected]> Co-authored-by: Leona Kuse <[email protected]>
- Loading branch information
1 parent
5dcdeda
commit 461d583
Showing
7 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import { Response } from 'express'; | ||
import { BridgeRequest, FollowUpWithIntegrationEntities } from '../models'; | ||
import { TaskController } from './task.controller'; | ||
|
||
describe('Task Controller', () => { | ||
const mockAdapter = { | ||
getTasks: jest.fn(), | ||
findAllByQuery: jest.fn(), | ||
createFollowUp: jest.fn(), | ||
}; | ||
const mockNext = jest.fn(); | ||
|
||
describe('findAllByQuery', () => { | ||
beforeEach(() => jest.clearAllMocks()); | ||
|
||
it('Should check for providerConfig', async () => { | ||
const controller = new TaskController(mockAdapter); | ||
|
||
const result = await controller.findAllByQuery( | ||
{} as BridgeRequest<void>, | ||
{} as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should check if adapter.getTasks is implemented', async () => { | ||
const controller = new TaskController({}); | ||
|
||
const result = await controller.findAllByQuery( | ||
{} as BridgeRequest<void>, | ||
{} as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should handle erroneous adapter.getTasks call', async () => { | ||
const controller = new TaskController(mockAdapter); | ||
|
||
mockAdapter.getTasks.mockRejectedValue(null); | ||
|
||
const result = await controller.findAllByQuery( | ||
{ | ||
providerConfig: { | ||
userId: '123', | ||
apiKey: '123123123', | ||
apiUrl: ':)', | ||
locale: 'de-DE', | ||
}, | ||
} as BridgeRequest<void>, | ||
{} as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should resolve happy path', async () => { | ||
const controller = new TaskController(mockAdapter); | ||
const mockResponse = { json: jest.fn() }; | ||
|
||
mockAdapter.getTasks.mockResolvedValue([]); | ||
|
||
const req = { | ||
providerConfig: { | ||
userId: '123', | ||
apiKey: '123123123', | ||
apiUrl: ':)', | ||
locale: 'de-DE', | ||
}, | ||
} as BridgeRequest<void>; | ||
|
||
const result = await controller.findAllByQuery( | ||
req, | ||
mockResponse as unknown as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockAdapter.getTasks).toHaveBeenCalledWith( | ||
req, | ||
req.providerConfig, | ||
); | ||
expect(mockResponse.json).toHaveBeenCalledWith([]); | ||
}); | ||
}); | ||
|
||
describe('create', () => { | ||
it('Should check for providerConfig', async () => { | ||
const controller = new TaskController(mockAdapter); | ||
|
||
const result = await controller.create( | ||
{} as BridgeRequest<FollowUpWithIntegrationEntities>, | ||
{} as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should check if adapter.createFollowUp is implemented', async () => { | ||
const controller = new TaskController({}); | ||
|
||
const result = await controller.create( | ||
{} as BridgeRequest<FollowUpWithIntegrationEntities>, | ||
{} as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should handle erroneous adapter.createFollowUp call', async () => { | ||
const controller = new TaskController(mockAdapter); | ||
|
||
mockAdapter.createFollowUp.mockRejectedValue(null); | ||
|
||
const result = await controller.create( | ||
{ | ||
providerConfig: { | ||
userId: '123', | ||
apiKey: '123123123', | ||
apiUrl: ':)', | ||
locale: 'de-DE', | ||
}, | ||
} as BridgeRequest<FollowUpWithIntegrationEntities>, | ||
{} as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should resolve happy path', async () => { | ||
const controller = new TaskController(mockAdapter); | ||
const mockResponse = { json: jest.fn() }; | ||
|
||
const followUpId = 123; | ||
|
||
mockAdapter.createFollowUp.mockResolvedValue(followUpId); | ||
|
||
const req = { | ||
providerConfig: { | ||
userId: '123', | ||
apiKey: '123123123', | ||
apiUrl: ':)', | ||
locale: 'de-DE', | ||
}, | ||
} as BridgeRequest<FollowUpWithIntegrationEntities>; | ||
|
||
const result = await controller.create( | ||
req, | ||
mockResponse as unknown as Response, | ||
mockNext, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(mockAdapter.createFollowUp).toHaveBeenCalledWith( | ||
req.providerConfig, | ||
undefined, | ||
); | ||
expect(mockResponse.json).toHaveBeenCalledWith({ followUpId }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { NextFunction, Response } from 'express'; | ||
import { | ||
Adapter, | ||
BridgeRequest, | ||
FollowUpWithIntegrationEntities, | ||
} from '../models'; | ||
import { infoLogger } from '../util'; | ||
|
||
export class TaskController { | ||
constructor(private readonly adapter: Adapter) {} | ||
|
||
async findAllByQuery( | ||
req: BridgeRequest<void>, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
const { providerConfig } = req; | ||
|
||
if (!providerConfig) { | ||
next(new Error('Provider config not found')); | ||
return; | ||
} | ||
|
||
if (!this.adapter.getTasks) { | ||
next(new Error('Method not implemented')); | ||
return; | ||
} | ||
|
||
infoLogger('findAllByQuery', 'START', providerConfig.apiKey); | ||
|
||
try { | ||
const followUps = await this.adapter.getTasks(req, providerConfig); | ||
|
||
infoLogger( | ||
'findAllByQuery', | ||
`Received ${followUps.length} follow ups`, | ||
providerConfig.apiKey, | ||
); | ||
|
||
infoLogger('findAllByQuery', 'END', providerConfig.apiKey); | ||
res.json(followUps); | ||
} catch (err) { | ||
next(err); | ||
} | ||
} | ||
|
||
async create( | ||
req: BridgeRequest<FollowUpWithIntegrationEntities>, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
const { providerConfig } = req; | ||
|
||
if (!providerConfig) { | ||
next(new Error('Provider config not found')); | ||
return; | ||
} | ||
|
||
if (!this.adapter.createFollowUp) { | ||
next(new Error('Method not implemented')); | ||
return; | ||
} | ||
|
||
try { | ||
const followUpId = await this.adapter.createFollowUp( | ||
providerConfig, | ||
req.body, | ||
); | ||
res.json({ followUpId }); | ||
} catch (err) { | ||
next(err); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IntegrationEntity } from './integration-entity.model'; | ||
|
||
export type FollowUpEvent = { | ||
content: string; | ||
dueAt: number; | ||
title: string; | ||
type: string; | ||
}; | ||
|
||
export type FollowUpWithIntegrationEntities = FollowUpEvent & { | ||
integrationEntities: IntegrationEntity[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type Task = { | ||
id: string; | ||
content: string; | ||
createdAt: Date; | ||
dueAt: Date; | ||
link?: string; | ||
title: string; | ||
}; |