salix/back/methods/message/send.js

49 lines
1.4 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'
}
});
2019-09-18 13:11:09 +00:00
Self.send = async(ctx, data, options) => {
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;
2019-10-14 09:50:45 +00:00
const sender = await models.Account.findById(userId, null, options);
const recipient = await models.Account.findById(data.recipientFk, null, options);
2018-10-08 05:31:55 +00:00
await Self.create({
sender: sender.name,
recipient: recipient.name,
message: data.message
2019-09-18 13:11:09 +00:00
}, options);
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
2019-09-18 13:11:09 +00:00
}, options);
};
};