Skip to content

Commit e74a46b

Browse files
authored
Add Create Queue and Division Get (#1)
1 parent 16a44c0 commit e74a46b

File tree

8 files changed

+736
-12
lines changed

8 files changed

+736
-12
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import type { INodeProperties } from 'n8n-workflow';
2+
3+
export const divisionOperations: INodeProperties[] = [
4+
{
5+
displayName: 'Operation',
6+
name: 'operation',
7+
type: 'options',
8+
noDataExpression: true,
9+
displayOptions: {
10+
show: {
11+
resource: ['division'],
12+
},
13+
},
14+
options: [
15+
{
16+
name: 'Get',
17+
value: 'get',
18+
description: 'Get a division',
19+
action: 'Get a division',
20+
},
21+
{
22+
name: 'Get Many',
23+
value: 'getAll',
24+
description: 'Get many divisions',
25+
action: 'Get many divisions',
26+
},
27+
],
28+
default: 'get',
29+
},
30+
];
31+
32+
export const divisionFields: INodeProperties[] = [
33+
/* -------------------------------------------------------------------------- */
34+
/* division:get */
35+
/* -------------------------------------------------------------------------- */
36+
{
37+
displayName: 'Division ID',
38+
name: 'divisionId',
39+
type: 'string',
40+
default: '',
41+
required: true,
42+
displayOptions: {
43+
show: {
44+
resource: ['division'],
45+
operation: ['get'],
46+
},
47+
},
48+
description: 'The ID of the division to retrieve',
49+
},
50+
51+
/* -------------------------------------------------------------------------- */
52+
/* division:getAll */
53+
/* -------------------------------------------------------------------------- */
54+
{
55+
displayName: 'Return All',
56+
name: 'returnAll',
57+
type: 'boolean',
58+
displayOptions: {
59+
show: {
60+
resource: ['division'],
61+
operation: ['getAll'],
62+
},
63+
},
64+
default: false,
65+
description: 'Whether to return all results or only up to a given limit',
66+
},
67+
{
68+
displayName: 'Limit',
69+
name: 'limit',
70+
type: 'number',
71+
displayOptions: {
72+
show: {
73+
resource: ['division'],
74+
operation: ['getAll'],
75+
returnAll: [false],
76+
},
77+
},
78+
typeOptions: {
79+
minValue: 1,
80+
maxValue: 500,
81+
},
82+
default: 50,
83+
description: 'Max number of results to return',
84+
},
85+
{
86+
displayName: 'Options',
87+
name: 'options',
88+
type: 'collection',
89+
placeholder: 'Add Option',
90+
default: {},
91+
displayOptions: {
92+
show: {
93+
resource: ['division'],
94+
operation: ['getAll'],
95+
},
96+
},
97+
options: [
98+
{
99+
displayName: 'Sort Order',
100+
name: 'sortOrder',
101+
type: 'options',
102+
options: [
103+
{
104+
name: 'Ascending',
105+
value: 'ascending',
106+
},
107+
{
108+
name: 'Descending',
109+
value: 'descending',
110+
},
111+
],
112+
default: 'ascending',
113+
},
114+
{
115+
displayName: 'Division IDs',
116+
name: 'id',
117+
type: 'string',
118+
default: '',
119+
description: 'Include only divisions with the specified IDs (comma-separated)',
120+
},
121+
{
122+
displayName: 'Name',
123+
name: 'name',
124+
type: 'string',
125+
default: '',
126+
description: 'Search term to filter by division name',
127+
},
128+
],
129+
},
130+
];
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import type { IExecuteFunctions } from 'n8n-workflow';
2+
import { divisionOperation } from './DivisionOperation';
3+
import * as GenericFunctions from './GenericFunctions';
4+
5+
describe('DivisionOperation', () => {
6+
const mockExecuteFunctions = {
7+
getNodeParameter: jest.fn(),
8+
helpers: {
9+
constructExecutionMetaData: jest.fn().mockImplementation((data) => data),
10+
returnJsonArray: jest.fn().mockImplementation((data) => data),
11+
},
12+
} as unknown as IExecuteFunctions & {
13+
getNodeParameter: jest.Mock;
14+
helpers: {
15+
constructExecutionMetaData: jest.Mock;
16+
returnJsonArray: jest.Mock;
17+
};
18+
};
19+
20+
beforeEach(() => {
21+
jest.clearAllMocks();
22+
});
23+
24+
describe('get', () => {
25+
it('should retrieve a division by id', async () => {
26+
const index = 0;
27+
const divisionId = 'test-division-id';
28+
const response = { id: divisionId, name: 'Test Division' };
29+
30+
mockExecuteFunctions.getNodeParameter.mockImplementation((paramName: string) => {
31+
if (paramName === 'operation') return 'get';
32+
if (paramName === 'divisionId') return divisionId;
33+
return undefined;
34+
});
35+
36+
jest.spyOn(GenericFunctions, 'genesysCloudApiRequest').mockResolvedValue(response);
37+
38+
await divisionOperation.call(mockExecuteFunctions, index);
39+
40+
expect(GenericFunctions.genesysCloudApiRequest).toHaveBeenCalledWith(
41+
'GET',
42+
`/api/v2/authorization/divisions/${divisionId}`,
43+
);
44+
});
45+
});
46+
47+
describe('getAll', () => {
48+
it('should retrieve multiple divisions', async () => {
49+
const index = 0;
50+
const returnAll = true;
51+
const response = [{ id: '1', name: 'Div 1' }, { id: '2', name: 'Div 2' }];
52+
53+
mockExecuteFunctions.getNodeParameter.mockImplementation((paramName: string) => {
54+
if (paramName === 'operation') return 'getAll';
55+
if (paramName === 'returnAll') return returnAll;
56+
if (paramName === 'options') return {};
57+
return undefined;
58+
});
59+
60+
jest.spyOn(GenericFunctions, 'genesysCloudApiRequestAllItems').mockResolvedValue(response);
61+
62+
await divisionOperation.call(mockExecuteFunctions, index);
63+
64+
expect(GenericFunctions.genesysCloudApiRequestAllItems).toHaveBeenCalledWith(
65+
'entities',
66+
'GET',
67+
'/api/v2/authorization/divisions',
68+
{},
69+
{},
70+
0,
71+
);
72+
});
73+
74+
it('should retrieve multiple divisions with limit', async () => {
75+
const index = 0;
76+
const returnAll = false;
77+
const limit = 10;
78+
const response = [{ id: '1', name: 'Div 1' }];
79+
80+
mockExecuteFunctions.getNodeParameter.mockImplementation((paramName: string) => {
81+
if (paramName === 'operation') return 'getAll';
82+
if (paramName === 'returnAll') return returnAll;
83+
if (paramName === 'limit') return limit;
84+
if (paramName === 'options') return {};
85+
return undefined;
86+
});
87+
88+
jest.spyOn(GenericFunctions, 'genesysCloudApiRequestAllItems').mockResolvedValue(response);
89+
90+
await divisionOperation.call(mockExecuteFunctions, index);
91+
92+
expect(GenericFunctions.genesysCloudApiRequestAllItems).toHaveBeenCalledWith(
93+
'entities',
94+
'GET',
95+
'/api/v2/authorization/divisions',
96+
{},
97+
{},
98+
limit,
99+
);
100+
});
101+
102+
it('should filter divisions by name', async () => {
103+
const index = 0;
104+
const returnAll = true;
105+
const name = 'TestDiv';
106+
const response = [{ id: '1', name: 'TestDiv' }];
107+
108+
mockExecuteFunctions.getNodeParameter.mockImplementation((paramName: string) => {
109+
if (paramName === 'operation') return 'getAll';
110+
if (paramName === 'returnAll') return returnAll;
111+
if (paramName === 'options') return { name };
112+
return undefined;
113+
});
114+
115+
jest.spyOn(GenericFunctions, 'genesysCloudApiRequestAllItems').mockResolvedValue(response);
116+
117+
await divisionOperation.call(mockExecuteFunctions, index);
118+
119+
expect(GenericFunctions.genesysCloudApiRequestAllItems).toHaveBeenCalledWith(
120+
'entities',
121+
'GET',
122+
'/api/v2/authorization/divisions',
123+
{},
124+
{ name },
125+
0,
126+
);
127+
});
128+
129+
it('should filter divisions by ids', async () => {
130+
const index = 0;
131+
const returnAll = true;
132+
const id = '1,2';
133+
const response = [{ id: '1', name: 'Div 1' }, { id: '2', name: 'Div 2' }];
134+
135+
mockExecuteFunctions.getNodeParameter.mockImplementation((paramName: string) => {
136+
if (paramName === 'operation') return 'getAll';
137+
if (paramName === 'returnAll') return returnAll;
138+
if (paramName === 'options') return { id };
139+
return undefined;
140+
});
141+
142+
jest.spyOn(GenericFunctions, 'genesysCloudApiRequestAllItems').mockResolvedValue(response);
143+
144+
await divisionOperation.call(mockExecuteFunctions, index);
145+
146+
expect(GenericFunctions.genesysCloudApiRequestAllItems).toHaveBeenCalledWith(
147+
'entities',
148+
'GET',
149+
'/api/v2/authorization/divisions',
150+
{},
151+
{ id: ['1', '2'] },
152+
0,
153+
);
154+
});
155+
});
156+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { IDataObject, IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
2+
import { genesysCloudApiRequest, genesysCloudApiRequestAllItems } from './GenericFunctions';
3+
4+
export async function divisionOperation(
5+
this: IExecuteFunctions,
6+
index: number,
7+
): Promise<INodeExecutionData[]> {
8+
const operation = this.getNodeParameter('operation', index) as string;
9+
10+
if (operation === 'get') {
11+
return get.call(this, index);
12+
} else if (operation === 'getAll') {
13+
return getAll.call(this, index);
14+
}
15+
16+
return [];
17+
}
18+
19+
async function get(this: IExecuteFunctions, index: number): Promise<INodeExecutionData[]> {
20+
const divisionId = this.getNodeParameter('divisionId', index) as string;
21+
22+
const responseData = await genesysCloudApiRequest.call(
23+
this,
24+
'GET',
25+
`/api/v2/authorization/divisions/${divisionId}`,
26+
);
27+
28+
return this.helpers.constructExecutionMetaData(
29+
this.helpers.returnJsonArray(responseData as IDataObject[]),
30+
{ itemData: { item: index } },
31+
);
32+
}
33+
34+
export async function getAll(
35+
this: IExecuteFunctions,
36+
index: number,
37+
): Promise<INodeExecutionData[]> {
38+
const returnAll = this.getNodeParameter('returnAll', index) as boolean;
39+
const qs: IDataObject = {};
40+
41+
const limit = returnAll ? 0 : (this.getNodeParameter('limit', index) as number);
42+
const options = this.getNodeParameter('options', index) as IDataObject;
43+
44+
// Pass all options (like sortOrder) to the query string
45+
Object.assign(qs, options);
46+
47+
if (qs.id) {
48+
qs.id = (qs.id as string).split(',');
49+
}
50+
51+
const responseData = await genesysCloudApiRequestAllItems.call(
52+
this,
53+
'entities',
54+
'GET',
55+
'/api/v2/authorization/divisions',
56+
{},
57+
qs,
58+
limit,
59+
);
60+
61+
return this.helpers.constructExecutionMetaData(
62+
this.helpers.returnJsonArray(responseData as IDataObject[]),
63+
{ itemData: { item: index } },
64+
);
65+
}

0 commit comments

Comments
 (0)