{"size": 1896, "ext": "js", "max_stars_repo_path": "server/services/sapo-author-srv.js", "max_stars_repo_name": "duythien0912/sansborne-app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/services/sapo-author-srv.js", "max_issues_repo_name": "duythien0912/sansborne-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/services/sapo-author-srv.js", "max_forks_repo_name": "duythien0912/sansborne-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.8351648352, "max_line_length": 94, "alphanum_fraction": 0.5801687764}
'use strict';
const { Authors } = require('../dal/models');
const { pick } = require('../helpers/objects');

class SapoAuthorsrv {
  static async readMany(query, options) {
    return Authors.find(query)
      .limit(+options.limit || 10)
      .skip(+options.offset)
      .sort(options.sort);
  }

  static async readOne(_id) {
    return Authors.findOne({ _id });
  }

  static async createOne(body) {
    return Authors.create(body);
  }

  static async createMany(body) {
    return Authors.insertMany(body);
  }

  static async updateOne(_id, body) {
    const newData = pick(body, [
      'email',
      'phone',
      'first_name',
      'last_name',
      'note',
      'tags',
      'total_spent',
      'orders_count',
    ]);

    return Authors.findOneAndUpdate({ _id }, newData, { useFindAndModify: false, new: true });
  }

  static async upsertMany(body) {
    var bulkUpdateOps = body.map(function(doc) {
      return {
        updateOne: {
          filter: { authors: doc },
          update: { $set: { authors: doc } },
          upsert: true,
        },
      };
    });
    const { nModified } = await Authors.bulkWrite(bulkUpdateOps);

    return nModified > 0;
  }

  static async updateMany(body) {
    const newData = pick(body.updatingFields, [
      'email',
      'phone',
      'first_name',
      'last_name',
      'note',
      'tags',
      'total_spent',
      'orders_count',
    ]);

    const { nModified } = await Authors.updateMany(
      body.filter, // find criteria
      newData // changing data
    );

    return nModified > 0;
  }

  static async deleteOne(_id) {
    const { deletedCount } = await Authors.deleteOne({ _id });

    return deletedCount > 0;
  }

  static async deleteMany(ids) {
    const { deletedCount } = await Authors.deleteMany({ _id: { $in: ids } });

    return deletedCount > 0;
  }
}

module.exports = {
  SapoAuthorsrv,
};
