<commit_before>import { Organization, Webhook } from "../interfaces/tables/organization";
import {
  createOrganization,
  updateOrganization,
  deleteOrganization,
  getOrganization,
  getOrganizationApiKeys,
  getApiKey,
  updateApiKey,
  createApiKey,
  deleteApiKey,
  getOrganizationDomains,
  getDomain,
  updateDomain,
  createDomain,
  deleteDomain,
  checkDomainAvailability,
  getOrganizationWebhooks,
  getWebhook,
  updateWebhook,
  createWebhook,
  deleteWebhook,
  getOrganizationMemberships,
  deleteAllOrganizationMemberships,
  updateOrganizationMembership,
  deleteOrganizationMembership,
  getDomainByDomainName,
  getOrganizationMembershipDetailed,
  getApiKeyLogs
} from "../crud/organization";
import { InsertResult } from "../interfaces/mysql";
import {
  createMembership,
  getUserOrganizationMembership
} from "../crud/membership";
import {
  MembershipRole,
  ErrorCode,
  EventType,
  Webhooks,
  OrgScopes,
  Authorizations,
  Templates
} from "../interfaces/enum";
import { Locals, KeyValue } from "../interfaces/general";
import { can } from "../helpers/authorization";
import {
  getStripeCustomer,
  createStripeCustomer,
  updateStripeCustomer,
  getStripeInvoices,
  getStripeSubscriptions,
  getStripeProductPricing,
  getStripeSources,
  getStripeSource,
  createStripeSource,
  updateStripeSource,
  deleteStripeSource,
  deleteStripeCustomer,
  getStripeSubscription,
  updateStripeSubscription,
  getStripeInvoice,
  createStripeSubscription
} from "../crud/billing";
import { getUser, getUserByEmail } from "../crud/user";
import { getUserPrimaryEmail } from "../crud/email";
import { ApiKeyResponse } from "../helpers/jwt";
import axios from "axios";
import { dnsResolve } from "../helpers/utils";
import { JWT_ISSUER } from "../config";
import { queueWebhook } from "../helpers/webhooks";
import { User } from "../interfaces/tables/user";
import { register } from "./auth";
import { trackEvent } from "../helpers/tracking";
import { mail } from "../helpers/mail";

export const getOrganizationForUser = async (
  userId: string | ApiKeyResponse,
  organizationId: string
) => {
  if (await can(userId, OrgScopes.READ_ORG, "organization", organizationId))
    return await getOrganization(organizationId);
  throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

export const newOrganizationForUser = async (
  userId: string,
  organization: Organization,
  locals: Locals
) => {
  if (!organization.name) {
    const user = await getUser(userId);
    organization.name = `${user.name}'s Team`;
  }
  const org = <InsertResult>await createOrganization(organization);
  const organizationId = org.insertId;
  await createMembership({
    organizationId,
    userId,
    role: MembershipRole.OWNER
  });
  await createStripeCustomer(organizationId, {
    email: await getUserPrimaryEmail(userId),
    name: (await getUser(userId)).name
  });
  return;
};

export const updateOrganizationForUser = async (
  userId: string | ApiKeyResponse,
  organizationId: string,
  data: Organization,
  locals: Locals
) => {
  if (await can(userId, OrgScopes.UPDATE_ORG, "organization", organizationId)) {
    await updateOrganization(organizationId, data);
    queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION);
    trackEvent({ organizationId, type: Webhooks.UPDATE_ORGANIZATION }, locals);
    return;
  }
  throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

export const deleteOrganizationForUser = async (
  userId: string | ApiKeyResponse,
  organizationId: string,
  locals: Locals
) => {
  if (await can(userId, OrgScopes.DELETE_ORG, "organization", organizationId)) {
    const organizationDetails = await getOrganization(organizationId);
    if (organizationDetails.stripeCustomerId)<commit_msg>:recycle: Remove immediately create Stripe org<commit_after>import { Organization, Webhook } from "../interfaces/tables/organization";
import {
  createOrganization,
  updateOrganization,
  deleteOrganization,
  getOrganization,
  getOrganizationApiKeys,
  getApiKey,
  updateApiKey,
  createApiKey,
  deleteApiKey,
  getOrganizationDomains,
  getDomain,
  updateDomain,
  createDomain,
  deleteDomain,
  checkDomainAvailability,
  getOrganizationWebhooks,
  getWebhook,
  updateWebhook,
  createWebhook,
  deleteWebhook,
  getOrganizationMemberships,
  deleteAllOrganizationMemberships,
  updateOrganizationMembership,
  deleteOrganizationMembership,
  getDomainByDomainName,
  getOrganizationMembershipDetailed,
  getApiKeyLogs
} from "../crud/organization";
import { InsertResult } from "../interfaces/mysql";
import {
  createMembership,
  getUserOrganizationMembership
} from "../crud/membership";
import {
  MembershipRole,
  ErrorCode,
  EventType,
  Webhooks,
  OrgScopes,
  Authorizations,
  Templates
} from "../interfaces/enum";
import { Locals, KeyValue } from "../interfaces/general";
import { can } from "../helpers/authorization";
import {
  getStripeCustomer,
  createStripeCustomer,
  updateStripeCustomer,
  getStripeInvoices,
  getStripeSubscriptions,
  getStripeProductPricing,
  getStripeSources,
  getStripeSource,
  createStripeSource,
  updateStripeSource,
  deleteStripeSource,
  deleteStripeCustomer,
  getStripeSubscription,
  updateStripeSubscription,
  getStripeInvoice,
  createStripeSubscription
} from "../crud/billing";
import { getUser, getUserByEmail } from "../crud/user";
import { getUserPrimaryEmail } from "../crud/email";
import { ApiKeyResponse } from "../helpers/jwt";
import axios from "axios";
import { dnsResolve } from "../helpers/utils";
import { JWT_ISSUER } from "../config";
import { queueWebhook } from "../helpers/webhooks";
import { User } from "../interfaces/tables/user";
import { register } from "./auth";
import { trackEvent } from "../helpers/tracking";
import { mail } from "../helpers/mail";

export const getOrganizationForUser = async (
  userId: string | ApiKeyResponse,
  organizationId: string
) => {
  if (await can(userId, OrgScopes.READ_ORG, "organization", organizationId))
    return await getOrganization(organizationId);
  throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

export const newOrganizationForUser = async (
  userId: string,
  organization: Organization,
  locals: Locals
) => {
  if (!organization.name) {
    const user = await getUser(userId);
    organization.name = `${user.name}'s Team`;
  }
  const org = <InsertResult>await createOrganization(organization);
  const organizationId = org.insertId;
  await createMembership({
    organizationId,
    userId,
    role: MembershipRole.OWNER
  });
  return;
};

export const updateOrganizationForUser = async (
  userId: string | ApiKeyResponse,
  organizationId: string,
  data: Organization,
  locals: Locals
) => {
  if (await can(userId, OrgScopes.UPDATE_ORG, "organization", organizationId)) {
    await updateOrganization(organizationId, data);
    queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION);
    trackEvent({ organizationId, type: Webhooks.UPDATE_ORGANIZATION }, locals);
    return;
  }
  throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

export const deleteOrganizationForUser = async (
  userId: string | ApiKeyResponse,
  organizationId: string,
  locals: Locals
) => {
  if (await can(userId, OrgScopes.DELETE_ORG, "organization", organizationId)) {
    const organizationDetails = await getOrganization(organizationId);
    if (organizationDetails.stripeCustomerId)
{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.0, 'mean_word_length': 5.014646053702197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '3592815', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2303, 'n_words': 486}