Skip to content

Commit 00bf532

Browse files
pkiparraLemonaOna
authored andcommitted
✨ Add route for getTaskById
1 parent 25b2319 commit 00bf532

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

src/controllers/task.controller.test.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import { Response } from 'express';
2-
import { BridgeRequest, FollowUpWithIntegrationEntities } from '../models';
2+
import {
3+
BridgeRequest,
4+
FollowUpWithIntegrationEntities,
5+
IdBridgeRequest,
6+
Task,
7+
} from '../models';
38
import { TaskController } from './task.controller';
49

510
describe('Task Controller', () => {
611
const mockAdapter = {
7-
getTasks: jest.fn(),
8-
findAllByQuery: jest.fn(),
12+
getTask: jest.fn(),
13+
findById: jest.fn(),
914
createFollowUp: jest.fn(),
1015
};
1116
const mockNext = jest.fn();
1217

13-
describe('findAllByQuery', () => {
18+
describe('findById', () => {
1419
beforeEach(() => jest.clearAllMocks());
1520

1621
it('Should check for providerConfig', async () => {
1722
const controller = new TaskController(mockAdapter);
1823

19-
const result = await controller.findAllByQuery(
20-
{} as BridgeRequest<void>,
24+
const result = await controller.findById(
25+
{ params: { id: '123' } } as IdBridgeRequest<Task>,
2126
{} as Response,
2227
mockNext,
2328
);
@@ -26,11 +31,11 @@ describe('Task Controller', () => {
2631
expect(mockNext).toHaveBeenCalled();
2732
});
2833

29-
it('Should check if adapter.getTasks is implemented', async () => {
34+
it('Should check if adapter.getTask is implemented', async () => {
3035
const controller = new TaskController({});
3136

32-
const result = await controller.findAllByQuery(
33-
{} as BridgeRequest<void>,
37+
const result = await controller.findById(
38+
{ params: { id: '123' } } as IdBridgeRequest<Task>,
3439
{} as Response,
3540
mockNext,
3641
);
@@ -39,20 +44,21 @@ describe('Task Controller', () => {
3944
expect(mockNext).toHaveBeenCalled();
4045
});
4146

42-
it('Should handle erroneous adapter.getTasks call', async () => {
47+
it('Should handle erroneous adapter.getTask call', async () => {
4348
const controller = new TaskController(mockAdapter);
4449

45-
mockAdapter.getTasks.mockRejectedValue(null);
50+
mockAdapter.getTask.mockRejectedValue(null);
4651

47-
const result = await controller.findAllByQuery(
52+
const result = await controller.findById(
4853
{
4954
providerConfig: {
5055
userId: '123',
5156
apiKey: '123123123',
5257
apiUrl: ':)',
5358
locale: 'de-DE',
5459
},
55-
} as BridgeRequest<void>,
60+
params: { id: '123' },
61+
} as IdBridgeRequest<Task>,
5662
{} as Response,
5763
mockNext,
5864
);
@@ -65,7 +71,15 @@ describe('Task Controller', () => {
6571
const controller = new TaskController(mockAdapter);
6672
const mockResponse = { json: jest.fn() };
6773

68-
mockAdapter.getTasks.mockResolvedValue([]);
74+
const mockTask: Task = {
75+
id: '123',
76+
content: 'string',
77+
createdAt: 12345678,
78+
dueAt: 12345678,
79+
title: 'string',
80+
type: 'string',
81+
};
82+
mockAdapter.getTask.mockResolvedValue(mockTask);
6983

7084
const req = {
7185
providerConfig: {
@@ -74,20 +88,21 @@ describe('Task Controller', () => {
7488
apiUrl: ':)',
7589
locale: 'de-DE',
7690
},
77-
} as BridgeRequest<void>;
91+
params: { id: '123' },
92+
} as IdBridgeRequest<Task>;
7893

79-
const result = await controller.findAllByQuery(
94+
const result = await controller.findById(
8095
req,
8196
mockResponse as unknown as Response,
8297
mockNext,
8398
);
8499

85100
expect(result).toBeUndefined();
86-
expect(mockAdapter.getTasks).toHaveBeenCalledWith(
87-
req,
101+
expect(mockAdapter.getTask).toHaveBeenCalledWith(
88102
req.providerConfig,
103+
req.params.id,
89104
);
90-
expect(mockResponse.json).toHaveBeenCalledWith([]);
105+
expect(mockResponse.json).toHaveBeenCalledWith(mockTask);
91106
});
92107
});
93108

src/controllers/task.controller.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,36 @@ import {
33
Adapter,
44
BridgeRequest,
55
FollowUpWithIntegrationEntities,
6+
Task,
67
} from '../models';
78
import { infoLogger } from '../util';
89

910
export class TaskController {
1011
constructor(private readonly adapter: Adapter) {}
1112

12-
async findAllByQuery(
13-
req: BridgeRequest<void>,
14-
res: Response,
15-
next: NextFunction,
16-
) {
17-
const { providerConfig } = req;
13+
async findById(req: BridgeRequest<Task>, res: Response, next: NextFunction) {
14+
const {
15+
providerConfig,
16+
params: { id },
17+
} = req;
1818

1919
if (!providerConfig) {
2020
next(new Error('Provider config not found'));
2121
return;
2222
}
2323

24-
if (!this.adapter.getTasks) {
24+
if (!this.adapter.getTask) {
2525
next(new Error('Method not implemented'));
2626
return;
2727
}
2828

2929
try {
30-
infoLogger('getTasks', 'START', providerConfig.apiKey);
30+
infoLogger('getTask', 'START', providerConfig.apiKey);
3131

32-
const followUps = await this.adapter.getTasks(req, providerConfig);
33-
34-
infoLogger(
35-
'getTasks',
36-
`Received ${followUps.length} follow ups`,
37-
providerConfig.apiKey,
38-
);
32+
const task = await this.adapter.getTask(providerConfig, id);
3933

40-
infoLogger('getTasks', 'END', providerConfig.apiKey);
41-
res.json(followUps);
34+
infoLogger('getTask', 'END', providerConfig.apiKey);
35+
res.json(task);
4236
} catch (err) {
4337
next(err);
4438
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export function start(
152152
controller.handleWebhook(req, res, next),
153153
);
154154

155-
app.get('/tasks', (req, res, next) =>
156-
taskController.findAllByQuery(req, res, next),
155+
app.get('/tasks/:id', (req, res, next) =>
156+
taskController.findById(req, res, next),
157157
);
158158

159159
app.post('/tasks', (req, res, next) => taskController.create(req, res, next));

src/models/adapter.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export interface Adapter {
9494
) => Promise<{ apiKey: string; apiUrl: string }>;
9595
handleWebhook?: (req: Request) => Promise<IntegrationsEvent[]>;
9696
verifyWebhookRequest?: (req: Request) => Promise<boolean>;
97-
getTasks?: (req: Request, config: Config) => Promise<Task[]>;
97+
getTask?: (config: Config, id: string) => Promise<Task>;
9898
createFollowUp?: (
9999
config: Config,
100100
body: FollowUpWithIntegrationEntities,

src/models/task.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export type Task = {
22
id: string;
33
content: string;
4-
createdAt: Date;
5-
dueAt: Date;
4+
createdAt: number;
5+
dueAt: number;
66
title: string;
77
type: string;
88
link?: string;

0 commit comments

Comments
 (0)