Skip to content

Commit

Permalink
Bugfix: Abort transactions on error in api (#5842)
Browse files Browse the repository at this point in the history
* add trx.abort() when api returns or throws error

* add abort mocks to tests

* add trx.abort() to dmarc-report service

* add trx.abort() to super-admin service

* fix super-admin tests
  • Loading branch information
lcampbell2 authored Oct 29, 2024
1 parent a2a4f0e commit 641ff2f
Show file tree
Hide file tree
Showing 73 changed files with 382 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2615,6 +2615,7 @@ describe('invite user to org', () => {
collections: collectionNames,
transaction: jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue('trx step err'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down Expand Up @@ -2691,6 +2692,7 @@ describe('invite user to org', () => {
transaction: jest.fn().mockReturnValue({
step: jest.fn(),
commit: jest.fn().mockRejectedValue('trx commit err'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down Expand Up @@ -3182,6 +3184,7 @@ describe('invite user to org', () => {
collections: collectionNames,
transaction: jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue('trx step err'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down Expand Up @@ -3258,6 +3261,7 @@ describe('invite user to org', () => {
transaction: jest.fn().mockReturnValue({
step: jest.fn(),
commit: jest.fn().mockRejectedValue('trx commit err'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ describe('given an unsuccessful leave', () => {

const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue(new Error('Step error occurred.')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -706,6 +707,7 @@ describe('given an unsuccessful leave', () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValue(new Error('Step error occurred.')),
commit: jest.fn().mockRejectedValue(new Error('Trx Commit Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -849,6 +851,7 @@ describe('given an unsuccessful leave', () => {

const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue(new Error('Step error occurred.')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -917,6 +920,7 @@ describe('given an unsuccessful leave', () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValue(new Error('Step error occurred.')),
commit: jest.fn().mockRejectedValue(new Error('Trx Commit Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,7 @@ describe('given the removeUserFromOrg mutation', () => {

const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue(new Error('trx step error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -1716,6 +1717,7 @@ describe('given the removeUserFromOrg mutation', () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValue(),
commit: jest.fn().mockRejectedValue(new Error('trx commit error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -2354,6 +2356,7 @@ describe('given the removeUserFromOrg mutation', () => {

const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue(new Error('trx step error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -2433,6 +2436,7 @@ describe('given the removeUserFromOrg mutation', () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValue(),
commit: jest.fn().mockRejectedValue(new Error('trx commit error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ describe('given the transferOrgOwnership mutation', () => {
it('throws an error', async () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue(new Error('Step Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -880,6 +881,7 @@ describe('given the transferOrgOwnership mutation', () => {
it('throws an error', async () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValueOnce().mockRejectedValue(new Error('Step Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -949,6 +951,7 @@ describe('given the transferOrgOwnership mutation', () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValue(),
commit: jest.fn().mockRejectedValue(new Error('Commit Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -1387,6 +1390,7 @@ describe('given the transferOrgOwnership mutation', () => {
it('throws an error', async () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue(new Error('Step Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -1456,6 +1460,7 @@ describe('given the transferOrgOwnership mutation', () => {
it('throws an error', async () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValueOnce().mockRejectedValue(new Error('Step Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down Expand Up @@ -1527,6 +1532,7 @@ describe('given the transferOrgOwnership mutation', () => {
const mockedTransaction = jest.fn().mockReturnValue({
step: jest.fn().mockReturnValue(),
commit: jest.fn().mockRejectedValue(new Error('Commit Error')),
abort: jest.fn(),
})

const response = await graphql({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ describe('update a users role', () => {
collections: collectionNames,
transaction: jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue('trx step error'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down Expand Up @@ -1809,6 +1810,7 @@ describe('update a users role', () => {
transaction: jest.fn().mockReturnValue({
step: jest.fn(),
commit: jest.fn().mockRejectedValue('trx commit error'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down Expand Up @@ -2566,6 +2568,7 @@ describe('update a users role', () => {
collections: collectionNames,
transaction: jest.fn().mockReturnValue({
step: jest.fn().mockRejectedValue('trx step error'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down Expand Up @@ -2640,6 +2643,7 @@ describe('update a users role', () => {
transaction: jest.fn().mockReturnValue({
step: jest.fn(),
commit: jest.fn().mockRejectedValue('trx commit error'),
abort: jest.fn(),
}),
userKey: 123,
auth: {
Expand Down
2 changes: 2 additions & 0 deletions api/src/affiliation/mutations/invite-user-to-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ able to sign-up and be assigned to that organization in one mutation.`,
console.error(
`Transaction step error occurred while user: ${userKey} attempted to invite user: ${requestedUser._key} to org: ${org.slug}, error: ${err}`,
)
await trx.abort()
return {
_type: 'error',
code: 500,
Expand All @@ -249,6 +250,7 @@ able to sign-up and be assigned to that organization in one mutation.`,
console.error(
`Transaction commit error occurred while user: ${userKey} attempted to invite user: ${requestedUser._key} to org: ${org.slug}, error: ${err}`,
)
await trx.abort()
return {
_type: 'error',
code: 500,
Expand Down
2 changes: 2 additions & 0 deletions api/src/affiliation/mutations/leave-organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ export const leaveOrganization = new mutationWithClientMutationId({
console.error(
`Trx step error occurred when removing user: ${user._key} affiliation with org: ${org._key}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}

try {
await trx.commit()
} catch (err) {
console.error(`Trx commit error occurred when user: ${user._key} attempted to leave org: ${org._key}: ${err}`)
await trx.abort()
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}

Expand Down
2 changes: 2 additions & 0 deletions api/src/affiliation/mutations/remove-user-from-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const removeUserFromOrg = new mutationWithClientMutationId({
console.error(
`Trx step error occurred when user: ${userKey} attempted to remove user: ${requestedUser._key} from org: ${requestedOrg._key}, error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to remove user from this organization. Please try again.`))
}

Expand All @@ -167,6 +168,7 @@ export const removeUserFromOrg = new mutationWithClientMutationId({
console.error(
`Trx commit error occurred when user: ${userKey} attempted to remove user: ${requestedUser._key} from org: ${requestedOrg._key}, error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to remove user from this organization. Please try again.`))
}

Expand Down
6 changes: 6 additions & 0 deletions api/src/affiliation/mutations/request-org-affiliation.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const requestOrgAffiliation = new mutationWithClientMutationId({
console.error(
`Transaction step error occurred while user: ${userKey} attempted to request invite to org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to request invite. Please try again.`))
}

Expand All @@ -135,6 +136,7 @@ export const requestOrgAffiliation = new mutationWithClientMutationId({
console.error(
`Database error occurred when user: ${userKey} attempted to request invite to ${orgId}, error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to request invite. Please try again.`))
}

Expand All @@ -145,6 +147,7 @@ export const requestOrgAffiliation = new mutationWithClientMutationId({
console.error(
`Cursor error occurred when user: ${userKey} attempted to request invite to ${orgId}, error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to request invite. Please try again.`))
}

Expand All @@ -165,6 +168,7 @@ export const requestOrgAffiliation = new mutationWithClientMutationId({
console.error(
`Database error occurred when user: ${userKey} attempted to request invite to org: ${org._key}. Error while creating cursor for retrieving organization names. error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to request invite. Please try again.`))
}
let orgNames
Expand All @@ -174,6 +178,7 @@ export const requestOrgAffiliation = new mutationWithClientMutationId({
console.error(
`Cursor error occurred when user: ${userKey} attempted to request invite to org: ${org._key}. Error while retrieving organization names. error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to request invite. Please try again.`))
}
const adminLink = `https://${request.get('host')}/admin/organizations`
Expand Down Expand Up @@ -204,6 +209,7 @@ export const requestOrgAffiliation = new mutationWithClientMutationId({
console.error(
`Transaction commit error occurred while user: ${userKey} attempted to request invite to org: ${org.slug}, error: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to request invite. Please try again.`))
}

Expand Down
3 changes: 3 additions & 0 deletions api/src/affiliation/mutations/transfer-org-ownership.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export const transferOrgOwnership = new mutationWithClientMutationId({
console.error(
`Trx step error occurred for user: ${requestingUser._key} when they were attempting to transfer org: ${org.slug} ownership to user: ${requestedUser._key}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to transfer organization ownership. Please try again.`))
}

Expand All @@ -164,6 +165,7 @@ export const transferOrgOwnership = new mutationWithClientMutationId({
console.error(
`Trx step error occurred for user: ${requestingUser._key} when they were attempting to transfer org: ${org.slug} ownership to user: ${requestedUser._key}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to transfer organization ownership. Please try again.`))
}

Expand All @@ -174,6 +176,7 @@ export const transferOrgOwnership = new mutationWithClientMutationId({
console.error(
`Trx commit error occurred for user: ${requestingUser._key} when they were attempting to transfer org: ${org.slug} ownership to user: ${requestedUser._key}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to transfer organization ownership. Please try again.`))
}

Expand Down
4 changes: 2 additions & 2 deletions api/src/affiliation/mutations/update-user-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ given organization.`,
console.error(
`Transaction step error occurred when user: ${userKey} attempted to update a user's: ${requestedUser._key} role, error: ${err}`,
)

await trx.abort()
throw new Error(i18n._(t`Unable to update user's role. Please try again.`))
}

Expand All @@ -207,7 +207,7 @@ given organization.`,
console.warn(
`Transaction commit error occurred when user: ${userKey} attempted to update a user's: ${requestedUser._key} role, error: ${err}`,
)

await trx.abort()
throw new Error(i18n._(t`Unable to update user's role. Please try again.`))
}

Expand Down
10 changes: 4 additions & 6 deletions api/src/audit-logs/mutations/log-activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ export const logActivity = async ({
`,
)
} catch (err) {
console.error(
`Transaction step error occurred while attempting to log user action: ${err}`,
)
console.error(`Transaction step error occurred while attempting to log user action: ${err}`)
await trx.abort()
}

try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred while attempting to log user action: ${err}`,
)
console.error(`Transaction commit error occurred while attempting to log user action: ${err}`)
await trx.abort()
}

return auditLog
Expand Down
Loading

0 comments on commit 641ff2f

Please sign in to comment.