Display a CTA to view the existing duplicate when adding a duplicate email/domain. (#16483)

Closes [89](https://github.com/twentyhq/core-team-issues/issues/89)

## Problem

When users attempt to create or update a record with a duplicate value
for a unique field (e.g., duplicate email or domain name), they receive
a generic error message: "This record already exists. Please check your
data and try again." This provides no actionable way to locate and view
the existing conflicting record, forcing users to manually search for
it.

## Solution

This PR enhances duplicate key constraint error handling to
automatically detect the conflicting record and display a "View existing
record" link in the error notification. When clicked, users are
navigated directly to the existing record's detail page.

## Backend Changes

### 1. PostgreSQL Error Parsing
(`parse-postgres-constraint-error.util.ts`)
- Extracts structured information from PostgreSQL `QueryFailedError`
messages.

### 2. Conflicting Record Lookup (`find-conflicting-record.util.ts`)
- Queries the database to find the existing record with the conflicting
value

### 3. Error Handling Orchestration
(`handle-duplicate-key-error.util.ts`)
- Parses PostgreSQL error to extract column name and conflicting value
- Attempts to find the conflicting record
- Enriches `TwentyORMException` with `conflictingRecordId` and
`conflictingObjectNameSingular` if found

### 4. Exception Computation Updates (`compute-twenty-orm-exception.ts`)
- Made function `async` and added optional `entityManager` and
`internalContext` parameters
- Needed to support async database queries for conflicting record lookup

### 5. GraphQL Error Handler
(`twenty-orm-graphql-api-exception-handler.util.ts`)
- **Changes**: Enhanced `DUPLICATE_ENTRY_DETECTED` case to include
`conflictingRecordId` and `conflictingObjectNameSingular` in GraphQL
error extensions

## Frontend Changes

### 1. Error Extraction Utility
(`get-conflicting-record-from-apollo-error.util.ts`)
- Accesses GraphQL error extensions
- Validates that both `conflictingRecordId` and
`conflictingObjectNameSingular` exist and are strings
- Returns `null` if validation fails

### 2. SnackBar Enhancement (`useSnackBar.ts`)
- Extracts conflicting record info from Apollo error
- Constructs URL using `getAppPath` utility
- Adds link object to snackbar options with text "View existing record"

<img width="931" height="858" alt="image"
src="https://github.com/user-attachments/assets/28137dc7-18ab-4ffe-b669-1f2d4ec264d1"
/>
This commit is contained in:
Abdullah.
2025-12-16 18:27:34 +05:00
committed by GitHub
parent 75bba5a8ee
commit d998e3b92c
16 changed files with 335 additions and 41 deletions
@@ -140,7 +140,7 @@ export class WorkspaceDeleteQueryBuilder<
affected: result.affected,
};
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -226,7 +226,12 @@ export class WorkspaceInsertQueryBuilder<
this.internalContext,
);
throw computeTwentyORMException(error, objectMetadata);
throw await computeTwentyORMException(
error,
objectMetadata,
this.connection.manager as WorkspaceEntityManager,
this.internalContext,
);
}
}
@@ -95,7 +95,7 @@ export class WorkspaceSelectQueryBuilder<
identifiers: result.identifiers,
};
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -121,29 +121,29 @@ export class WorkspaceSelectQueryBuilder<
return formattedResult;
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
override getRawOne<U = any>(): Promise<U | undefined> {
override async getRawOne<U = any>(): Promise<U | undefined> {
try {
this.validatePermissions();
return super.getRawOne();
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
override getRawMany<U = any>(): Promise<U[]> {
override async getRawMany<U = any>(): Promise<U[]> {
try {
this.validatePermissions();
return super.getRawMany();
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -171,7 +171,7 @@ export class WorkspaceSelectQueryBuilder<
return formattedResult;
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -197,17 +197,17 @@ export class WorkspaceSelectQueryBuilder<
return formattedResult[0];
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
override getCount(): Promise<number> {
override async getCount(): Promise<number> {
try {
this.validatePermissions();
return super.getCount();
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -240,7 +240,7 @@ export class WorkspaceSelectQueryBuilder<
return [formattedResult, count];
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -140,7 +140,7 @@ export class WorkspaceSoftDeleteQueryBuilder<
affected: after.affected,
};
} catch (error) {
throw computeTwentyORMException(error);
throw await computeTwentyORMException(error);
}
}
@@ -19,6 +19,7 @@ import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-contex
import { type QueryDeepPartialEntityWithNestedRelationFields } from 'src/engine/twenty-orm/entity-manager/types/query-deep-partial-entity-with-nested-relation-fields.type';
import { type RelationConnectQueryConfig } from 'src/engine/twenty-orm/entity-manager/types/relation-connect-query-config.type';
import { type RelationDisconnectQueryFieldsByEntityIndex } from 'src/engine/twenty-orm/entity-manager/types/relation-nested-query-fields-by-entity-index.type';
import { type WorkspaceEntityManager } from 'src/engine/twenty-orm/entity-manager/workspace-entity-manager';
import { computeTwentyORMException } from 'src/engine/twenty-orm/error-handling/compute-twenty-orm-exception';
import {
TwentyORMException,
@@ -223,7 +224,12 @@ export class WorkspaceUpdateQueryBuilder<
this.internalContext,
);
throw computeTwentyORMException(error, objectMetadata);
throw await computeTwentyORMException(
error,
objectMetadata,
this.connection.manager as WorkspaceEntityManager,
this.internalContext,
);
}
}
@@ -373,7 +379,12 @@ export class WorkspaceUpdateQueryBuilder<
this.internalContext,
);
throw computeTwentyORMException(error, objectMetadata);
throw await computeTwentyORMException(
error,
objectMetadata,
this.connection.manager as WorkspaceEntityManager,
this.internalContext,
);
}
}