Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert endpoint and CSV import upsert #5970

Merged
merged 21 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/twenty-front/codegen-metadata.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
schema: process.env.REACT_APP_SERVER_BASE_URL + '/metadata',
schema: (process.env.REACT_APP_SERVER_BASE_URL ?? 'http://localhost:3000') + '/metadata',
documents: [
'./src/modules/databases/graphql/**/*.ts',
'./src/modules/object-metadata/graphql/*.ts',
Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-front/codegen.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
schema: process.env.REACT_APP_SERVER_BASE_URL + '/graphql',
schema: (process.env.REACT_APP_SERVER_BASE_URL ?? 'http://localhost:3000') + '/graphql',
documents: [
'!./src/modules/databases/**',
'!./src/modules/object-metadata/**',
Expand Down
20 changes: 11 additions & 9 deletions packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ export type Mutation = {
deleteCurrentWorkspace: Workspace;
deleteOneObject: Object;
deleteUser: User;
disablePostgresProxy: PostgresCredentials;
emailPasswordResetLink: EmailPasswordResetLink;
enablePostgresProxy: PostgresCredentials;
exchangeAuthorizationCode: ExchangeAuthCode;
generateApiKeyToken: ApiKeyToken;
generateJWT: AuthTokens;
Expand Down Expand Up @@ -483,6 +485,14 @@ export type PageInfo = {
startCursor?: Maybe<Scalars['ConnectionCursor']>;
};

export type PostgresCredentials = {
__typename?: 'PostgresCredentials';
id: Scalars['UUID'];
password: Scalars['String'];
user: Scalars['String'];
workspaceId: Scalars['String'];
};

export type ProductPriceEntity = {
__typename?: 'ProductPriceEntity';
created: Scalars['Float'];
Expand All @@ -506,6 +516,7 @@ export type Query = {
currentUser: User;
currentWorkspace: Workspace;
findWorkspaceFromInviteHash: Workspace;
getPostgresCredentials?: Maybe<PostgresCredentials>;
getProductPrices: ProductPricesEntity;
getTimelineCalendarEventsFromCompanyId: TimelineCalendarEventsWithTotal;
getTimelineCalendarEventsFromPersonId: TimelineCalendarEventsWithTotal;
Expand Down Expand Up @@ -1061,8 +1072,6 @@ export type GetTimelineThreadsFromPersonIdQueryVariables = Exact<{

export type GetTimelineThreadsFromPersonIdQuery = { __typename?: 'Query', getTimelineThreadsFromPersonId: { __typename?: 'TimelineThreadsWithTotal', totalNumberOfThreads: number, timelineThreads: Array<{ __typename?: 'TimelineThread', id: any, read: boolean, visibility: MessageChannelVisibility, lastMessageReceivedAt: string, lastMessageBody: string, subject: string, numberOfMessagesInThread: number, participantCount: number, firstParticipant: { __typename?: 'TimelineThreadParticipant', personId?: any | null, workspaceMemberId?: any | null, firstName: string, lastName: string, displayName: string, avatarUrl: string, handle: string }, lastTwoParticipants: Array<{ __typename?: 'TimelineThreadParticipant', personId?: any | null, workspaceMemberId?: any | null, firstName: string, lastName: string, displayName: string, avatarUrl: string, handle: string }> }> } };

export type TimelineThreadFragment = { __typename?: 'TimelineThread', id: any, subject: string, lastMessageReceivedAt: string };

export type TrackMutationVariables = Exact<{
type: Scalars['String'];
data: Scalars['JSON'];
Expand Down Expand Up @@ -1364,13 +1373,6 @@ export const TimelineThreadsWithTotalFragmentFragmentDoc = gql`
}
}
${TimelineThreadFragmentFragmentDoc}`;
export const TimelineThreadFragmentDoc = gql`
fragment timelineThread on TimelineThread {
id
subject
lastMessageReceivedAt
}
`;
export const AuthTokenFragmentFragmentDoc = gql`
fragment AuthTokenFragment on AuthToken {
token
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';

export type RecordGqlOperationFindDuplicatesResult = {
[objectNamePlural: string]: RecordGqlConnection[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gql } from '@apollo/client';
import { Person } from '@/people/types/Person';

export const query = gql`
mutation CreatePeople($data: [PersonCreateInput!]!) {
mutation CreatePeople($data: [PersonCreateInput!]!, $upsert: Boolean) {
createPeople(data: $data) {
__typename
xLink {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('useFindDuplicateRecords', () => {
const { result } = renderHook(
() =>
useFindDuplicateRecords({
objectRecordId,
objectRecordIds: [objectRecordId],
objectNameSingular,
}),
{
Expand All @@ -54,7 +54,7 @@ describe('useFindDuplicateRecords', () => {

await waitFor(() => {
expect(result.current.loading).toBe(false);
expect(result.current.records).toBeDefined();
expect(result.current.results).toBeDefined();
});

expect(mocks[0].result).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ export const useCreateManyRecords = <

const createManyRecords = async (
recordsToCreate: Partial<CreatedObjectRecord>[],
upsert?: boolean,
) => {
const sanitizedCreateManyRecordsInput = recordsToCreate.map(
(recordToCreate) => {
const idForCreation = recordToCreate?.id ?? v4();
const idForCreation = recordToCreate?.id ?? (upsert ? undefined : v4());

return {
...sanitizeRecordInput({
Expand All @@ -67,8 +68,12 @@ export const useCreateManyRecords = <
const recordsCreatedInCache = [];

for (const recordToCreate of sanitizedCreateManyRecordsInput) {
if (recordToCreate.id === null) {
continue;
}

const recordCreatedInCache = createOneRecordInCache({
...recordToCreate,
...(recordToCreate as { id: string }),
__typename: getObjectTypename(objectMetadataItem.nameSingular),
});

Expand All @@ -94,6 +99,7 @@ export const useCreateManyRecords = <
mutation: createManyRecordsMutation,
variables: {
data: sanitizedCreateManyRecordsInput,
upsert: upsert,
},
update: (cache, { data }) => {
const records = data?.[mutationResponseField];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ export const useCreateManyRecordsMutation = ({
const createManyRecordsMutation = gql`
mutation Create${capitalize(
objectMetadataItem.namePlural,
)}($data: [${capitalize(objectMetadataItem.nameSingular)}CreateInput!]!) {
${mutationResponseField}(data: $data) ${mapObjectMetadataToGraphQLQuery({
objectMetadataItems,
objectMetadataItem,
recordGqlFields,
})}
)}($data: [${capitalize(
objectMetadataItem.nameSingular,
)}CreateInput!]!, $upsert: Boolean) {
${mutationResponseField}(data: $data, upsert: $upsert) ${mapObjectMetadataToGraphQLQuery(
{
objectMetadataItems,
objectMetadataItem,
recordGqlFields,
},
)}
}`;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadata
import { ObjectMetadataItemIdentifier } from '@/object-metadata/types/ObjectMetadataItemIdentifier';
import { getRecordsFromRecordConnection } from '@/object-record/cache/utils/getRecordsFromRecordConnection';
import { RecordGqlConnection } from '@/object-record/graphql/types/RecordGqlConnection';
import { RecordGqlOperationFindManyResult } from '@/object-record/graphql/types/RecordGqlOperationFindManyResult';
import { RecordGqlOperationFindDuplicatesResult } from '@/object-record/graphql/types/RecordGqlOperationFindDuplicatesResults';
import { useFindDuplicateRecordsQuery } from '@/object-record/hooks/useFindDuplicatesRecordsQuery';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { getFindDuplicateRecordsQueryResponseField } from '@/object-record/utils/getFindDuplicateRecordsQueryResponseField';
Expand All @@ -14,12 +14,12 @@ import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { logError } from '~/utils/logError';

export const useFindDuplicateRecords = <T extends ObjectRecord = ObjectRecord>({
objectRecordId = '',
objectRecordIds = [],
objectNameSingular,
onCompleted,
}: ObjectMetadataItemIdentifier & {
objectRecordId: string | undefined;
onCompleted?: (data: RecordGqlConnection) => void;
objectRecordIds: string[] | undefined;
onCompleted?: (data: RecordGqlConnection[]) => void;
skip?: boolean;
}) => {
const findDuplicateQueryStateIdentifier = objectNameSingular;
Expand All @@ -38,46 +38,48 @@ export const useFindDuplicateRecords = <T extends ObjectRecord = ObjectRecord>({
objectMetadataItem.nameSingular,
);

const { data, loading, error } = useQuery<RecordGqlOperationFindManyResult>(
findDuplicateRecordsQuery,
{
variables: {
id: objectRecordId,
const { data, loading, error } =
useQuery<RecordGqlOperationFindDuplicatesResult>(
findDuplicateRecordsQuery,
{
variables: {
ids: objectRecordIds,
},
onCompleted: (data) => {
onCompleted?.(data[queryResponseField]);
},
onError: (error) => {
logError(
`useFindDuplicateRecords for "${objectMetadataItem.nameSingular}" error : ` +
error,
);
enqueueSnackBar(
`Error during useFindDuplicateRecords for "${objectMetadataItem.nameSingular}", ${error.message}`,
{
variant: SnackBarVariant.Error,
},
);
},
},
onCompleted: (data) => {
onCompleted?.(data[queryResponseField]);
},
onError: (error) => {
logError(
`useFindDuplicateRecords for "${objectMetadataItem.nameSingular}" error : ` +
error,
);
enqueueSnackBar(
`Error during useFindDuplicateRecords for "${objectMetadataItem.nameSingular}", ${error.message}`,
{
variant: SnackBarVariant.Error,
},
);
},
},
);
);

const objectRecordConnection = data?.[queryResponseField];
const objectResults = data?.[queryResponseField];

const records = useMemo(
const results = useMemo(
() =>
objectRecordConnection
? (getRecordsFromRecordConnection({
recordConnection: objectRecordConnection,
}) as T[])
: [],
[objectRecordConnection],
objectResults?.map((result: RecordGqlConnection) => {
return result
? (getRecordsFromRecordConnection({
recordConnection: result,
}) as T[])
: [];
}),
[objectResults],
);

return {
objectMetadataItem,
records,
totalCount: objectRecordConnection?.totalCount,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to totalCount? 😄 We don't need it anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's within the query itself, the count here would now be equal to the number of ids you sent, I guess if you ask for totalCount as a field within the query fields it should work but I didn't try :-/

results,
loading,
error,
queryStateIdentifier: findDuplicateQueryStateIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export const useFindDuplicateRecordsQuery = ({
const findDuplicateRecordsQuery = gql`
query FindDuplicate${capitalize(
objectMetadataItem.nameSingular,
)}($id: ID!) {
)}($ids: [ID]!) {
${getFindDuplicateRecordsQueryResponseField(
objectMetadataItem.nameSingular,
)}(id: $id) {
)}(ids: $ids) {
edges {
node ${mapObjectMetadataToGraphQLQuery({
objectMetadataItems,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('generateCsv', () => {
},
];
const csv = generateCsv({ columns, rows });
expect(csv).toEqual(`id,Foo,Empty,Nested Foo,Nested Nested,Relation
expect(csv).toEqual(`Id,Foo,Empty,Nested Foo,Nested Nested,Relation
1,some field,,foo,nested,a relation`);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ export const RecordDetailDuplicatesSection = ({
objectRecordId: string;
objectNameSingular: string;
}) => {
const { records: duplicateRecords } = useFindDuplicateRecords({
objectRecordId,
const { results: queryResults } = useFindDuplicateRecords({
objectRecordIds: [objectRecordId],
objectNameSingular,
});

if (!duplicateRecords.length) return null;
if (!queryResults || !queryResults[0] || queryResults[0].length === 0)
Weiko marked this conversation as resolved.
Show resolved Hide resolved
return null;

return (
<RecordDetailSection>
<RecordDetailSectionHeader title="Duplicates" />
<RecordDetailRecordsList>
{duplicateRecords.slice(0, 5).map((duplicateRecord) => (
{queryResults[0].slice(0, 5).map((duplicateRecord) => (
<RecordDetailRecordsListItem key={duplicateRecord.id}>
<RecordChip
record={duplicateRecord}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => {
.filter(
(x) =>
x.isActive &&
!x.isSystem &&
(!x.isSystem || x.name === 'id') &&
x.name !== 'createdAt' &&
(x.type !== FieldMetadataType.Relation || x.toRelationMetadata),
)
Expand Down Expand Up @@ -110,11 +110,15 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => {

switch (field.type) {
case FieldMetadataType.Boolean:
fieldMapping[field.name] = value === 'true' || value === true;
if (value !== undefined) {
fieldMapping[field.name] = value === 'true' || value === true;
}
break;
case FieldMetadataType.Number:
case FieldMetadataType.Numeric:
fieldMapping[field.name] = Number(value);
if (value !== undefined) {
fieldMapping[field.name] = Number(value);
}
break;
case FieldMetadataType.Currency:
if (value !== undefined) {
Expand Down Expand Up @@ -154,14 +158,16 @@ export const useSpreadsheetRecordImport = (objectNameSingular: string) => {
}
break;
default:
fieldMapping[field.name] = value;
if (value !== undefined) {
fieldMapping[field.name] = value;
}
break;
}
}
return fieldMapping;
});
try {
await createManyRecords(createInputs);
await createManyRecords(createInputs, true);
} catch (error: any) {
enqueueSnackBar(error?.message || 'Something went wrong', {
variant: SnackBarVariant.Error,
Expand Down
Loading
Loading