salix/services/loopback/common/methods/message/send.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2018-10-08 05:31:55 +00:00
Self.remoteMethodCtx('send', {
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',
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) => {
const userId = ctx.req.accessToken.userId;
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-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);
};
};