Skip to content

Commit

Permalink
notifications API
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Jul 29, 2023
1 parent f35c241 commit 5c3154c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Issues } from './issues';
import { Labels } from './labels';
import { Languages } from './languages';
import { MachineTranslation } from './machineTranslation';
import { Notifications } from './notifications';
import { OrganizationWebhooks } from './organizationWebhooks';
import { ProjectsGroups } from './projectsGroups';
import { Reports } from './reports';
Expand Down Expand Up @@ -35,6 +36,7 @@ export * from './issues';
export * from './labels';
export * from './languages';
export * from './machineTranslation';
export * from './notifications';
export * from './organizationWebhooks';
export * from './projectsGroups';
export * from './reports';
Expand Down Expand Up @@ -87,6 +89,7 @@ export default class Client extends CrowdinApi {
readonly labelsApi: Labels;
readonly stringCommentsApi: StringComments;
readonly bundlesApi: Bundles;
readonly notificationsApi: Notifications;

constructor(credentials: Credentials, config?: ClientConfig) {
super(credentials, config);
Expand Down Expand Up @@ -116,5 +119,6 @@ export default class Client extends CrowdinApi {
this.labelsApi = new Labels(credentials, config);
this.stringCommentsApi = new StringComments(credentials, config);
this.bundlesApi = new Bundles(credentials, config);
this.notificationsApi = new Notifications(credentials, config);
}
}
50 changes: 50 additions & 0 deletions src/notifications/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { CrowdinApi } from '../core';

export class Notifications extends CrowdinApi {
/**
* @param request request body
* @see https://developer.crowdin.com/api/v2/#operation/api.notify.post
*/
sendNotificationToAuthenticatedUser(request: NotificationsModel.Notification): Promise<void> {
const url = `${this.url}/notify`;
return this.post(url, request, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param request request body
* @see https://developer.crowdin.com/api/v2/#operation/api.projects.notify.post
*/
sendNotificationToProjectMembers(
projectId: number,
request: NotificationsModel.NotificationByUsers | NotificationsModel.NotificationByRole,
): Promise<void> {
const url = `${this.url}/projects/${projectId}/notify`;
return this.post(url, request, this.defaultConfig());
}

/**
* @param request request body
* @see https://developer.crowdin.com/enterprise/api/v2/#operation/api.notify.post
*/
sendNotificationToOrganizationMembers(
request: NotificationsModel.NotificationByUsers | NotificationsModel.NotificationByRole,
): Promise<void> {
const url = `${this.url}/notify`;
return this.post(url, request, this.defaultConfig());
}
}

export namespace NotificationsModel {
export interface Notification {
message: string;
}

export interface NotificationByUsers extends Notification {
userIds: number[];
}

export interface NotificationByRole extends Notification {
role: 'owner' | 'admin';
}
}
73 changes: 73 additions & 0 deletions tests/notifications/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as nock from 'nock';
import { Credentials, Notifications } from '../../src';

describe('Notifications API', () => {
let scope: nock.Scope;
const credentials: Credentials = {
token: 'testToken',
organization: 'testOrg',
};
const api: Notifications = new Notifications(credentials);
const projectId = 2;
const message = 'Hello';
const role = 'admin';
const userId = 123;

beforeAll(() => {
scope = nock(api.url)
.post(
'/notify',
{
message,
},
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200)
.post(
`/projects/${projectId}/notify`,
{
message,
role,
},
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200)
.post(
'/notify',
{
message,
userIds: [userId],
},
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200);
});

afterAll(() => {
scope.done();
});

it('Send Notification to Authenticated User', async () => {
await api.sendNotificationToAuthenticatedUser({ message });
});

it('Send Notification To Project Members', async () => {
await api.sendNotificationToProjectMembers(projectId, { message, role });
});

it('Send Notification To Organization Members', async () => {
await api.sendNotificationToOrganizationMembers({ message, userIds: [userId] });
});
});

0 comments on commit 5c3154c

Please sign in to comment.