<commit_before>import knex from '../knex';
/* eslint-disable import/no-cycle */
import Proposal from './Proposal';
/* eslint-enable import/no-cycle */

import User from './User';
import Request from './Request';
import Discussion from './Discussion'; // eslint-disable-line import/no-cycle

import { canSee, canMutate, Models } from '../../core/accessControl';
import { Groups, isAdmin } from '../../organization';

async function validateCoordinator(viewer, id, loaders) {
  const coordinator = await User.gen(viewer, id, loaders);
  return coordinator; // TODO val rules
}

async function updateCoordinatorsGroups(viewer, id, loaders) {
  const coordinator = await User.gen(viewer, id, loaders);
  if (coordinator) {
    // eslint-disable-next-line no-bitwise
    const newGroups = coordinator.groups | Groups.TEAM_LEADER;
    await User.update(viewer, { id, groups: newGroups }, loaders);
    loaders.users.clear(id);
  }
}

async function changeWTStatus(viewer, loaders, deactivate, workteamId) {
  const now = new Date();
  let value = null;

  if (deactivate) {
    value = now;
  }
  const [updatedWorkTeam = null] = await knex.transaction(async trx => {
    // soft-delete discussions
    await knex('discussions')
      .transacting(trx)
      .forUpdate()
      .where({ work_team_id: workteamId })
      .update({ deleted_at: value, updated_at: now });

    // soft-delete proposals
    const proposalIds = await knex('proposals')
      .where({ work_team_id: workteamId })
      .pluck('id');

    const proposalPromises = proposalIds.map(pId =>
      Proposal.toggleStatus(viewer, { id: pId, value }, loaders, trx),
    );

    await Promise.all(proposalPromises);

    // set user_work_teams to disabled/deleted

    await knex('user_work_teams')
      .transacting(trx)
      .forUpdate()
      .where({ work_team_id: workteamId })
      .update({ inactive: !!value, updated_at: now });

    return knex('work_teams')
      .transacting(trx)
      .forUpdate()
      .where({ id: workteamId })
      .update({ deleted_at: value, updated_at: now })
      .returning('*');
  });
  // eslint-disable-next-line
  return updatedWorkTeam ? new WorkTeam(updatedWorkTeam) : null;
}

class WorkTeam {
  constructor(data) {
    this.id = data.id;
    this.coordinatorId = data.coordinator_id;
    this.name = data.name;
    this.numMembers = data.num_members;
    this.numDiscussions = data.num_discussions;
    this.numProposals = data.num_proposals;
    this.restricted = data.restricted;
    this.deName = data.de_name;
    this.itName = data.it_name;
    this.lldName = data.lld_name;
    this.mainTeam = data.main;
    this.deletedAt = data.deleted_at;
  }

  canNotify(viewer) {
    // eslint-disable-next-line eqeqeq
    return viewer.id == this.coordinatorId || viewer.role.type === 'admin';
  }

  canModifyMemberShips(viewer, requester) {
    // eslint-disable-next-line eqeqeq
    if (this.restricted) {
      /* eslint-disable eqeqeq */
      if (
        requester.id == this.coordinatorId ||
        viewer.id == this.coordinatorId
      ) {
        return true;
      }
      return false;
    }

    return false;
  }<commit_msg>allowed admins to add members to any group<commit_after>import knex from '../knex';
/* eslint-disable import/no-cycle */
import Proposal from './Proposal';
/* eslint-enable import/no-cycle */

import User from './User';
import Request from './Request';
import Discussion from './Discussion'; // eslint-disable-line import/no-cycle

import { canSee, canMutate, Models } from '../../core/accessControl';
import { Groups, isAdmin } from '../../organization';

async function validateCoordinator(viewer, id, loaders) {
  const coordinator = await User.gen(viewer, id, loaders);
  return coordinator; // TODO val rules
}

async function updateCoordinatorsGroups(viewer, id, loaders) {
  const coordinator = await User.gen(viewer, id, loaders);
  if (coordinator) {
    // eslint-disable-next-line no-bitwise
    const newGroups = coordinator.groups | Groups.TEAM_LEADER;
    await User.update(viewer, { id, groups: newGroups }, loaders);
    loaders.users.clear(id);
  }
}

async function changeWTStatus(viewer, loaders, deactivate, workteamId) {
  const now = new Date();
  let value = null;

  if (deactivate) {
    value = now;
  }
  const [updatedWorkTeam = null] = await knex.transaction(async trx => {
    // soft-delete discussions
    await knex('discussions')
      .transacting(trx)
      .forUpdate()
      .where({ work_team_id: workteamId })
      .update({ deleted_at: value, updated_at: now });

    // soft-delete proposals
    const proposalIds = await knex('proposals')
      .where({ work_team_id: workteamId })
      .pluck('id');

    const proposalPromises = proposalIds.map(pId =>
      Proposal.toggleStatus(viewer, { id: pId, value }, loaders, trx),
    );

    await Promise.all(proposalPromises);

    // set user_work_teams to disabled/deleted

    await knex('user_work_teams')
      .transacting(trx)
      .forUpdate()
      .where({ work_team_id: workteamId })
      .update({ inactive: !!value, updated_at: now });

    return knex('work_teams')
      .transacting(trx)
      .forUpdate()
      .where({ id: workteamId })
      .update({ deleted_at: value, updated_at: now })
      .returning('*');
  });
  // eslint-disable-next-line
  return updatedWorkTeam ? new WorkTeam(updatedWorkTeam) : null;
}

class WorkTeam {
  constructor(data) {
    this.id = data.id;
    this.coordinatorId = data.coordinator_id;
    this.name = data.name;
    this.numMembers = data.num_members;
    this.numDiscussions = data.num_discussions;
    this.numProposals = data.num_proposals;
    this.restricted = data.restricted;
    this.deName = data.de_name;
    this.itName = data.it_name;
    this.lldName = data.lld_name;
    this.mainTeam = data.main;
    this.deletedAt = data.deleted_at;
  }

  canNotify(viewer) {
    // eslint-disable-next-line eqeqeq
    return viewer.id == this.coordinatorId || viewer.role.type === 'admin';
  }

  canModifyMemberShips(viewer, requester) {
    // eslint-disable-next-line eqeqeq
    if (this.restricted) {
      /* eslint-disable eqeqeq */
      if (
        requester.id == this.coordinatorId ||
        viewer.id == this.coordinatorId ||
        isAdmin(viewer)
      ) {
        return true;
      }
      return false;
    }

    return false;
  }
{'fraction_non_alphanumeric': 0.13247325361862808, 'fraction_numerical': 0.0, 'mean_word_length': 3.0081967213114753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7291490', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2056, 'n_words': 460}