const BaseRepository = require('./baseRepository')
const User = require('../model/user')
const utils = require('../utils/utils')

class UserRepository extends BaseRepository {
  constructor () {
    super(User)
  }

  async getUserUUID (userName, orgUUID) {
    return utils.getUserUUID(userName, orgUUID)
  }

  async isAdmin (username, shortname) {
    return utils.isAdmin(username, shortname)
  }

  async isAdminUUID (username, orgUUID) {
    return utils.isAdminUUID(username, orgUUID)
  }

  async findOneByUUID (UUID) {
    return this.collection.findOne().byUUID(UUID)
  }

  async findUsersByOrgUUID (orgUUID) {
    return this.collection.find().byOrgUUID(orgUUID).countDocuments().exec()
  }

  async findOneByUserNameAndOrgUUID (userName, orgUUID) {
    return this.collection.findOne().byUserNameAndOrgUUID(userName, orgUUID)
  }

  async updateByUserNameAndOrgUUID (username, orgUUID, user, options = {}) {
    return this.collection.findOneAndUpdate().byUserNameAndOrgUUID(username, orgUUID).updateOne(user).setOptions(options)
  }

  async getAllUsers () {
    return this.collection.find()
  }
}

module.exports = UserRepository
