2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-04-01 01:12:47 -07:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2021-02-20 12:12:55 -08:00
|
|
|
IHookFunctions,
|
2022-04-01 01:12:47 -07:00
|
|
|
INodeCredentialTestResult,
|
2021-02-20 12:12:55 -08:00
|
|
|
INodePropertyOptions,
|
2022-04-01 01:12:47 -07:00
|
|
|
JsonObject,
|
2021-02-20 12:12:55 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-02-20 12:12:55 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an authenticated REST API request to Emelia, used for trigger node.
|
|
|
|
*/
|
|
|
|
export async function emeliaApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object = {},
|
|
|
|
qs: object = {},
|
|
|
|
) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const { apiKey } = (await this.getCredentials('emeliaApi')) as { apiKey: string };
|
2021-02-20 12:12:55 -08:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
Authorization: apiKey,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `https://graphql.emelia.io${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request.call(this, options);
|
2021-02-20 12:12:55 -08:00
|
|
|
} catch (error) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-02-20 12:12:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
/**
|
|
|
|
* Make an authenticated GraphQL request to Emelia.
|
|
|
|
*/
|
|
|
|
export async function emeliaGraphqlRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
body: object = {},
|
|
|
|
) {
|
|
|
|
const response = await emeliaApiRequest.call(this, 'POST', '/graphql', body);
|
|
|
|
|
|
|
|
if (response.errors) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), response as JsonObject);
|
2023-01-13 09:11:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2021-02-20 12:12:55 -08:00
|
|
|
/**
|
|
|
|
* Load resources so that the user can select them easily.
|
|
|
|
*/
|
|
|
|
export async function loadResource(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
resource: 'campaign' | 'contactList',
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2022-08-01 13:47:55 -07:00
|
|
|
const mapping: { [key in 'campaign' | 'contactList']: { query: string; key: string } } = {
|
2021-02-20 12:12:55 -08:00
|
|
|
campaign: {
|
|
|
|
query: `
|
|
|
|
query GetCampaigns {
|
|
|
|
campaigns {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
key: 'campaigns',
|
|
|
|
},
|
|
|
|
contactList: {
|
|
|
|
query: `
|
|
|
|
query GetContactLists {
|
|
|
|
contact_lists {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
key: 'contact_lists',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const responseData = await emeliaGraphqlRequest.call(this, { query: mapping[resource].query });
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
return responseData.data[mapping[resource].key].map(
|
|
|
|
(campaign: { name: string; _id: string }) => ({
|
|
|
|
name: campaign.name,
|
|
|
|
value: campaign._id,
|
|
|
|
}),
|
|
|
|
);
|
2022-04-01 01:12:47 -07:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function emeliaApiTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
2022-04-01 01:12:47 -07:00
|
|
|
const credentials = credential.data;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
query: `
|
|
|
|
query all_campaigns {
|
|
|
|
all_campaigns {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
status
|
|
|
|
createdAt
|
|
|
|
stats {
|
|
|
|
mailsSent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
operationName: 'all_campaigns',
|
|
|
|
};
|
2021-02-20 12:12:55 -08:00
|
|
|
|
2022-04-01 01:12:47 -07:00
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
Authorization: credentials?.apiKey,
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
2022-12-29 03:20:43 -08:00
|
|
|
uri: 'https://graphql.emelia.io/graphql',
|
2022-04-01 01:12:47 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
await this.helpers.request(options);
|
2022-04-01 01:12:47 -07:00
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: `Connection details not valid: ${(error as JsonObject).message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Authentication successful!',
|
|
|
|
};
|
2021-02-20 12:12:55 -08:00
|
|
|
}
|