Skip to content

Commit

Permalink
fix(dialpad): minor cleanup (#149)
Browse files Browse the repository at this point in the history
## Describe your changes
Some additional cleanup

## Issue ticket number and link

## Checklist before requesting a review (skip if just adding/editing
APIs & templates)

-   [ ] I added tests, otherwise the reason is:
-   [ ] External API requests have `retries`
-   [ ] Pagination is used where appropriate
- [ ] The built in `nango.paginate` call is used instead of a `while
(true)` loop
- [ ] Third party requests are NOT parallelized (this can cause issues
with rate limits)
- [ ] If a sync requires metadata the `nango.yaml` has `auto_start:
false`
-   [ ] If the sync is a `full` sync then `track_deletes: true` is set
- [ ] I followed the best practices and guidelines from the [Writing
Integration
Scripts](/NangoHQ/integration-templates/blob/main/WRITING_INTEGRATION_SCRIPTS.md)
doc
  • Loading branch information
khaliqgant authored Dec 11, 2024
1 parent 4224887 commit eaf8101
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 101 deletions.
6 changes: 3 additions & 3 deletions flows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2654,10 +2654,10 @@ integrations:
path: /users/email
group: Users
output: SuccessResponse
input: EmailEntity
input: IdEntity
models:
EmailEntity:
email: string
IdEntity:
id: string
SuccessResponse:
success: boolean
User:
Expand Down
16 changes: 8 additions & 8 deletions integrations/dialpad/actions/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NangoAction, ProxyConfiguration } from '../../models';
import type { DialpadCreateUser, DialpadUser, User } from '../types';
import type { NangoAction, ProxyConfiguration, DialpadCreateUser, User } from '../../models';
import type { DialpadUser } from '../types';
import { dialpadCreateUserSchema } from '../schema.zod.js';

/**
Expand Down Expand Up @@ -33,14 +33,14 @@ export default async function createUser(input: DialpadCreateUser, nango: NangoA
retries: 10
};

const response = await nango.post<{ resource: DialpadUser }>(config);
const response = await nango.post<DialpadUser>(config);

const newUser = response.data.resource;
const newUser = response.data;
const user: User = {
id: newUser.id,
firstName: newUser.first_name,
lastName: newUser.last_name,
email: newUser.emails
id: newUser.id ? newUser.id.toString() : '',
firstName: newUser.first_name || '',
lastName: newUser.last_name || '',
email: parsedInput.data.email
};

return user;
Expand Down
43 changes: 6 additions & 37 deletions integrations/dialpad/actions/delete-user.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
import type { NangoAction, ProxyConfiguration, SuccessResponse, EmailEntity } from '../../models';
import { emailEntitySchema } from '../schema.zod.js';
import type { NangoAction, ProxyConfiguration, SuccessResponse, IdEntity } from '../../models';
import { idEntitySchema } from '../schema.zod.js';

/**
* Executes the delete user action by validating email input, fetching the user ID,
* and making the API call to Dialpad to delete the user.
* API Reference: https://developers.dialpad.com/reference/usersdelete
*/
export default async function runAction(nango: NangoAction, input: EmailEntity): Promise<SuccessResponse> {
const parsedInput = emailEntitySchema.safeParse(input);
export default async function runAction(nango: NangoAction, input: IdEntity): Promise<SuccessResponse> {
const parsedInput = idEntitySchema.safeParse(input);

if (!parsedInput.success) {
for (const error of parsedInput.error.errors) {
await nango.log(`Invalid input provided to delete a user: ${error.message} at path ${error.path.join('.')}`, { level: 'error' });
}

throw new nango.ActionError({
message: 'Invalid email provided to delete a user'
message: 'Invalid id provided to delete a user'
});
}

// Fetch the user ID using the email
const userId = await fetchUserIdByEmail(nango, parsedInput.data.email);
if (!userId) {
throw new nango.ActionError({
message: `User with email ${parsedInput.data.email} not found`
});
}

// Delete the user by ID
const config: ProxyConfiguration = {
// https://developers.dialpad.com/reference/usersdelete
endpoint: `/api/v2/users/${encodeURIComponent(userId)}`,
endpoint: `/api/v2/users/${encodeURIComponent(parsedInput.data.id)}`,
retries: 10
};

Expand All @@ -40,20 +26,3 @@ export default async function runAction(nango: NangoAction, input: EmailEntity):
success: true
};
}

//Fetches the user ID by email from Dialpad.
async function fetchUserIdByEmail(nango: NangoAction, email: string): Promise<string | null> {
const config: ProxyConfiguration = {
// https://developers.dialpad.com/reference/userslist
endpoint: '/api/v2/users',
params: {
email: encodeURIComponent(email)
},
retries: 10
};

const response = await nango.get<{ users: { id: string; email: string }[] }>(config);
const user = response.data.users.find((user) => user.email === email);

return user ? user.id : null;
}
6 changes: 3 additions & 3 deletions integrations/dialpad/nango.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ integrations:
path: /users/email
group: Users
output: SuccessResponse
input: EmailEntity
input: IdEntity

models:
EmailEntity:
email: string
IdEntity:
id: string
SuccessResponse:
success: boolean
User:
Expand Down
4 changes: 2 additions & 2 deletions integrations/dialpad/schema.zod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Generated by ts-to-zod
import { z } from 'zod';

export const emailEntitySchema = z.object({
email: z.string()
export const idEntitySchema = z.object({
id: z.string()
});

export const successResponseSchema = z.object({
Expand Down
21 changes: 9 additions & 12 deletions integrations/dialpad/syncs/users.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NangoSync, ProxyConfiguration } from '../../models';
import type { DialpadUser, User, UserListParams } from '../types';
import type { NangoSync, ProxyConfiguration, User } from '../../models';
import type { DialpadUser } from '../types';

export default async function fetchData(nango: NangoSync, params?: UserListParams) {
export default async function fetchData(nango: NangoSync) {
const proxyConfiguration: ProxyConfiguration = {
// https://developers.dialpad.com/reference/userslist
endpoint: '/api/v2/users',
Expand All @@ -10,24 +10,21 @@ export default async function fetchData(nango: NangoSync, params?: UserListParam
cursor_name_in_request: 'cursor',
cursor_path_in_response: 'cursor',
response_path: 'items',
limit: params?.limit || 100,
limit: 100,
limit_name_in_request: 'limit'
},
params: {
...(params?.state ? { state: params.state } : {}),
...(params?.email ? { email: params.email } : {}),
...(params?.number ? { number: params.number } : {}),
...(params?.company_admin !== undefined ? { company_admin: String(params.company_admin) } : {})
state: 'active'
}
};

for await (const dialpadUsers of nango.paginate<DialpadUser>(proxyConfiguration)) {
const users: User[] = dialpadUsers.map((dialpadUser: DialpadUser) => {
return {
id: dialpadUser.id ?? null,
firstName: dialpadUser.first_name ?? null,
lastName: dialpadUser.last_name ?? null,
email: dialpadUser.emails ?? null
id: dialpadUser.id ? dialpadUser.id.toString() : '',
firstName: dialpadUser.first_name || '',
lastName: dialpadUser.last_name || '',
email: dialpadUser.emails && dialpadUser.emails[0] ? dialpadUser.emails[0] : ''
};
});

Expand Down
36 changes: 0 additions & 36 deletions integrations/dialpad/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
export interface EmailEntity {
email: string;
}

export interface SuccessResponse {
success: boolean;
}

export interface User {
id: number | null;
email: string[] | null;
firstName: string | null;
lastName: string | null;
}

export type GroupRole = 'admin' | 'operator' | 'supervisor';

export type GroupType =
Expand Down Expand Up @@ -71,24 +56,3 @@ export interface DialpadUser {
status_message: string | null;
timezone: string | null;
}

export interface CreateUser {
firstName: string;
lastName: string;
email: string;
}

export interface DialpadCreateUser extends CreateUser {
license?: string;
officeId?: string;
autoAssign?: boolean;
}

export interface UserListParams {
cursor?: string;
state?: 'active' | 'all' | 'cancelled' | 'suspended' | 'deleted' | 'pending';
company_admin?: boolean;
limit?: number;
email?: string;
number?: string;
}

0 comments on commit eaf8101

Please sign in to comment.