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:
@ -36,6 +36,23 @@ export class CompanyRepository {
|
||||
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(
|
||||
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,
|
||||
|
||||
@ -85,7 +85,7 @@ export class CreateCompanyService {
|
||||
|
||||
const { name, city } = await this.getCompanyInfoFromDomainName(domainName);
|
||||
|
||||
this.companyRepository.createCompany(
|
||||
await this.companyRepository.createCompany(
|
||||
workspaceId,
|
||||
{
|
||||
id: companyId,
|
||||
|
||||
@ -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<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(
|
||||
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,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user