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

Clean up transaction calls #4578

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 8 additions & 10 deletions api/src/affiliation/mutations/leave-organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,9 @@ export const leaveOrganization = new mutationWithClientMutationId({
}

try {
await Promise.all([
trx.step(
() =>
query`
await trx.step(
() =>
query`
WITH affiliations, organizations, users
LET userEdges = (
FOR v, e IN 1..1 OUTBOUND ${org._id} affiliations
Expand All @@ -258,16 +257,15 @@ export const leaveOrganization = new mutationWithClientMutationId({
)
RETURN true
`,
),
trx.step(
() =>
query`
)
await trx.step(
() =>
query`
WITH organizations
REMOVE ${org._key} IN organizations
OPTIONS { waitForSync: true }
`,
),
])
)
} catch (err) {
console.error(
`Trx step error occurred while attempting to remove affiliations, and the org for org: ${org._key}, when user: ${user._key} attempted to leave: ${err}`,
Expand Down
9 changes: 5 additions & 4 deletions api/src/affiliation/mutations/remove-user-from-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,17 @@ export const removeUserFromOrg = new mutationWithClientMutationId({
const trx = await transaction(collections)

try {
await trx.step(async () => {
query`
await trx.step(
() =>
query`
WITH affiliations, organizations, users
FOR aff IN affiliations
FILTER aff._from == ${requestedOrg._id}
FILTER aff._to == ${requestedUser._id}
REMOVE aff IN affiliations
RETURN true
`
})
`,
)
} catch (err) {
console.error(
`Trx step error occurred when user: ${userKey} attempted to remove user: ${requestedUser._key} from org: ${requestedOrg._key}, error: ${err}`,
Expand Down
73 changes: 22 additions & 51 deletions api/src/organization/mutations/remove-organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export const removeOrganization = new mutationWithClientMutationId({
outputFields: () => ({
result: {
type: GraphQLNonNull(removeOrganizationUnion),
description:
'`RemoveOrganizationUnion` returning either an `OrganizationResult`, or `OrganizationError` object.',
description: '`RemoveOrganizationUnion` returning either an `OrganizationResult`, or `OrganizationError` object.',
resolve: (payload) => payload,
},
}),
Expand Down Expand Up @@ -48,9 +47,7 @@ export const removeOrganization = new mutationWithClientMutationId({

// Check to see if org exists
if (!organization) {
console.warn(
`User: ${userKey} attempted to remove org: ${orgId}, but there is no org associated with that id.`,
)
console.warn(`User: ${userKey} attempted to remove org: ${orgId}, but there is no org associated with that id.`)
return {
_type: 'error',
code: 400,
Expand Down Expand Up @@ -82,9 +79,7 @@ export const removeOrganization = new mutationWithClientMutationId({
return {
_type: 'error',
code: 403,
description: i18n._(
t`Permission Denied: Please contact super admin for help with removing organization.`,
),
description: i18n._(t`Permission Denied: Please contact super admin for help with removing organization.`),
}
}

Expand All @@ -103,9 +98,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Database error occurred for user: ${userKey} while attempting to get dmarcSummaryInfo while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

let dmarcSummaryCheckList
Expand All @@ -115,9 +108,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Cursor error occurred for user: ${userKey} while attempting to get dmarcSummaryInfo while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

for (const ownership of dmarcSummaryCheckList) {
Expand Down Expand Up @@ -147,9 +138,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Trx step error occurred for user: ${userKey} while attempting to remove dmarc summaries while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

try {
Expand All @@ -164,9 +153,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Trx step error occurred for user: ${userKey} while attempting to remove ownerships while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}
}

Expand Down Expand Up @@ -196,9 +183,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Database error occurred for user: ${userKey} while attempting to gather domain count while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

let domainInfo
Expand All @@ -208,9 +193,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Cursor error occurred for user: ${userKey} while attempting to gather domain count while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

for (const domain of domainInfo) {
Expand Down Expand Up @@ -284,9 +267,7 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Trx step error occurred for user: ${userKey} while attempting to remove domains while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}
}
}
Expand All @@ -310,10 +291,9 @@ export const removeOrganization = new mutationWithClientMutationId({
}

try {
await Promise.all([
trx.step(
() =>
query`
await trx.step(
() =>
query`
WITH affiliations, organizations, users
LET userEdges = (
FOR v, e IN 1..1 OUTBOUND ${organization._id} affiliations
Expand All @@ -326,23 +306,20 @@ export const removeOrganization = new mutationWithClientMutationId({
)
RETURN true
`,
),
trx.step(
() =>
query`
)
await trx.step(
() =>
query`
WITH organizations
REMOVE ${organization._key} IN organizations
OPTIONS { waitForSync: true }
`,
),
])
)
} catch (err) {
console.error(
`Trx step error occurred for user: ${userKey} while attempting to remove affiliations, and the org while removing org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

try {
Expand All @@ -351,14 +328,10 @@ export const removeOrganization = new mutationWithClientMutationId({
console.error(
`Trx commit error occurred for user: ${userKey} while attempting remove of org: ${organization._key}, ${err}`,
)
throw new Error(
i18n._(t`Unable to remove organization. Please try again.`),
)
throw new Error(i18n._(t`Unable to remove organization. Please try again.`))
}

console.info(
`User: ${userKey} successfully removed org: ${organization._key}.`,
)
console.info(`User: ${userKey} successfully removed org: ${organization._key}.`)
await logActivity({
transaction,
collections,
Expand All @@ -380,9 +353,7 @@ export const removeOrganization = new mutationWithClientMutationId({

return {
_type: 'result',
status: i18n._(
t`Successfully removed organization: ${organization.slug}.`,
),
status: i18n._(t`Successfully removed organization: ${organization.slug}.`),
organization,
}
},
Expand Down
14 changes: 6 additions & 8 deletions api/src/user/mutations/close-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,8 @@ export const closeAccount = new mutationWithClientMutationId({

// remove users affiliation
try {
await Promise.all([
trx.step(
() => query`
await trx.step(
() => query`
WITH affiliations, organizations, users
LET userEdges = (
FOR v, e IN 1..1 INBOUND ${affiliation._from} affiliations
Expand All @@ -328,15 +327,14 @@ export const closeAccount = new mutationWithClientMutationId({
)
RETURN true
`,
),
trx.step(
() => query`
)
await trx.step(
() => query`
WITH organizations
REMOVE PARSE_IDENTIFIER(${affiliation._from}).key
IN organizations OPTIONS { waitForSync: true }
`,
),
])
)
} catch (err) {
console.error(
`Trx step error occurred when removing ownership org and users affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
Expand Down