Skip to content

The open alternative to Salesforce, designed for AI.

3.3 KB
Raw Blame
import { Injectable } from '@nestjs/common';

import { type FullNameMetadata } from 'twenty-shared/types';
import { DeepPartial } from 'typeorm';

import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';
import { type WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace-repository';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';

@Injectable()
export class CreatePersonService {
  constructor(private readonly workspaceOrmManager: WorkspaceOrmManager) {}

  public async createPeople(
    peopleToCreate: Partial<PersonWorkspaceEntity>[],
    workspaceId: string,
  ): Promise<DeepPartial<PersonWorkspaceEntity>[]> {
    if (peopleToCreate.length === 0) return [];

    const authContext = buildSystemAuthContext(workspaceId);

    return this.workspaceOrmManager.executeInWorkspaceContext(async () => {
      const personRepository =
        this.workspaceOrmManager.getRepositoryWithoutPermissionChecks(
          PersonWorkspaceEntity,
        );

      const lastPersonPosition =
        await this.getLastPersonPosition(personRepository);

      const createdPeople = await personRepository.insert(
        peopleToCreate.map((person, index) => ({
          ...person,
          position: lastPersonPosition + index,
        })),
      );

      return createdPeople.raw;
    }, authContext);
  }

  public async restorePeople(
    people: { personId: string; companyId: string | undefined }[],
    workspaceId: string,
  ): Promise<DeepPartial<PersonWorkspaceEntity>[]> {
    if (people.length === 0) {
      return [];
    }

    const authContext = buildSystemAuthContext(workspaceId);

    return this.workspaceOrmManager.executeInWorkspaceContext(async () => {
      const personRepository =
        this.workspaceOrmManager.getRepositoryWithoutPermissionChecks(
          PersonWorkspaceEntity,
        );

      const restoredPeople = await personRepository.updateMany(
        people.map(({ personId, companyId }) => ({
          criteria: personId,
          partialEntity: {
            deletedAt: null,
            companyId,
          },
        })),
      );

      return restoredPeople.raw;
    }, authContext);
  }

  public async enrichPeopleNames(
    peopleToEnrich: { personId: string; name: FullNameMetadata }[],
    workspaceId: string,
  ): Promise<DeepPartial<PersonWorkspaceEntity>[]> {
    if (peopleToEnrich.length === 0) {
      return [];
    }

    const authContext = buildSystemAuthContext(workspaceId);

    return this.workspaceOrmManager.executeInWorkspaceContext(async () => {
      const personRepository =
        this.workspaceOrmManager.getRepositoryWithoutPermissionChecks(
          PersonWorkspaceEntity,
        );

      const enrichedPeople = await personRepository.updateMany(
        peopleToEnrich.map(({ personId, name }) => ({
          criteria: personId,
          partialEntity: { name },
        })),
      );

      return enrichedPeople.raw;
    }, authContext);
  }

  private async getLastPersonPosition(
    personRepository: WorkspaceRepository<PersonWorkspaceEntity>,
  ): Promise<number> {
    const lastPersonPosition = await personRepository.maximum(
      'position',
      undefined,
    );

    return lastPersonPosition ?? 0;
  }
}