2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-09-21 10:38:24 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
2021-09-21 10:38:24 -07:00
|
|
|
|
|
|
|
import * as channel from './channel';
|
|
|
|
import * as message from './message';
|
|
|
|
import * as reaction from './reaction';
|
|
|
|
import * as user from './user';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { Mattermost } from './Interfaces';
|
2021-09-21 10:38:24 -07:00
|
|
|
|
2022-01-21 01:32:16 -08:00
|
|
|
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2021-09-21 10:38:24 -07:00
|
|
|
const items = this.getInputData();
|
|
|
|
const operationResult: INodeExecutionData[] = [];
|
2022-08-30 08:55:33 -07:00
|
|
|
let responseData: IDataObject | IDataObject[] = [];
|
2021-09-21 10:38:24 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const resource = this.getNodeParameter<Mattermost>('resource', i);
|
|
|
|
let operation = this.getNodeParameter('operation', i);
|
|
|
|
if (operation === 'del') {
|
|
|
|
operation = 'delete';
|
|
|
|
} else if (operation === 'desactive') {
|
|
|
|
operation = 'deactive';
|
|
|
|
}
|
|
|
|
|
|
|
|
const mattermost = {
|
|
|
|
resource,
|
|
|
|
operation,
|
|
|
|
} as Mattermost;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (mattermost.resource === 'channel') {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = await channel[mattermost.operation].execute.call(this, i);
|
2021-09-21 10:38:24 -07:00
|
|
|
} else if (mattermost.resource === 'message') {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = await message[mattermost.operation].execute.call(this, i);
|
2021-09-21 10:38:24 -07:00
|
|
|
} else if (mattermost.resource === 'reaction') {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = await reaction[mattermost.operation].execute.call(this, i);
|
2021-09-21 10:38:24 -07:00
|
|
|
} else if (mattermost.resource === 'user') {
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = await user[mattermost.operation].execute.call(this, i);
|
2021-09-21 10:38:24 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
operationResult.push(...executionData);
|
2021-09-21 10:38:24 -07:00
|
|
|
} catch (err) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-17 08:50:24 -07:00
|
|
|
operationResult.push({ json: this.getInputData(i)[0].json, error: err });
|
2021-09-21 10:38:24 -07:00
|
|
|
} else {
|
2022-06-03 08:25:07 -07:00
|
|
|
if (err.context) err.context.itemIndex = i;
|
2021-09-21 10:38:24 -07:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 01:32:16 -08:00
|
|
|
return [operationResult];
|
2021-09-21 10:38:24 -07:00
|
|
|
}
|