Add import from integration api implementation

This commit is contained in:
Hongbo Wu
2023-02-16 12:41:39 +08:00
parent e2d050288e
commit 02247bc0a6
5 changed files with 53 additions and 4 deletions

View File

@ -954,7 +954,7 @@ export type ImportFromIntegrationResult = ImportFromIntegrationError | ImportFro
export type ImportFromIntegrationSuccess = {
__typename?: 'ImportFromIntegrationSuccess';
success: Scalars['Boolean'];
count: Scalars['Int'];
};
export type Integration = {
@ -4807,7 +4807,7 @@ export type ImportFromIntegrationResultResolvers<ContextType = ResolverContext,
};
export type ImportFromIntegrationSuccessResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['ImportFromIntegrationSuccess'] = ResolversParentTypes['ImportFromIntegrationSuccess']> = {
success?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
count?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

View File

@ -847,7 +847,7 @@ enum ImportFromIntegrationErrorCode {
union ImportFromIntegrationResult = ImportFromIntegrationError | ImportFromIntegrationSuccess
type ImportFromIntegrationSuccess {
success: Boolean!
count: Int!
}
type Integration {

View File

@ -62,6 +62,7 @@ import {
googleLoginResolver,
googleSignupResolver,
groupsResolver,
importFromIntegrationResolver,
integrationsResolver,
joinGroupResolver,
labelsResolver,
@ -202,6 +203,7 @@ export const functionResolvers = {
uploadImportFile: uploadImportFileResolver,
markEmailAsItem: markEmailAsItemResolver,
bulkAction: bulkActionResolver,
importFromIntegration: importFromIntegrationResolver,
},
Query: {
me: getMeUserResolver,
@ -650,4 +652,5 @@ export const functionResolvers = {
...resultResolveTypeResolver('RecentEmails'),
...resultResolveTypeResolver('MarkEmailAsItem'),
...resultResolveTypeResolver('BulkAction'),
...resultResolveTypeResolver('ImportFromIntegration'),
}

View File

@ -3,10 +3,14 @@ import {
DeleteIntegrationError,
DeleteIntegrationErrorCode,
DeleteIntegrationSuccess,
ImportFromIntegrationError,
ImportFromIntegrationErrorCode,
ImportFromIntegrationSuccess,
IntegrationsError,
IntegrationsErrorCode,
IntegrationsSuccess,
MutationDeleteIntegrationArgs,
MutationImportFromIntegrationArgs,
MutationSetIntegrationArgs,
SetIntegrationError,
SetIntegrationErrorCode,
@ -222,3 +226,45 @@ export const deleteIntegrationResolver = authorized<
}
}
})
export const importFromIntegrationResolver = authorized<
ImportFromIntegrationSuccess,
ImportFromIntegrationError,
MutationImportFromIntegrationArgs
>(async (_, { integrationId }, { claims: { uid }, log }) => {
log.info('importFromIntegrationResolver')
try {
const integration = await getRepository(Integration).findOne({
where: { id: integrationId, user: { id: uid } },
relations: ['user'],
})
if (!integration) {
return {
errorCodes: [ImportFromIntegrationErrorCode.Unauthorized],
}
}
const integrationService = getIntegrationService(integration.name)
const count = await integrationService.import(integration)
analytics.track({
userId: uid,
event: 'integration_import',
properties: {
integrationId,
},
})
return {
count,
}
} catch (error) {
log.error(error)
return {
errorCodes: [ImportFromIntegrationErrorCode.BadRequest],
}
}
})

View File

@ -2424,7 +2424,7 @@ const schema = gql`
| ImportFromIntegrationError
type ImportFromIntegrationSuccess {
success: Boolean!
count: Int!
}
type ImportFromIntegrationError {