Fix record position on contact creation (#5227)

Fix record position on contact creation

---------

Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
bosiraphael
2024-04-30 17:09:29 +02:00
committed by GitHub
parent 9ad68ffe84
commit 3bf9045990
3 changed files with 65 additions and 21 deletions

View File

@ -36,6 +36,23 @@ export class CompanyRepository {
return existingCompanies; return existingCompanies;
} }
public async getLastCompanyPosition(
workspaceId: string,
transactionManager?: EntityManager,
): Promise<number> {
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( public async createCompany(
workspaceId: string, workspaceId: string,
companyToCreate: CompanyToCreate, companyToCreate: CompanyToCreate,
@ -44,14 +61,20 @@ export class CompanyRepository {
const dataSourceSchema = const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId); this.workspaceDataSourceService.getSchemaName(workspaceId);
const lastCompanyPosition = await this.getLastCompanyPosition(
workspaceId,
transactionManager,
);
await this.workspaceDataSourceService.executeRawQuery( await this.workspaceDataSourceService.executeRawQuery(
`INSERT INTO ${dataSourceSchema}.company (id, "domainName", name, address) `INSERT INTO ${dataSourceSchema}.company (id, "domainName", name, address, position)
VALUES ($1, $2, $3, $4)`, VALUES ($1, $2, $3, $4, $5)`,
[ [
companyToCreate.id, companyToCreate.id,
companyToCreate.domainName, companyToCreate.domainName,
companyToCreate.name ?? '', companyToCreate.name ?? '',
companyToCreate.city ?? '', companyToCreate.city ?? '',
lastCompanyPosition + 1,
], ],
workspaceId, workspaceId,
transactionManager, transactionManager,

View File

@ -85,7 +85,7 @@ export class CreateCompanyService {
const { name, city } = await this.getCompanyInfoFromDomainName(domainName); const { name, city } = await this.getCompanyInfoFromDomainName(domainName);
this.companyRepository.createCompany( await this.companyRepository.createCompany(
workspaceId, workspaceId,
{ {
id: companyId, id: companyId,

View File

@ -5,6 +5,7 @@ import { EntityManager } from 'typeorm';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record'; 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 { 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() @Injectable()
export class PersonRepository { export class PersonRepository {
@ -28,6 +29,23 @@ export class PersonRepository {
); );
} }
async getLastPersonPosition(
workspaceId: string,
transactionManager?: EntityManager,
): Promise<number> {
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( async createPeople(
peopleToCreate: { peopleToCreate: {
id: string; id: string;
@ -42,26 +60,29 @@ export class PersonRepository {
const dataSourceSchema = const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId); this.workspaceDataSourceService.getSchemaName(workspaceId);
const valuesString = peopleToCreate const lastPersonPosition = await this.getLastPersonPosition(
.map( workspaceId,
(_, index) => transactionManager,
`($${index * 5 + 1}, $${index * 5 + 2}, $${index * 5 + 3}, $${ );
index * 5 + 4
}, $${index * 5 + 5})`, peopleToCreate = peopleToCreate.map((contact, index) => ({
) ...contact,
.join(', '); 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( return await this.workspaceDataSourceService.executeRawQuery(
`INSERT INTO ${dataSourceSchema}.person (id, email, "nameFirstName", "nameLastName", "companyId") VALUES ${valuesString}`, `INSERT INTO ${dataSourceSchema}.person (id, email, "nameFirstName", "nameLastName", "companyId", "position") VALUES ${valuesString}`,
peopleToCreate flattenedValues,
.map((contact) => [
contact.id,
contact.handle,
contact.firstName,
contact.lastName,
contact.companyId,
])
.flat(),
workspaceId, workspaceId,
transactionManager, transactionManager,
); );