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

feat(api-client): Create controller for Integration module #397

Merged
merged 18 commits into from
Aug 9, 2024
Merged
73 changes: 73 additions & 0 deletions packages/api-client/src/controllers/integration/integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
CreateIntegrationRequest,
CreateIntegrationResponse,
DeleteIntegrationRequest,
DeleteIntegrationResponse,
GetAllIntegrationRequest,
GetAllIntegrationResponse,
GetIntegrationRequest,
GetIntegrationResponse,
UpdateIntegrationRequest,
UpdateIntegrationResponse
} from '@package/types/integrayion.types'
import client from '@package/client'

export default class IntegrationController {
private static apiClient = client

static async createIntegration(
request: CreateIntegrationRequest,
headers?: Record<string, string>
): Promise<CreateIntegrationResponse> {
return this.apiClient.post(
`/api/integration/${request.workspaceId}`,
request,
headers
)
}

static async updateIntegration(
request: UpdateIntegrationRequest,
headers?: Record<string, string>
): Promise<UpdateIntegrationResponse> {
return this.apiClient.put(
`/api/integration/${request.integrationId}`,
request,
headers
)
}

static async getIntegration(
request: GetIntegrationRequest,
headers?: Record<string, string>
): Promise<GetIntegrationResponse> {
return this.apiClient.get(
`/api/integration/${request.integrationId}`,
headers
)
}

static async getAllIntegrations(
request: GetAllIntegrationRequest,
headers?: Record<string, string>
): Promise<GetAllIntegrationResponse> {
let url = `/api/integration/all/${request.workspaceId}`
request.page && (url += `page=${request.page}&`)
request.limit && (url += `limit=${request.limit}&`)
request.sort && (url += `sort=${request.sort}&`)
request.order && (url += `order=${request.order}&`)
request.search && (url += `search=${request.search}&`)

return this.apiClient.get(url, headers)
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
}

static async deleteIntegration(
request: DeleteIntegrationRequest,
headers?: Record<string, string>
): Promise<DeleteIntegrationResponse> {
return this.apiClient.delete(
`/api/integration/${request.integrationId}`,
headers
)
}
}
87 changes: 87 additions & 0 deletions packages/api-client/src/types/integrayion.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
export interface CreateIntegrationRequest {
workspaceId: string
projectId: string
name: string
type: string
notifyOn: [string]
metadata: any
environmentId: string
}

export interface CreateIntegrationResponse {
id: string
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
name: string
metadata: any
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
createdAt: string
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
updatedAt: string
type: string
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
notifyOn: [string]
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
workspaceId: string
projectId: string
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
environmentId: string
}

export interface UpdateIntegrationRequest {
integrationId: string
name: string
}

export interface UpdateIntegrationResponse {
id: string
name: string
metadata: any
createdAt: string
updatedAt: string
type: string
notifyOn: [string]
workspaceId: string
projectId: string
environmentId: string
}

export interface DeleteIntegrationResponse {}

export interface DeleteIntegrationRequest {
integrationId: string
}

export interface GetIntegrationRequest {
integrationId: string
}

export interface GetIntegrationResponse {
id: string
name: string
metadata: any
createdAt: string
updatedAt: string
type: string
notifyOn: [string]
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
workspaceId: string
projectId: string
environmentId: string
}

export interface GetAllIntegrationRequest {
page?: number
limit?: number
sort?: string
order?: string
search?: string
workspaceId: string
}

export interface GetAllIntegrationResponse {
items: {
id: string
name: string
metadata: any
createdAt: string
updatedAt: string
type: string
notifyOn: [string]
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
workspaceId: string
projectId: string
environmentId: string
}[]
}
137 changes: 137 additions & 0 deletions packages/api-client/tests/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import client from '@package/client'
import IntegrationController from '@package/controllers/integration/integration'

describe('Get Environments Tests', () => {
const email = '[email protected]'
let projectId: string | null
let workspaceId: string | null
let environment: any
let integrationId: string | null

beforeAll(async () => {
//Create the user's workspace
const workspaceResponse = (await client.post(
'/api/workspace',
{
name: 'My Workspace'
},
{
'x-e2e-user-email': email
}
)) as any

workspaceId = workspaceResponse.id

// Create a project
const projectResponse = (await client.post(
`/api/project/${workspaceId}`,
{
name: 'Project',
storePrivateKey: true
},
{
'x-e2e-user-email': email
}
)) as any

projectId = projectResponse.id

const createEnvironmentResponse = await client.post(
`/api/environment/${projectId}`,
{
name: 'Dev'
},
{
'x-e2e-user-email': email
}
)

environment = createEnvironmentResponse
})

afterAll(async () => {
// Delete the workspace
await client.delete(`/api/workspace/${workspaceId}`, {
'x-e2e-user-email': email
})
})

it('should create a integration', async () => {
const integration = await IntegrationController.createIntegration(
{
workspaceId,
projectId,
name: 'Discord second',
type: 'DISCORD',
notifyOn: ['WORKSPACE_UPDATED'],
metadata: {
webhookUrl: '{{vault:WEBHOOK_URL}}'
},
environmentId: environment.id
},
{
'x-e2e-user-email': email
}
)
expect(integration.name).toBe('Discord second')
expect(integration.projectId).toBe(projectId)
expect(integration.environmentId).toBe(environment.id)
expect(integration.workspaceId).toBe(workspaceId)
expect(integration.type).toBe('DISCORD')
integrationId = integration.id
})

it('should update the integration', async () => {
const updatedIntegration: any =
await IntegrationController.updateIntegration(
{ integrationId, name: 'Github second' },
{ 'x-e2e-user-email': email }
)
expect(updatedIntegration.name).toBe('Github second')
})

it('should get a integration', async () => {
const integration: any = await IntegrationController.getIntegration(
{ integrationId },
{ 'x-e2e-user-email': email }
)
expect(integration).toBeDefined()
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
})

it('should get all the integration in workspace', async () => {
// adding more integrations
await IntegrationController.createIntegration(
{
workspaceId,
projectId,
name: 'Discord third',
type: 'DISCORD',
notifyOn: ['WORKSPACE_UPDATED'],
metadata: {
webhookUrl: '{{vault:WEBHOOK_URL}}'
},
environmentId: environment.id
},
{
'x-e2e-user-email': email
}
)
const integrations: any = await IntegrationController.getAllIntegrations(
{ workspaceId },
{ 'x-e2e-user-email': email }
)
expect(integrations.length).toBe(2)
})

it('should delete a integration', async () => {
await IntegrationController.deleteIntegration(
{ integrationId },
{ 'x-e2e-user-email': email }
)
const integrations: any = await IntegrationController.getAllIntegrations(
{ workspaceId },
{ 'x-e2e-user-email': email }
)
expect(integrations.length).toBe(1)
})
})
Loading