All files / src/components/Messaging/Message Recipient.js

38.89% Statements 7/18
41.18% Branches 7/17
50% Functions 1/2
50% Lines 7/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                  19x 1x     18x       18x 18x 18x 18x                                
/* eslint-disable object-curly-newline */
 
/*
  id: '<PSID>'
 
  Phone number: If you know a user's phone number, you can specify recipient.phone_number
  in the API request. This will send a message request to the recipient, without
  requiring them to interact with your page first. Sending messages to phone
  numbers requires the pages_messaging_phone_number permission. For more
  information, see Customer Matching.
*/
 
/*
  User Ref: For more information, see https://developers.facebook.com/docs/messenger-platform/discovery/checkbox-plugin.
*/
class Recipient {
  constructor({ id, phone_number, user_ref, name }) {
    if (!id && !phone_number && !user_ref) {
      throw new Error('Recipient must include one of id, phone_number, or user_ref.');
    }
 
    Iif (name && !phone_number) {
      console.warn('Recipient name should only use with phone_number');
    }
 
    this.id = id;
    this.phone_number = phone_number;
    this.user_ref = user_ref;
    this.name = name;
  }
 
  toObject() {
    const obj = {};
 
    if (this.id) obj.id = this.id;
    if (this.phone_number) obj.phone_number = this.phone_number;
    if (this.user_ref) obj.user_ref = this.user_ref;
    if (this.name) obj.name = this.name;
 
    return obj;
  }
}
 
export default Recipient;