Add deleteIntegration API and integration tests

This commit is contained in:
Hongbo Wu 2022-08-08 18:34:13 +08:00
parent 4aa62d6031
commit 7ac45ca748
3 changed files with 119 additions and 0 deletions

View file

@ -32,6 +32,7 @@ import {
createReminderResolver,
deleteAccountResolver,
deleteHighlightResolver,
deleteIntegrationResolver,
deleteLabelResolver,
deleteNewsletterEmailResolver,
deleteReminderResolver,
@ -168,6 +169,7 @@ export const functionResolvers = {
setLabelsForHighlight: setLabelsForHighlightResolver,
moveLabel: moveLabelResolver,
setIntegration: setIntegrationResolver,
deleteIntegration: deleteIntegrationResolver,
},
Query: {
me: getMeUserResolver,
@ -598,4 +600,5 @@ export const functionResolvers = {
...resultResolveTypeResolver('MoveLabel'),
...resultResolveTypeResolver('SetIntegration'),
...resultResolveTypeResolver('Integrations'),
...resultResolveTypeResolver('DeleteIntegration'),
}

View file

@ -1,8 +1,12 @@
import { authorized } from '../../utils/helpers'
import {
DeleteIntegrationError,
DeleteIntegrationErrorCode,
DeleteIntegrationSuccess,
IntegrationsError,
IntegrationsErrorCode,
IntegrationsSuccess,
MutationDeleteIntegrationArgs,
MutationSetIntegrationArgs,
SetIntegrationError,
SetIntegrationErrorCode,
@ -127,6 +131,8 @@ export const integrationsResolver = authorized<
IntegrationsSuccess,
IntegrationsError
>(async (_, __, { claims: { uid }, log }) => {
log.info('integrationsResolver')
try {
const user = await getRepository(User).findOneBy({ id: uid })
if (!user) {
@ -149,3 +155,67 @@ export const integrationsResolver = authorized<
}
}
})
export const deleteIntegrationResolver = authorized<
DeleteIntegrationSuccess,
DeleteIntegrationError,
MutationDeleteIntegrationArgs
>(async (_, { id }, { claims: { uid }, log }) => {
log.info('deleteIntegrationResolver')
try {
const user = await getRepository(User).findOneBy({ id: uid })
if (!user) {
return {
errorCodes: [DeleteIntegrationErrorCode.Unauthorized],
}
}
const integration = await getRepository(Integration).findOne({
where: { id },
relations: ['user'],
})
if (!integration) {
return {
errorCodes: [DeleteIntegrationErrorCode.NotFound],
}
}
if (integration.user.id !== uid) {
return {
errorCodes: [DeleteIntegrationErrorCode.Unauthorized],
}
}
if (integration.taskName) {
// delete the task if task exists
await deleteTask(integration.taskName)
log.info('task deleted', integration.taskName)
}
const deletedIntegration = await getRepository(Integration).remove(
integration
)
deletedIntegration.id = id
analytics.track({
userId: uid,
event: 'integration_delete',
properties: {
integrationId: deletedIntegration.id,
env: env.server.apiEnv,
},
})
return {
integration: deletedIntegration,
}
} catch (error) {
log.error(error)
return {
errorCodes: [DeleteIntegrationErrorCode.BadRequest],
}
}
})

View file

@ -345,4 +345,50 @@ describe('Integrations resolvers', () => {
)
})
})
describe('deleteIntegration API', () => {
const query = (id: string) => `
mutation {
deleteIntegration(id: "${id}") {
... on DeleteIntegrationSuccess {
integration {
id
}
}
... on DeleteIntegrationError {
errorCodes
}
}
}
`
context('when integration exists', () => {
let existingIntegration: Integration
beforeEach(async () => {
existingIntegration = await getRepository(Integration).save({
user: loginUser,
type: DataIntegrationType.Readwise,
token: 'fakeToken',
taskName: 'some task name',
})
})
it('deletes the integration and cloud task', async () => {
const res = await graphqlRequest(
query(existingIntegration.id),
authToken
)
const integration = await getRepository(Integration).findOneBy({
id: existingIntegration.id,
})
expect(res.body.data.deleteIntegration.integration).to.be.an('object')
expect(res.body.data.deleteIntegration.integration.id).to.eql(
existingIntegration.id
)
expect(integration).to.be.null
})
})
})
})