Skip to content

Commit

Permalink
feature(integration): add integration templates for zoho-mail (NangoH…
Browse files Browse the repository at this point in the history
…Q#1857)

## Describe your changes

I have added integration templates for zoho-mail. These include
templates to sync emails and templates action to add a user to an
organization and send an email.
  • Loading branch information
hassan254-prog authored Mar 19, 2024
1 parent fda6eb2 commit 1f79791
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 45 deletions.
162 changes: 117 additions & 45 deletions integration-templates/zoho-mail/nango.yaml
Original file line number Diff line number Diff line change
@@ -1,47 +1,119 @@
integrations:
zoho-mail:
syncs:
zoho-mail-tasks:
runs: every 6 hours
description: |
Fetches a list of all your personal tasks in Zoho mail
output: ZohoMailTask
sync_type: full
endpoint: /zoho-mail/tasks
scopes:
- ZohoMail.tasks.READ
zoho-mail:
syncs:
zoho-mail-tasks:
runs: every 6 hours
description: |
Fetches a list of all your personal tasks in Zoho mail
output: ZohoMailTask
sync_type: full
endpoint: /zoho-mail/tasks
scopes:
- ZohoMail.tasks.READ
zoho-mail-emails:
runs: every 6 hours
description: |
Fetches a list of all your account's emails in Zoho mail
output: ZohoMailEmail
sync_type: full
endpoint: /zoho-mail/emails
scopes:
- ZohoMail.messages.READ
actions:
zoho-mail-send-email:
description: |
An action to send an email in zoho mail
output: ZohoMailSendEmailOutput
input: ZohoMailSendEmailInput
endpoint: POST /zoho-mail/send-email
scopes:
- ZohoMail.messages.CREATE
zoho-mail-add-user:
description: |
An action to add a user to the organization in zoho mail
output: ZohoMailAddUserOutput
input: ZohoMailAddUserInput
endpoint: POST /zoho-mail/add-user
scopes:
- ZohoMail.organization.accounts.CREATE
models:
ZohoMailTask:
id: string
serviceType: number
modifiedTime: date
resourceId: string
attachments: []
statusStr: string
statusValue: number
description: string
project:
name: string
id: string
isTaskPublished: boolean
title: string
createdAt: date
portalId: number
serviceId: string
owner:
name: string
id: number
assigneeList: []
dependency: []
subtasks: []
priority: string
tags: []
followers: []
namespaceId: string
dependents: []
assignee:
name: string
id: number
serviceUniqId: number
depUniqId: string
status: string
ZohoMailTask:
id: string
serviceType: number
modifiedTime: date
resourceId: string
attachments: []
statusStr: string
statusValue: number
description: string
project:
name: string
id: string
isTaskPublished: boolean
title: string
createdAt: date
portalId: number
serviceId: string
owner:
name: string
id: number
assigneeList: []
dependency: []
subtasks: []
priority: string
tags: []
followers: []
namespaceId: string
dependents: []
assignee:
name: string
id: number
serviceUniqId: number
depUniqId: string
status: string
ZohoMailEmail:
summary: string
sentDateInGMT: string
calendarType: number
subject: string
messageId: string
flagid: string
status2: string
priority: string
hasInline: string
toAddress: string
folderId: string
ccAddress: string
hasAttachment: string
size: string
sender: string
receivedTime: string
fromAddress: string
status: string
ZohoMailSendEmailInput:
accountId: string
fromAddress: string
toAddress: string
ccAddress: string
bccAddress: string
subject: string
encoding: string
mailFormat: string
askReceipt: string
ZohoMailSendEmailOutput:
status: object
data: object
ZohoMailAddUserInput:
zoid: number
primaryEmailAddress: string
password: string
displayName: string
role: string
country: string
language: string
timeZone: string
oneTimePassword: boolean
groupMailList: string[]
ZohoMailAddUserOutput:
status: object
data: object
48 changes: 48 additions & 0 deletions integration-templates/zoho-mail/zoho-mail-add-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { NangoAction, ZohoMailAddUserOutput, ZohoMailAddUserInput } from './models';

export default async function runAction(nango: NangoAction, input: ZohoMailAddUserInput): Promise<ZohoMailAddUserOutput> {
//zoid is shorter in this 847300000
if (!input.zoid || typeof input.zoid !== 'number') {
throw new nango.ActionError({
message: 'zoid is a required parameter and needs to be of a non-empty number'
});
} else if (!input.primaryEmailAddress || typeof input.primaryEmailAddress !== 'string') {
throw new nango.ActionError({
message: 'primaryEmailAddress is a required body field and must be of a non-empty string'
});
} else if (!input.password || typeof input.password !== 'string') {
throw new nango.ActionError({
message: 'toAddress is a required body field and must be of a non-empty string'
});
}

try {
const endpoint = `/api/organization/${input.zoid}/accounts`;

const postData = {
primaryEmailAddress: input.primaryEmailAddress,
password: input.password,
displayName: input.displayName,
role: input.role,
country: input.country,
language: input.language,
timeZone: input.timeZone,
oneTimePassword: input.oneTimePassword,
groupMailList: input.groupMailList
};

const resp = await nango.post({
endpoint: endpoint,
data: postData
});

return {
status: resp.data.status,
data: resp.data.data
};
} catch (error) {
throw new nango.ActionError({
message: `Error in runAction: ${error}`
});
}
}
70 changes: 70 additions & 0 deletions integration-templates/zoho-mail/zoho-mail-emails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { NangoSync, ZohoMailEmail } from './models';

const LIMIT = 100;

export default async function fetchData(nango: NangoSync) {
let totalRecords = 0;
let offset = 1;

const metadata = (await nango.getMetadata()) || {};
const accountId = metadata['accountId'] ? String(metadata['accountId']) : '';

if (!accountId || typeof accountId !== 'string') {
throw new Error(`Please set a custom metadata accountId for the connection`);
}

try {
let moreEmails = true;
while (moreEmails) {
const response = await nango.get({
endpoint: `/api/accounts/${accountId}/messages/view`,
params: {
limit: LIMIT,
start: offset
}
});

if (response.data && response.data.data.length > 0) {
const mappedEmail: ZohoMailEmail[] = response.data.data.map(mapEmail) || [];
// Save Email
const batchSize: number = mappedEmail.length;
totalRecords += batchSize;
await nango.log(`Saving batch of ${batchSize} email(s) (total email(s): ${totalRecords})`);
await nango.batchSave(mappedEmail, 'ZohoMailEmail');

if (response.data.data.length < LIMIT) {
break;
}

offset += LIMIT;
} else {
moreEmails = false;
}
}
} catch (error) {
throw new Error(`Error in fetchData: ${error}`);
}
}

function mapEmail(email: any): ZohoMailEmail {
return {
summary: email.summary,
sentDateInGMT: email.sentDateInGMT,
calendarType: email.calendarType,
subject: email.subject,
messageId: email.messageId,
flagid: email.flagid,
status2: email.status2,
priority: email.priority,
hasInline: email.hasInline,
toAddress: email.toAddress,
folderId: email.folderId,
ccAddress: email.ccAddress,
hasAttachment: email.hasAttachment,
size: email.size,
sender: email.sender,
receivedTime: email.receivedTime,
fromAddress: email.fromAddress,
status: email.status
};
}
47 changes: 47 additions & 0 deletions integration-templates/zoho-mail/zoho-mail-send-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { NangoAction, ZohoMailSendEmailOutput, ZohoMailSendEmailInput } from './models';

export default async function runAction(nango: NangoAction, input: ZohoMailSendEmailInput): Promise<ZohoMailSendEmailOutput> {
//we need to enforce accountId to be of type string since accountId contains bigint values 6984040000000000000
if (!input.accountId || typeof input.accountId !== 'string') {
throw new nango.ActionError({
message: 'accountId is a required parameter and needs to be of a non-empty string'
});
} else if (!input.fromAddress || typeof input.accountId !== 'string') {
throw new nango.ActionError({
message: 'fromAddress is a required body field and must be of a non-empty string'
});
} else if (!input.toAddress || typeof input.accountId !== 'string') {
throw new nango.ActionError({
message: 'toAddress is a required body field and must be of a non-empty string'
});
}

try {
const endpoint = `/api/accounts/${input.accountId}/messages`;

const postData = {
fromAddress: input.fromAddress,
toAddress: input.toAddress,
ccAddress: input.ccAddress,
bccAddress: input.bccAddress,
subject: input.subject,
encoding: input.encoding,
mailFormat: input.mailFormat,
askReceipt: input.askReceipt
};

const resp = await nango.post({
endpoint: endpoint,
data: postData
});

return {
status: resp.data.status,
data: resp.data.data
};
} catch (error) {
throw new nango.ActionError({
message: `Error in runAction: ${error}`
});
}
}
Loading

0 comments on commit 1f79791

Please sign in to comment.