Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operation for POST /rooms/{room_id}/tasks api #39

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/nodes/Chatwork/Chatwork.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum Operations {
deleteMessage = 'deleteMessage',
getRoomTasks = 'getRoomTasks',
getRoomTaskDetail = 'getRoomTaskDetail',
createRoomTask = 'createRoomTask',
}

interface ITestCase01 {
Expand Down Expand Up @@ -462,5 +463,46 @@ describe('Chatwork', () => {
expect(chatworkApiRequestMock).toHaveBeenCalledWith('GET', `/rooms/${roomId}/tasks/${taskId}`, null);
expect(result).toEqual([[{ json: apiResponse }]]);
})

test('should call create task api when operation = "createRoomTask"', async () => {
const apiResponse = {
'task_ids': [123, 124],
};
const getResourceMock = getNodeParameterMock.mockReturnValueOnce(Resources.rooms);
const getOperationMock = getNodeParameterMock.mockReturnValueOnce(Operations.createRoomTask);

const roomId = 1;
const taskDes = 'Buy milk'
const limit = '2020-11-08T05:16:37.000Z';
const toIds = '1,3,6';

const getDefaultRoomIdMock = getNodeParameterMock.mockReturnValueOnce(roomId);
const getRoomIdMock = getNodeParameterMock.mockReturnValueOnce(roomId);
const getTaskDesMock = getNodeParameterMock.mockReturnValueOnce(taskDes);
const getLimitMock = getNodeParameterMock.mockReturnValueOnce(limit);
const getToIds = getNodeParameterMock.mockReturnValueOnce(toIds);

getInputDataMock.mockReturnValue([{}]);
chatworkApiRequestMock.mockResolvedValueOnce(apiResponse);

const expectationBody = {
body: taskDes,
limit: new Date(limit).valueOf() / 1000,
to_ids: toIds,
};

const result = await chatworkNode.execute.call(context as any);

expect(getResourceMock).toHaveBeenCalledWith('resource', 0);
expect(getOperationMock).toHaveBeenCalledWith('operation', 0);
expect(getDefaultRoomIdMock).toHaveBeenCalledWith('roomId', 0);
expect(getRoomIdMock).toHaveBeenCalledWith('roomId', 0);
expect(getTaskDesMock).toHaveBeenCalledWith('body', 0);
expect(getLimitMock).toHaveBeenCalledWith('limit', 0);
expect(getToIds).toHaveBeenCalledWith('toIds', 0);

expect(chatworkApiRequestMock).toHaveBeenCalledWith('POST', `/rooms/${roomId}/tasks`, expectationBody);
expect(result).toEqual([[{ json: apiResponse }]]);
})
})
});
69 changes: 69 additions & 0 deletions src/nodes/Chatwork/Chatwork.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ export class Chatwork implements INodeType {
value: 'getRoomTaskDetail',
description: 'Get information about the specified task',
},
{
name: 'Add a new task to the chat',
value: 'createRoomTask',
description: 'Add a new task to the chat',
},
],
default: 'get',
},
Expand All @@ -289,6 +294,7 @@ export class Chatwork implements INodeType {
'deleteMessage',
'getRoomTasks',
'getRoomTaskDetail',
'createRoomTask',
],
},
},
Expand Down Expand Up @@ -411,6 +417,57 @@ export class Chatwork implements INodeType {
placeholder: 'Task id',
description: 'Id of the special task',
},
{
displayName: 'Task description',
name: 'body',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['rooms'],
operation: [
'createRoomTask',
],
},
},
placeholder: 'Task description',
description: 'Task description',
},
{
displayName: 'Deadline',
name: 'limit',
type: 'dateTime',
required: false,
default: new Date().toISOString(),
displayOptions: {
show: {
resource: ['rooms'],
operation: [
'createRoomTask',
],
},
},
placeholder: 'Deadline',
description: 'When the task is due (Use Unix time as input)',
},
{
displayName: 'To',
name: 'toIds',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: ['rooms'],
operation: [
'createRoomTask',
],
},
},
placeholder: 'To',
description: 'Account ID of the person/people responsible to complete the task. If multiple, IDs must be separated by comma',
},
],
};

Expand Down Expand Up @@ -488,6 +545,18 @@ export class Chatwork implements INodeType {
const taskId = this.getNodeParameter('taskId', itemIndex);
endpoint += `/tasks/${taskId}`;
break;
case 'createRoomTask':
const taskDes = this.getNodeParameter('body', itemIndex);
const limit = (new Date(this.getNodeParameter('limit', itemIndex) as string)).valueOf() / 1000;
const toIds = this.getNodeParameter('toIds', itemIndex);
body = {
body: taskDes,
limit,
to_ids: toIds,
}
method = 'POST';
endpoint += '/tasks';
break;
default:
throw new Error(`${operation} is not supported.`)
}
Expand Down