Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Integration: Zendesk #158

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions engine-idk/src/common_models/ticketing/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** Copyright (c) 2023, Poozle, all rights reserved. **/

export interface Account {
id: string;
name: string;
domains: string[];
created_at: string;
updated_at: string;
}
1 change: 1 addition & 0 deletions engine-idk/src/common_models/ticketing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** Copyright (c) 2023, Poozle, all rights reserved. **/

export * from './account';
export * from './ticket';
export * from './collection';
export * from './comment';
Expand Down
2 changes: 2 additions & 0 deletions engine-idk/src/common_models/ticketing/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface Team {
name: string;
description: string;
members: Member[];
created_at: string;
updated_at: string;
}

export interface CreateTeamBody {
Expand Down
4 changes: 4 additions & 0 deletions engine-idk/src/common_models/ticketing/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Ticket {
id: string;
parent_id: string;
collection_id: string;
organization_id: string;
type: string;
name: string;
description: string;
Expand All @@ -37,6 +38,9 @@ export interface CreateTicketBody {
tags: TicketTag[];
created_by: string;
type: string;
account_id: string;
priority: string;
status: string;
}

export interface UpdateTicketBody {
Expand Down
3 changes: 3 additions & 0 deletions engine-idk/src/common_models/ticketing/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ export interface User {
name: string;
email_address: string;
avatar: string;
is_active: boolean;
created_at: string;
updated_at: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/** Copyright (c) 2023, Poozle, all rights reserved. **/

import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { IntegrationType } from '@prisma/client';

import { IntegrationAccount } from '@@generated/integrationAccount/entities';

import { GetIntegrationAccount } from 'common/decorators/integration_account.decorator';

import { AuthGuard } from 'modules/auth/auth.guard';

import {
AccountQueryParams,
GetAccountQueryParams,
PathParamsWithAccountId,
TicketingAccountResponse,
TicketingAccountsResponse,
} from './account.interface';
import { AccountService } from './account.service';

@Controller({
version: '1',
path: 'ticketing/accounts',
})
@ApiTags('Ticketing')
export class AccountController {
constructor(private accountService: AccountService) {}

@Get()
@UseGuards(new AuthGuard())
async getAccounts(
@Query() query: AccountQueryParams,
@GetIntegrationAccount(IntegrationType.TICKETING)
integrationAccount: IntegrationAccount,
): Promise<TicketingAccountsResponse> {
const accountResponse = await this.accountService.getAccounts(
integrationAccount,
query,
);

return accountResponse;
}

@Get(':account_id')
async getAccount(
@Query() query: GetAccountQueryParams,
@Param()
params: PathParamsWithAccountId,
@GetIntegrationAccount(IntegrationType.TICKETING)
integrationAccount: IntegrationAccount,
): Promise<TicketingAccountResponse> {
const accountResponse = await this.accountService.getAccount(
integrationAccount,
query,
params,
);

return accountResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/** Copyright (c) 2023, Poozle, all rights reserved. **/

import { Transform } from 'class-transformer';
import { IsBoolean, IsOptional, IsString } from 'class-validator';

import { JustRawParams, QueryParams } from 'common/interfaces/query.interface';
import { Meta } from 'common/interfaces/response.interface';

export class AccountQueryParams extends QueryParams {
@IsOptional()
@IsBoolean()
@Transform(({ value }) => {
return value === 'true' || value === 'True' || value === true;
})
realtime?: boolean = false;
}

export class GetAccountQueryParams extends JustRawParams {
@IsOptional()
@IsBoolean()
@Transform(({ value }) => {
return value === 'true' || value === 'True' || value === true;
})
realtime?: boolean = false;
}

export class Account {
id: string;
name: string;
domains: string[];
created_at: string;
updated_at: string;

integration_account_id?: string;
}

export const ACCOUNT_KEYS = [
'id',
'domains',
'name',
'description',
'updated_at',
'created_at',
];

export class TicketingAccountsResponse {
data: Account[];
meta: Meta;
}

export class TicketingAccountResponse {
data: Account;
}

export class PathParamsWithAccountId {
@IsString()
account_id: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* eslint-disable dot-location */
/** Copyright (c) 2023, Poozle, all rights reserved. **/

import { Injectable } from '@nestjs/common';
import { Method } from 'shared/integration_account.utils';

import { IntegrationAccount } from '@@generated/integrationAccount/entities';

import {
applyDateFilter,
getBaseQuery,
getMetaParams,
getObjectFromDb,
} from 'common/knex';
import { pagination } from 'common/utils';

import { DataService } from 'modules/data/data.service';

import {
ACCOUNT_KEYS,
AccountQueryParams,
GetAccountQueryParams,
PathParamsWithAccountId,
} from './account.interface';

const DATABASE_NAME = 'ticketing_account';

@Injectable()
export class AccountService {
constructor(private dataService: DataService) {}

async getAccounts(
integrationAccount: IntegrationAccount,
query: AccountQueryParams,
) {
if (query.realtime || !integrationAccount.syncEnabled) {
return await this.getAccountsForRealtime(integrationAccount, query);
}

return await this.getAccountsFromDb(integrationAccount, query);
}

async getAccount(
integrationAccount: IntegrationAccount,
query: GetAccountQueryParams,
params: PathParamsWithAccountId,
) {
if (query.realtime || !integrationAccount.syncEnabled) {
return await this.getAccountForRealtime(
integrationAccount,
query,
params,
);
}

return await this.getAccountFromDb(integrationAccount, query, params);
}

async getListFromDb(
workspaceName: string,
table: string,
where: Record<string, string>,
SELECT_KEYS: string[],
queryParams: AccountQueryParams,
) {
const { offset, limit, page } = pagination(
queryParams.limit,
queryParams.cursor,
);

let query = getBaseQuery(
workspaceName,
table,
where,
SELECT_KEYS,
queryParams.raw,
);

query = applyDateFilter(query, queryParams);

const data = await query.limit(limit).offset(offset);

return {
data,
meta: getMetaParams(data, limit, page),
};
}

async getAccountsFromDb(
integrationAccount: IntegrationAccount,
query: AccountQueryParams,
) {
return await this.getListFromDb(
integrationAccount.workspaceName,
DATABASE_NAME,
{
integration_account_id: integrationAccount.integrationAccountId,
},
ACCOUNT_KEYS,
query,
);
}

async getAccountFromDb(
integrationAccount: IntegrationAccount,
query: GetAccountQueryParams,
params: PathParamsWithAccountId,
) {
return await getObjectFromDb(
integrationAccount.workspaceName,
DATABASE_NAME,
{
integration_account_id: integrationAccount.integrationAccountId,
id: params.account_id,
},
ACCOUNT_KEYS,
query.raw,
);
}

async getAccountForRealtime(
integrationAccount: IntegrationAccount,
query: GetAccountQueryParams,
params: PathParamsWithAccountId,
) {
const accountResponse = await this.dataService.getDataFromAccount(
integrationAccount,
`/accounts/${params.account_id}`,
Method.GET,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{ raw: query.raw },
params,
);

return accountResponse;
}

async getAccountsForRealtime(
integrationAccount: IntegrationAccount,
query: AccountQueryParams,
) {
const accountResponse = await this.dataService.getDataFromAccount(
integrationAccount,
'/accounts',
Method.GET,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{
limit: query.limit,
cursor: query.cursor,
raw: query.raw,
created_after: query.created_after,
created_before: query.created_before,
},
{},
);

return accountResponse;
}
}
Loading