-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of different apps (#100)
* Add support of different apps * Fix batch push notifications * Use fixed version of smart-config * Add default app config
- Loading branch information
1 parent
1fd27a4
commit 5091005
Showing
17 changed files
with
158 additions
and
55 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
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
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
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
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 |
---|---|---|
@@ -1,27 +1,24 @@ | ||
const RecipientProvider = require('../../recipient-profile/model'); | ||
const Notification = require('../../notification/model'); | ||
const _ = require('lodash'); | ||
const Promise = require('bluebird'); | ||
const pushHelper = require('../../../helpers/push-send'); | ||
|
||
module.exports = async({ message, payload, recipientId }) => { | ||
const credentials = await getRecipientCredentials({ recipientId, message, payload }); | ||
return await send(credentials); | ||
}; | ||
|
||
async function createNotifications({ recipientId, message, payload }) { | ||
return await Promise.all( | ||
_(await RecipientProvider.find({ recipientId, providerType: 'push' })) | ||
.uniq('token') | ||
.map(({ recipientId, token, platform, deviceId }) => { | ||
return Notification.create({ recipientId, token, platform, deviceId, message, payload }); | ||
}) | ||
.value() | ||
); | ||
} | ||
|
||
function send(toSend) { | ||
return Promise.map(toSend, push => pushHelper.send(push)); | ||
} | ||
|
||
const pushes = await createNotifications({ recipientId, message, payload }); | ||
async function getRecipientCredentials({ recipientId, message, payload }) { | ||
return await Promise.all( | ||
_(await RecipientProvider.find({ recipientId, providerType: 'push' })) | ||
.uniq('token') | ||
.map((pushProfile) => { | ||
return { recipientId, message, payload, ...pushProfile.toJSON() }; | ||
}) | ||
.value() | ||
); | ||
} | ||
|
||
return await send(pushes); | ||
}; | ||
function send(toSend) { | ||
return Promise.map(toSend, push => pushHelper.send(push)); | ||
} |
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
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,76 @@ | ||
require('./test-env'); | ||
|
||
const config = require('smart-config'); | ||
const { toObject } = require('node-helpers'); | ||
const Notification = require('../src/services/notification/model'); | ||
|
||
const platform = 'android'; | ||
const app = { name: 'android' }; | ||
const token = String(new Date()); | ||
|
||
describe('Push send', () => { | ||
|
||
describe('success', () => { | ||
let recipientId; | ||
const message = String(new Date()); | ||
|
||
before(async() => { | ||
const recipient = await helpers.createRandomRecipient(); | ||
recipientId = String(recipient.id); | ||
ctx.recipientProfile = await helpers.createRecipientProfile({ | ||
recipientId, providerType: 'push', platform, app, token | ||
}); | ||
}); | ||
|
||
it('should be sent', async() => { | ||
await request | ||
.post('/notification/batch') | ||
.send({ | ||
recipients: [recipientId], | ||
providers: { push: { message } } | ||
}); | ||
|
||
await helpers.timeout(1000); | ||
const notification = toObject(await Notification.findOne({ message })); | ||
|
||
assert.equal(notification.token, token); | ||
assert.equal(notification.platform, platform); | ||
assert.equal(notification.app.name, app.name); | ||
assert.equal(notification.message, message); | ||
assert(notification.sendDate); | ||
}); | ||
}); | ||
|
||
describe('default app', () => { | ||
|
||
let recipientWithoutApp; | ||
const message = String(new Date() + 'no app'); | ||
|
||
before(async() => { | ||
const recipient = await helpers.createRandomRecipient(); | ||
recipientWithoutApp = String(recipient.id); | ||
ctx.recipientProfile = await helpers.createRecipientProfile({ | ||
recipientId: recipientWithoutApp, providerType: 'push', platform, token | ||
}); | ||
}); | ||
|
||
it('should use default app', async() => { | ||
await request | ||
.post('/notification/batch') | ||
.send({ | ||
recipients: [recipientWithoutApp], | ||
providers: { push: { message } } | ||
}); | ||
|
||
await helpers.timeout(1000); | ||
const notification = toObject(await Notification.findOne({ message })); | ||
|
||
assert.equal(notification.token, token); | ||
assert.equal(notification.platform, platform); | ||
assert.equal(notification.message, message); | ||
assert(notification.sendDate); | ||
assert(!notification.error); | ||
}); | ||
}); | ||
|
||
}); |
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
Oops, something went wrong.