Fix CSV import upsert (#12048)
Fixes https://github.com/twentyhq/twenty/issues/11864 and https://github.com/twentyhq/core-team-issues/issues/908 We should not send `createManyXXX` mutations with FE-forged ids in the payload if we want to do an upsert, because that 1) prevents records from being merged 2) triggers optimistic rendering while we can't know before-hand which records will actually be created and which records will only be updated Also noticed createdBy was being overriden even for records we are updating and not creating, which did not seem right, so fixed that too
This commit is contained in:
@ -266,15 +266,20 @@ export class GraphqlQueryCreateManyResolverService extends GraphqlQueryBaseResol
|
||||
for (const record of recordsToUpdate) {
|
||||
const recordId = record.id as string;
|
||||
|
||||
// TODO: we should align update and insert
|
||||
// For insert, formating is done in the server
|
||||
// While for update, formatting is done at the resolver level
|
||||
|
||||
const formattedRecord = formatData(
|
||||
// we should not update an existing record's createdBy value
|
||||
const recordWithoutCreatedByUpdate = this.getRecordWithoutCreatedBy(
|
||||
record,
|
||||
objectMetadataItemWithFieldMaps,
|
||||
);
|
||||
|
||||
const formattedRecord = formatData(
|
||||
recordWithoutCreatedByUpdate,
|
||||
objectMetadataItemWithFieldMaps,
|
||||
);
|
||||
|
||||
// TODO: we should align update and insert
|
||||
// For insert, formating is done in the server
|
||||
// While for update, formatting is done at the resolver level
|
||||
await repository.update(recordId, formattedRecord);
|
||||
result.identifiers.push({ id: recordId });
|
||||
result.generatedMaps.push({ id: recordId });
|
||||
@ -362,6 +367,25 @@ export class GraphqlQueryCreateManyResolverService extends GraphqlQueryBaseResol
|
||||
);
|
||||
}
|
||||
|
||||
private getRecordWithoutCreatedBy(
|
||||
record: Partial<ObjectRecord>,
|
||||
objectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps,
|
||||
) {
|
||||
let recordWithoutCreatedByUpdate = record;
|
||||
|
||||
if (
|
||||
'createdBy' in record &&
|
||||
objectMetadataItemWithFieldMaps.fieldsByName['createdBy']?.isCustom ===
|
||||
false
|
||||
) {
|
||||
const { createdBy: _createdBy, ...recordWithoutCreatedBy } = record;
|
||||
|
||||
recordWithoutCreatedByUpdate = recordWithoutCreatedBy;
|
||||
}
|
||||
|
||||
return recordWithoutCreatedByUpdate;
|
||||
}
|
||||
|
||||
async validate<T extends ObjectRecord>(
|
||||
args: CreateManyResolverArgs<Partial<T>>,
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
|
||||
Reference in New Issue
Block a user