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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-18 13:11:09 +00:00
|
|
|
Self.send = async(ctx, data, options) => {
|
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;
|
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-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
|
2019-09-18 13:11:09 +00:00
|
|
|
}, options);
|
2018-04-20 13:16:03 +00:00
|
|
|
};
|
|
|
|
};
|