diff --git a/docs/snippets/elastic-sip-trunking/projects/add.js b/docs/snippets/elastic-sip-trunking/projects/add.js new file mode 100644 index 00000000..fe09b244 --- /dev/null +++ b/docs/snippets/elastic-sip-trunking/projects/add.js @@ -0,0 +1,33 @@ +/** + * Sinch Node.js Snippet + * See: https://github.com/sinch/sinch-sdk-node/docs/snippets + */ +import { SinchClient } from '@sinch/sdk-core'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +async function main() { + const projectId = process.env.SINCH_PROJECT_ID ?? 'MY_PROJECT_ID'; + const keyId = process.env.SINCH_KEY_ID ?? 'MY_KEY_ID'; + const keySecret = process.env.SINCH_KEY_SECRET ?? 'MY_KEY_SECRET'; + + // The project ID you want to add to EST + const projectIdToAdd = '00000000-0000-0000-0000-000000000000'; + + const sinch = new SinchClient({ projectId, keyId, keySecret }); + + try { + const response = await sinch.elasticSipTrunking.projects.add({ + addProjectsRequestBody: { + projectIds: [projectIdToAdd], + }, + }); + console.log('✅ Successfully added additional projects to EST.'); + console.log(JSON.stringify(response, null, 2)); + } catch (err) { + console.error('❌ Failed to add additional projects to EST:'); + console.error(err); + } +} + +main(); diff --git a/docs/snippets/package.json b/docs/snippets/package.json index 5ac0c1a9..98c63a17 100644 --- a/docs/snippets/package.json +++ b/docs/snippets/package.json @@ -95,6 +95,7 @@ "elastic-sip-trunking:country-permissions:list": "node elastic-sip-trunking/country-permissions/list.js", "elastic-sip-trunking:country-permissions:update": "node elastic-sip-trunking/country-permissions/update.js", "elastic-sip-trunking:call-history:find": "node elastic-sip-trunking/calls-history/find.js", + "elastic-sip-trunking:projects:add": "node elastic-sip-trunking/projects/add.js", "fax:faxes:send": "node fax/faxes/send.js", "fax:faxes:get": "node fax/faxes/get.js", "fax:faxes:list": "node fax/faxes/list.js", diff --git a/examples/simple-examples/package.json b/examples/simple-examples/package.json index c978813d..8e1b8d3e 100644 --- a/examples/simple-examples/package.json +++ b/examples/simple-examples/package.json @@ -107,6 +107,7 @@ "elasticSipTrunks:countryPermissions:list": "ts-node src/elastic-sip-trunking/country-permissions/list.ts", "elasticSipTrunks:countryPermissions:update": "ts-node src/elastic-sip-trunking/country-permissions/update.ts", "elasticSipTrunks:calls:find": "ts-node src/elastic-sip-trunking/calls-history/find.ts", + "elasticSipTrunks:projects:add": "ts-node src/elastic-sip-trunking/projects/add.ts", "fax:services:create": "ts-node src/fax/services/create.ts", "fax:services:get": "ts-node src/fax/services/get.ts", "fax:services:list": "ts-node src/fax/services/list.ts", diff --git a/examples/simple-examples/src/elastic-sip-trunking/projects/add.ts b/examples/simple-examples/src/elastic-sip-trunking/projects/add.ts new file mode 100644 index 00000000..617b3782 --- /dev/null +++ b/examples/simple-examples/src/elastic-sip-trunking/projects/add.ts @@ -0,0 +1,24 @@ +import { ElasticSipTrunking } from '@sinch/sdk-core'; +import { + initElasticSipTrunkingService, + printFullResponse, +} from '../../config'; + +(async () => { + console.log('******************'); + console.log('* AddEstProjects *'); + console.log('******************'); + + const requestData: ElasticSipTrunking.AddProjectsRequestData = { + addProjectsRequestBody: { + projectIds: [ + '00000000-0000-0000-0000-000000000000', + ], + }, + }; + + const elasticSipTrunkingService = initElasticSipTrunkingService(); + const response = await elasticSipTrunkingService.projects.add(requestData); + + printFullResponse(response); +})(); diff --git a/packages/elastic-sip-trunking/src/models/v1/add-projects-request/add-projects-request.ts b/packages/elastic-sip-trunking/src/models/v1/add-projects-request/add-projects-request.ts new file mode 100644 index 00000000..b086f10c --- /dev/null +++ b/packages/elastic-sip-trunking/src/models/v1/add-projects-request/add-projects-request.ts @@ -0,0 +1,4 @@ +export interface AddProjectsRequest { + /** The project IDs you want to add. */ + projectIds: string[]; +} diff --git a/packages/elastic-sip-trunking/src/models/v1/add-projects-request/index.ts b/packages/elastic-sip-trunking/src/models/v1/add-projects-request/index.ts new file mode 100644 index 00000000..59070fdd --- /dev/null +++ b/packages/elastic-sip-trunking/src/models/v1/add-projects-request/index.ts @@ -0,0 +1 @@ +export type { AddProjectsRequest } from './add-projects-request'; diff --git a/packages/elastic-sip-trunking/src/models/v1/add-projects-response/add-projects-response.ts b/packages/elastic-sip-trunking/src/models/v1/add-projects-response/add-projects-response.ts new file mode 100644 index 00000000..9a569d38 --- /dev/null +++ b/packages/elastic-sip-trunking/src/models/v1/add-projects-response/add-projects-response.ts @@ -0,0 +1,4 @@ +export interface AddProjectsResponse { + /** The added project IDs. */ + addedProjects?: string[]; +} diff --git a/packages/elastic-sip-trunking/src/models/v1/add-projects-response/index.ts b/packages/elastic-sip-trunking/src/models/v1/add-projects-response/index.ts new file mode 100644 index 00000000..35103366 --- /dev/null +++ b/packages/elastic-sip-trunking/src/models/v1/add-projects-response/index.ts @@ -0,0 +1 @@ +export type { AddProjectsResponse } from './add-projects-response'; diff --git a/packages/elastic-sip-trunking/src/models/v1/index.ts b/packages/elastic-sip-trunking/src/models/v1/index.ts index 6df44669..2fea2af6 100644 --- a/packages/elastic-sip-trunking/src/models/v1/index.ts +++ b/packages/elastic-sip-trunking/src/models/v1/index.ts @@ -1,5 +1,7 @@ export * from './access-control-list'; export * from './add-access-control-list-to-trunk'; +export * from './add-projects-request'; +export * from './add-projects-response'; export * from './call'; export * from './country-permission'; export * from './create-access-control-list-request'; diff --git a/packages/elastic-sip-trunking/src/models/v1/requests/index.ts b/packages/elastic-sip-trunking/src/models/v1/requests/index.ts index a24aea09..ae24bb98 100644 --- a/packages/elastic-sip-trunking/src/models/v1/requests/index.ts +++ b/packages/elastic-sip-trunking/src/models/v1/requests/index.ts @@ -1,5 +1,6 @@ export * from './access-control-list/access-control-list-request-data'; export * from './calls-history/calls-history-request-data'; export * from './country-permissions/country-permissions-request-data'; +export * from './projects/projects-request-data'; export * from './sip-endpoints/sip-endpoints-request-data'; export * from './sip-trunks/sip-trunks-request-data'; diff --git a/packages/elastic-sip-trunking/src/models/v1/requests/projects/projects-request-data.ts b/packages/elastic-sip-trunking/src/models/v1/requests/projects/projects-request-data.ts new file mode 100644 index 00000000..03bbc4eb --- /dev/null +++ b/packages/elastic-sip-trunking/src/models/v1/requests/projects/projects-request-data.ts @@ -0,0 +1,5 @@ +import { AddProjectsRequest } from '../../add-projects-request'; + +export interface AddProjectsRequestData { + addProjectsRequestBody?: AddProjectsRequest; +} diff --git a/packages/elastic-sip-trunking/src/rest/v1/elastic-sip-trunking-service.ts b/packages/elastic-sip-trunking/src/rest/v1/elastic-sip-trunking-service.ts index cda0352a..83772f3e 100644 --- a/packages/elastic-sip-trunking/src/rest/v1/elastic-sip-trunking-service.ts +++ b/packages/elastic-sip-trunking/src/rest/v1/elastic-sip-trunking-service.ts @@ -11,6 +11,7 @@ import { AccessControlListApi } from './access-control-list'; import { SipEndpointsApi } from './sip-endpoints'; import { CountryPermissionsApi } from './country-permissions'; import { CallsHistoryApi } from './calls-history'; +import { ProjectsApi } from './projects'; export class LazyElasticSipTrunkingApiClient { private client?: ApiClient; @@ -38,6 +39,7 @@ export class ElasticSipTrunkingService { public readonly accessControlList: AccessControlListApi; public readonly countryPermissions: CountryPermissionsApi; public readonly calls: CallsHistoryApi; + public readonly projects: ProjectsApi; private readonly lazyClient: LazyElasticSipTrunkingApiClient; @@ -49,6 +51,7 @@ export class ElasticSipTrunkingService { this.accessControlList = new AccessControlListApi(this.lazyClient); this.countryPermissions = new CountryPermissionsApi(this.lazyClient); this.calls = new CallsHistoryApi(this.lazyClient); + this.projects = new ProjectsApi(this.lazyClient); } /** diff --git a/packages/elastic-sip-trunking/src/rest/v1/projects/index.ts b/packages/elastic-sip-trunking/src/rest/v1/projects/index.ts new file mode 100644 index 00000000..aaa4f03d --- /dev/null +++ b/packages/elastic-sip-trunking/src/rest/v1/projects/index.ts @@ -0,0 +1,2 @@ +export * from './projects-api'; +export * from './projects-api.jest.fixture'; diff --git a/packages/elastic-sip-trunking/src/rest/v1/projects/projects-api.jest.fixture.ts b/packages/elastic-sip-trunking/src/rest/v1/projects/projects-api.jest.fixture.ts new file mode 100644 index 00000000..224d4095 --- /dev/null +++ b/packages/elastic-sip-trunking/src/rest/v1/projects/projects-api.jest.fixture.ts @@ -0,0 +1,10 @@ +import { ProjectsApi } from './projects-api'; +import { AddProjectsRequestData, AddProjectsResponse } from '../../../models'; + +export class ProjectsApiFixture implements Partial> { + + /** + * Fixture associated to function add + */ + public add: jest.Mock, [AddProjectsRequestData]> = jest.fn(); +} diff --git a/packages/elastic-sip-trunking/src/rest/v1/projects/projects-api.ts b/packages/elastic-sip-trunking/src/rest/v1/projects/projects-api.ts new file mode 100644 index 00000000..ad390f92 --- /dev/null +++ b/packages/elastic-sip-trunking/src/rest/v1/projects/projects-api.ts @@ -0,0 +1,38 @@ +import { RequestBody } from '@sinch/sdk-client'; +import { ElasticSipTrunkingDomainApi } from '../elastic-sip-trunking-domain-api'; +import { LazyElasticSipTrunkingApiClient } from '../elastic-sip-trunking-service'; +import { AddProjectsRequestData, AddProjectsResponse } from '../../../models'; + +export class ProjectsApi extends ElasticSipTrunkingDomainApi { + + constructor(lazyClient: LazyElasticSipTrunkingApiClient) { + super(lazyClient, 'SipProjectsApi'); + } + + /** + * Add additional projects to EST + * Programmatically add additional projects for use with Elastic SIP Trunking. If you list a project ID which EST is already aware of, it will be ignored. + * @param { AddProjectsRequestData } data - The data to provide to the API call. + */ + public async add(data: AddProjectsRequestData): Promise { + const getParams = this.client.extractQueryParams(data, [] as never[]); + const headers: { [key: string]: string | undefined } = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }; + + const body: RequestBody = data['addProjectsRequestBody'] ? JSON.stringify(data['addProjectsRequestBody']) : '{}'; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/addProjects`; + + const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); + + return this.client.processCall({ + url, + requestOptions, + apiName: this.apiName, + operationId: 'AddEstProjects', + }); + } + +} diff --git a/packages/elastic-sip-trunking/tests/rest/v1/projects/projects-api.test.ts b/packages/elastic-sip-trunking/tests/rest/v1/projects/projects-api.test.ts new file mode 100644 index 00000000..ea297f07 --- /dev/null +++ b/packages/elastic-sip-trunking/tests/rest/v1/projects/projects-api.test.ts @@ -0,0 +1,45 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { AddProjectsRequestData, AddProjectsResponse } from '../../../../src/models'; +import { ProjectsApi, ProjectsApiFixture } from '../../../../src/rest/v1/projects'; +import { LazyElasticSipTrunkingApiClient } from '../../../../src'; + +describe('ProjectsApi', () => { + let projectsApi: ProjectsApi; + let fixture: ProjectsApiFixture; + let credentials: SinchClientParameters; + + beforeEach(() => { + fixture = new ProjectsApiFixture(); + credentials = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + const lazyClient = new LazyElasticSipTrunkingApiClient(credentials); + projectsApi = new ProjectsApi(lazyClient); + }); + + + describe ('addEstProjects', () => { + it('should make a POST request to add additional projects to EST', async () => { + // Given + const requestData: AddProjectsRequestData = { + addProjectsRequestBody: { + projectIds: ['project-id-1', 'project-id-2'], + }, + }; + const expectedResponse: AddProjectsResponse = { + addedProjects: ['project-id-1', 'project-id-2'], + }; + + // When + fixture.add.mockResolvedValue(expectedResponse); + projectsApi.add = fixture.add; + const response = await projectsApi.add(requestData); + + // Then + expect(response).toEqual(expectedResponse); + expect(fixture.add).toHaveBeenCalledWith(requestData); + }); + }); +}); diff --git a/packages/elastic-sip-trunking/tests/rest/v1/projects/projects.steps.ts b/packages/elastic-sip-trunking/tests/rest/v1/projects/projects.steps.ts new file mode 100644 index 00000000..40058af8 --- /dev/null +++ b/packages/elastic-sip-trunking/tests/rest/v1/projects/projects.steps.ts @@ -0,0 +1,32 @@ +import { ElasticSipTrunkingService, ElasticSipTrunking } from '../../../../src'; +import { Given, Then, When } from '@cucumber/cucumber'; +import assert from 'assert'; +import { ProjectsApi } from '../../../../src/rest/v1/projects'; + +let projectsApi: ProjectsApi; +let addProjectsResponse: ElasticSipTrunking.AddProjectsResponse; + +Given('the Elastic SIP Trunking service "Projects" is available', function () { + const elasticSipTrunkingService = new ElasticSipTrunkingService({ + projectId: 'tinyfrog-jump-high-over-lilypadbasin', + keyId: 'keyId', + keySecret: 'keySecret', + authHostname: 'http://localhost:3011', + elasticSipTrunkingHostname: 'http://localhost:3016', + }); + projectsApi = elasticSipTrunkingService.projects; +}); + +When('I send a request to add a new project for use with EST', async () => { + addProjectsResponse = await projectsApi.add({ + addProjectsRequestBody: { + projectIds: ['babywolf-howl-over-hill-silverforest'], + }, + }); +}); + +Then('the response contains the list of projects added', () => { + assert.ok(addProjectsResponse.addedProjects); + assert.equal(addProjectsResponse.addedProjects.length, 1); + assert.equal(addProjectsResponse.addedProjects[0], 'babywolf-howl-over-hill-silverforest'); +});