module.exports = (mongoose) => {
  const schema = mongoose.Schema(
    {
      external_user_id: {
        type: String,
        required: true
      },
      allow_site: {
        type: Number,
        required: true
      },
      username: String,
      name: String,
      email: String,
      permissions: [
        {
          type: String
        }
      ],
      create_by: String,
      edit_by: String
    },
    { collection: "ext_usr_perm", timestamps: true }
  );

  schema.method("toJSON", function () {
    const { __v, _id, ...object } = this.toObject();
    object.id = _id;
    return object;
  });

  schema.index({ external_user_id: 1, allow_site: 1 }, { unique: true });

  const ExtUserPermission = mongoose.model("ext_usr_perm", schema);
  return ExtUserPermission;
};
