2018-04-20 13:16:03 +00:00
|
|
|
module.exports = Self => {
|
2018-10-08 05:31:55 +00:00
|
|
|
Self.remoteMethodCtx('send', {
|
2018-04-20 13:16:03 +00:00
|
|
|
description: 'Send message to user',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'data',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
2018-10-08 05:31:55 +00:00
|
|
|
description: 'recipientFk, message',
|
2018-04-20 13:16:03 +00:00
|
|
|
http: {source: 'body'}
|
|
|
|
}, {
|
|
|
|
arg: 'context',
|
|
|
|
type: 'object',
|
|
|
|
http: function(ctx) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:recipient/send`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-08 05:31:55 +00:00
|
|
|
Self.send = async(ctx, data, transaction) => {
|
2019-03-22 12:45:17 +00:00
|
|
|
const accessToken = ctx.options && ctx.options.accessToken || ctx.req && ctx.req.accessToken;
|
|
|
|
const userId = accessToken.userId;
|
2018-10-08 05:31:55 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const sender = await models.Account.findById(userId, transaction);
|
|
|
|
const recipient = await models.Account.findById(data.recipientFk, transaction);
|
|
|
|
|
|
|
|
await Self.create({
|
|
|
|
sender: sender.name,
|
|
|
|
recipient: recipient.name,
|
|
|
|
message: data.message
|
|
|
|
}, transaction);
|
2018-04-20 13:16:03 +00:00
|
|
|
|
2018-10-08 05:31:55 +00:00
|
|
|
return await models.MessageInbox.create({
|
|
|
|
sender: sender.name,
|
|
|
|
recipient: recipient.name,
|
|
|
|
finalRecipient: recipient.name,
|
|
|
|
message: data.message
|
|
|
|
}, transaction);
|
2018-04-20 13:16:03 +00:00
|
|
|
};
|
|
|
|
};
|