-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] }); | ||
}); | ||
}); |