From 3bf904599088f88d5c324b7ef82945c9f9abc1d8 Mon Sep 17 00:00:00 2001 From: bosiraphael <71827178+bosiraphael@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:09:29 +0200 Subject: [PATCH] Fix record position on contact creation (#5227) Fix record position on contact creation --------- Co-authored-by: Weiko --- .../repositories/company.repository.ts | 27 ++++++++- .../create-company/create-company.service.ts | 2 +- .../person/repositories/person.repository.ts | 57 +++++++++++++------ 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/packages/twenty-server/src/modules/company/repositories/company.repository.ts b/packages/twenty-server/src/modules/company/repositories/company.repository.ts index 162ee88c8..716a7a5bc 100644 --- a/packages/twenty-server/src/modules/company/repositories/company.repository.ts +++ b/packages/twenty-server/src/modules/company/repositories/company.repository.ts @@ -36,6 +36,23 @@ export class CompanyRepository { return existingCompanies; } + public async getLastCompanyPosition( + workspaceId: string, + transactionManager?: EntityManager, + ): Promise { + const dataSourceSchema = + this.workspaceDataSourceService.getSchemaName(workspaceId); + + const result = await this.workspaceDataSourceService.executeRawQuery( + `SELECT MAX(position) FROM ${dataSourceSchema}.company`, + [], + workspaceId, + transactionManager, + ); + + return result[0].max ?? 0; + } + public async createCompany( workspaceId: string, companyToCreate: CompanyToCreate, @@ -44,14 +61,20 @@ export class CompanyRepository { const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(workspaceId); + const lastCompanyPosition = await this.getLastCompanyPosition( + workspaceId, + transactionManager, + ); + await this.workspaceDataSourceService.executeRawQuery( - `INSERT INTO ${dataSourceSchema}.company (id, "domainName", name, address) - VALUES ($1, $2, $3, $4)`, + `INSERT INTO ${dataSourceSchema}.company (id, "domainName", name, address, position) + VALUES ($1, $2, $3, $4, $5)`, [ companyToCreate.id, companyToCreate.domainName, companyToCreate.name ?? '', companyToCreate.city ?? '', + lastCompanyPosition + 1, ], workspaceId, transactionManager, diff --git a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.service.ts b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.service.ts index c7f1e9a89..27af8f98d 100644 --- a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.service.ts +++ b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.service.ts @@ -85,7 +85,7 @@ export class CreateCompanyService { const { name, city } = await this.getCompanyInfoFromDomainName(domainName); - this.companyRepository.createCompany( + await this.companyRepository.createCompany( workspaceId, { id: companyId, diff --git a/packages/twenty-server/src/modules/person/repositories/person.repository.ts b/packages/twenty-server/src/modules/person/repositories/person.repository.ts index 656c524fa..cc66dc1a3 100644 --- a/packages/twenty-server/src/modules/person/repositories/person.repository.ts +++ b/packages/twenty-server/src/modules/person/repositories/person.repository.ts @@ -5,6 +5,7 @@ import { EntityManager } from 'typeorm'; import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record'; import { PersonObjectMetadata } from 'src/modules/person/standard-objects/person.object-metadata'; +import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util'; @Injectable() export class PersonRepository { @@ -28,6 +29,23 @@ export class PersonRepository { ); } + async getLastPersonPosition( + workspaceId: string, + transactionManager?: EntityManager, + ): Promise { + const dataSourceSchema = + this.workspaceDataSourceService.getSchemaName(workspaceId); + + const result = await this.workspaceDataSourceService.executeRawQuery( + `SELECT MAX(position) FROM ${dataSourceSchema}.person`, + [], + workspaceId, + transactionManager, + ); + + return result[0].max ?? 0; + } + async createPeople( peopleToCreate: { id: string; @@ -42,26 +60,29 @@ export class PersonRepository { const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(workspaceId); - const valuesString = peopleToCreate - .map( - (_, index) => - `($${index * 5 + 1}, $${index * 5 + 2}, $${index * 5 + 3}, $${ - index * 5 + 4 - }, $${index * 5 + 5})`, - ) - .join(', '); + const lastPersonPosition = await this.getLastPersonPosition( + workspaceId, + transactionManager, + ); + + peopleToCreate = peopleToCreate.map((contact, index) => ({ + ...contact, + position: lastPersonPosition + index + 1, + })); + + const { flattenedValues, valuesString } = + getFlattenedValuesAndValuesStringForBatchRawQuery(peopleToCreate, { + id: 'uuid', + handle: 'text', + firstName: 'text', + lastName: 'text', + companyId: 'uuid', + position: 'double precision', + }); return await this.workspaceDataSourceService.executeRawQuery( - `INSERT INTO ${dataSourceSchema}.person (id, email, "nameFirstName", "nameLastName", "companyId") VALUES ${valuesString}`, - peopleToCreate - .map((contact) => [ - contact.id, - contact.handle, - contact.firstName, - contact.lastName, - contact.companyId, - ]) - .flat(), + `INSERT INTO ${dataSourceSchema}.person (id, email, "nameFirstName", "nameLastName", "companyId", "position") VALUES ${valuesString}`, + flattenedValues, workspaceId, transactionManager, );