Expose duplicate check on REST API and enable batch duplicate checks (#6328)

This PR was created by [GitStart](https://gitstart.com/) to address the
requirements from this ticket:
[TWNTY-5472](https://clients.gitstart.com/twenty/5449/tickets/TWNTY-5472).
This ticket was imported from:
[TWNTY-5472](https://github.com/twentyhq/twenty/issues/5472)

 --- 

### Description:
- Following what is already done in the code, we create a REST endpoint
that generates the Graphql query and returns its result.

### Refs: #5472


### Demo:

<https://www.loom.com/share/e0b1030f056945a0bf93bdd88ea01d8f?sid=6f128e8c-370b-4079-958e-0ea2d073a241>

FIxes #5472

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: gitstart-twenty <140154534+gitstart-twenty@users.noreply.github.com>
Co-authored-by: martmull <martmull@hotmail.fr>
This commit is contained in:
gitstart-app[bot]
2024-08-01 10:08:22 +02:00
committed by GitHub
parent c3417ddba1
commit d9dcd63a1c
13 changed files with 329 additions and 49 deletions

View File

@ -4,17 +4,7 @@ import { Request } from 'express';
import { OpenAPIV3_1 } from 'openapi-types';
import { TokenService } from 'src/engine/core-modules/auth/services/token.service';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { baseSchema } from 'src/engine/core-modules/open-api/utils/base-schema.utils';
import {
computeBatchPath,
computeManyResultPath,
computeSingleResultPath,
} from 'src/engine/core-modules/open-api/utils/path.utils';
import {
get400ErrorResponses,
get401ErrorResponses,
} from 'src/engine/core-modules/open-api/utils/get-error-responses.utils';
import {
computeMetadataSchemaComponents,
computeParameterComponents,
@ -22,16 +12,27 @@ import {
} from 'src/engine/core-modules/open-api/utils/components.utils';
import { computeSchemaTags } from 'src/engine/core-modules/open-api/utils/compute-schema-tags.utils';
import { computeWebhooks } from 'src/engine/core-modules/open-api/utils/computeWebhooks.utils';
import { capitalize } from 'src/utils/capitalize';
import {
get400ErrorResponses,
get401ErrorResponses,
} from 'src/engine/core-modules/open-api/utils/get-error-responses.utils';
import {
computeBatchPath,
computeDuplicatesResultPath,
computeManyResultPath,
computeSingleResultPath,
} from 'src/engine/core-modules/open-api/utils/path.utils';
import { getRequestBody } from 'src/engine/core-modules/open-api/utils/request-body.utils';
import {
getCreateOneResponse201,
getDeleteResponse200,
getFindManyResponse200,
getCreateOneResponse201,
getFindOneResponse200,
getUpdateOneResponse200,
} from 'src/engine/core-modules/open-api/utils/responses.utils';
import { getRequestBody } from 'src/engine/core-modules/open-api/utils/request-body.utils';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { capitalize } from 'src/utils/capitalize';
import { getServerUrl } from 'src/utils/get-server-url';
@Injectable()
@ -68,6 +69,8 @@ export class OpenApiService {
paths[`/${item.namePlural}`] = computeManyResultPath(item);
paths[`/batch/${item.namePlural}`] = computeBatchPath(item);
paths[`/${item.namePlural}/{id}`] = computeSingleResultPath(item);
paths[`/${item.namePlural}/duplicates`] =
computeDuplicatesResultPath(item);
return paths;
}, schema.paths as OpenAPIV3_1.PathsObject);

View File

@ -1,20 +1,22 @@
import { OpenAPIV3_1 } from 'openapi-types';
import { capitalize } from 'src/utils/capitalize';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import {
getDeleteResponse200,
getJsonResponse,
getFindManyResponse200,
getCreateOneResponse201,
getCreateManyResponse201,
getFindOneResponse200,
getUpdateOneResponse200,
} from 'src/engine/core-modules/open-api/utils/responses.utils';
import {
getArrayRequestBody,
getFindDuplicatesRequestBody,
getRequestBody,
} from 'src/engine/core-modules/open-api/utils/request-body.utils';
import {
getCreateManyResponse201,
getCreateOneResponse201,
getDeleteResponse200,
getFindDuplicatesResponse200,
getFindManyResponse200,
getFindOneResponse200,
getJsonResponse,
getUpdateOneResponse200,
} from 'src/engine/core-modules/open-api/utils/responses.utils';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { capitalize } from 'src/utils/capitalize';
export const computeBatchPath = (
item: ObjectMetadataEntity,
@ -140,3 +142,23 @@ export const computeOpenApiPath = (
},
} as OpenAPIV3_1.PathItemObject;
};
export const computeDuplicatesResultPath = (
item: ObjectMetadataEntity,
): OpenAPIV3_1.PathItemObject => {
return {
post: {
tags: [item.namePlural],
summary: `Find ${item.nameSingular} Duplicates`,
description: `**depth** can be provided to request your **${item.nameSingular}**`,
operationId: `find${capitalize(item.nameSingular)}Duplicates`,
parameters: [{ $ref: '#/components/parameters/depth' }],
requestBody: getFindDuplicatesRequestBody(capitalize(item.nameSingular)),
responses: {
'200': getFindDuplicatesResponse200(item),
'400': { $ref: '#/components/responses/400' },
'401': { $ref: '#/components/responses/401' },
},
},
} as OpenAPIV3_1.PathItemObject;
};

View File

@ -27,3 +27,32 @@ export const getArrayRequestBody = (name: string) => {
},
};
};
export const getFindDuplicatesRequestBody = (name: string) => {
return {
description: 'body',
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
data: {
type: 'array',
items: {
$ref: `#/components/schemas/${name}`,
},
},
ids: {
type: 'array',
items: {
type: 'string',
format: 'uuid',
},
},
},
},
},
},
};
};

View File

@ -298,3 +298,45 @@ export const getJsonResponse = () => {
},
};
};
export const getFindDuplicatesResponse200 = (
item: Pick<ObjectMetadataEntity, 'nameSingular'>,
) => {
return {
description: 'Successful operation',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
data: {
type: 'array',
items: {
type: 'object',
properties: {
totalCount: { type: 'number' },
pageInfo: {
type: 'object',
properties: {
hasNextPage: { type: 'boolean' },
startCursor: { type: 'string' },
endCursor: { type: 'string' },
},
},
companyDuplicates: {
type: 'array',
items: {
$ref: `#/components/schemas/${capitalize(
item.nameSingular,
)}`,
},
},
},
},
},
},
},
},
},
};
};