class Recipient {
  constructor(opt) {
    if (opt) {
      this.fillObject(opt);
    }
  }

  fillObject(opt) {
    if (opt.email) {
      this.email = opt.email;
    } else if (opt.userId) {
      this.userId = opt.userId;
    } else {
      this.role = opt.role;
      this.to = opt.to;
    }
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'recipient.schema.json',
      type: 'object',
      additionalProperties: true,
      properties: {
        anyOf: [
          { email: { type: ['string', 'null'] } },
          { userId: { type: ['string', 'null'] } },
          { role: { type: ['string', 'null'] } },
          { to: { type: ['string', 'null'] } },
        ],
      },
    };
  }
}

module.exports = Recipient;
