fix: Update auth app_metadata when deleting users + customers (#9041)
* wip * more work * working on stuff * more * fix test * remove incorrect test * fix test * fix: Only allow deletion of yourself * remove redundant tests
This commit is contained in:
@@ -6,7 +6,7 @@ import { isDefined, ModuleRegistrationName } from "@medusajs/utils"
|
||||
export type SetAuthAppMetadataStepInput = {
|
||||
authIdentityId: string
|
||||
actorType: string
|
||||
value: string
|
||||
value: string | null // null means delete the key
|
||||
}
|
||||
|
||||
export const setAuthAppMetadataStepId = "set-auth-app-metadata"
|
||||
@@ -24,10 +24,13 @@ export const setAuthAppMetadataStep = createStep(
|
||||
const authIdentity = await service.retrieveAuthIdentity(data.authIdentityId)
|
||||
|
||||
const appMetadata = authIdentity.app_metadata || {}
|
||||
if (isDefined(appMetadata[key])) {
|
||||
|
||||
// If the value is null, we are deleting the association with an actor
|
||||
if (isDefined(appMetadata[key]) && data.value !== null) {
|
||||
throw new Error(`Key ${key} already exists in app metadata`)
|
||||
}
|
||||
|
||||
const oldValue = appMetadata[key]
|
||||
appMetadata[key] = data.value
|
||||
|
||||
await service.updateAuthIdentities({
|
||||
@@ -38,14 +41,16 @@ export const setAuthAppMetadataStep = createStep(
|
||||
return new StepResponse(authIdentity, {
|
||||
id: authIdentity.id,
|
||||
key: key,
|
||||
value: data.value,
|
||||
oldValue,
|
||||
})
|
||||
},
|
||||
async (idAndKey, { container }) => {
|
||||
if (!idAndKey) {
|
||||
async (idAndKeyAndValue, { container }) => {
|
||||
if (!idAndKeyAndValue) {
|
||||
return
|
||||
}
|
||||
|
||||
const { id, key } = idAndKey
|
||||
const { id, key, oldValue, value } = idAndKeyAndValue
|
||||
|
||||
const service = container.resolve<IAuthModuleService>(
|
||||
ModuleRegistrationName.AUTH
|
||||
@@ -54,7 +59,11 @@ export const setAuthAppMetadataStep = createStep(
|
||||
const authIdentity = await service.retrieveAuthIdentity(id)
|
||||
|
||||
const appMetadata = authIdentity.app_metadata || {}
|
||||
if (isDefined(appMetadata[key])) {
|
||||
|
||||
// If the value is null, we WERE deleting the association with an actor, so we need to restore it
|
||||
if (value === null) {
|
||||
appMetadata[key] = oldValue
|
||||
} else {
|
||||
delete appMetadata[key]
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export * from "./create-customers"
|
||||
export * from "./update-customers"
|
||||
export * from "./delete-customers"
|
||||
export * from "./create-customer-account"
|
||||
export * from "./create-addresses"
|
||||
export * from "./update-addresses"
|
||||
export * from "./create-customer-account"
|
||||
export * from "./create-customers"
|
||||
export * from "./delete-addresses"
|
||||
export * from "./delete-customers"
|
||||
export * from "./remove-customer-account"
|
||||
export * from "./update-addresses"
|
||||
export * from "./update-customers"
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
transform,
|
||||
when,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { deleteCustomersWorkflow } from "./delete-customers"
|
||||
|
||||
export type RemoveCustomerAccountWorkflowInput = {
|
||||
customerId: string
|
||||
}
|
||||
export const removeCustomerAccountWorkflowId = "remove-customer-account"
|
||||
/**
|
||||
* This workflow deletes a user and remove the association in the auth identity.
|
||||
*/
|
||||
export const removeCustomerAccountWorkflow = createWorkflow(
|
||||
removeCustomerAccountWorkflowId,
|
||||
(
|
||||
input: WorkflowData<RemoveCustomerAccountWorkflowInput>
|
||||
): WorkflowResponse<string> => {
|
||||
const customers = useRemoteQueryStep({
|
||||
entry_point: "customer",
|
||||
fields: ["id", "has_account"],
|
||||
variables: {
|
||||
id: input.customerId,
|
||||
},
|
||||
}).config({ name: "get-customer" })
|
||||
|
||||
deleteCustomersWorkflow.runAsStep({
|
||||
input: {
|
||||
ids: [input.customerId],
|
||||
},
|
||||
})
|
||||
|
||||
when({ customers }, ({ customers }) => {
|
||||
return !!customers[0]?.has_account
|
||||
}).then(() => {
|
||||
const authIdentities = useRemoteQueryStep({
|
||||
entry_point: "auth_identity",
|
||||
fields: ["id"],
|
||||
variables: {
|
||||
filters: {
|
||||
app_metadata: {
|
||||
customer_id: input.customerId,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const authIdentity = transform(
|
||||
{ authIdentities },
|
||||
({ authIdentities }) => {
|
||||
const authIdentity = authIdentities[0]
|
||||
|
||||
if (!authIdentity) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
"Auth identity not found"
|
||||
)
|
||||
}
|
||||
|
||||
return authIdentity
|
||||
}
|
||||
)
|
||||
|
||||
setAuthAppMetadataStep({
|
||||
authIdentityId: authIdentity.id,
|
||||
actorType: "customer",
|
||||
value: null,
|
||||
})
|
||||
})
|
||||
|
||||
return new WorkflowResponse(input.customerId)
|
||||
}
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from "./create-user-account"
|
||||
export * from "./create-users"
|
||||
export * from "./delete-users"
|
||||
export * from "./remove-user-account"
|
||||
export * from "./update-users"
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createWorkflow,
|
||||
transform,
|
||||
when,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { deleteUsersWorkflow } from "./delete-users"
|
||||
|
||||
export type RemoveUserAccountWorkflowInput = {
|
||||
userId: string
|
||||
}
|
||||
export const removeUserAccountWorkflowId = "remove-user-account"
|
||||
/**
|
||||
* This workflow deletes a user and remove the association in the auth identity.
|
||||
*/
|
||||
export const removeUserAccountWorkflow = createWorkflow(
|
||||
removeUserAccountWorkflowId,
|
||||
(
|
||||
input: WorkflowData<RemoveUserAccountWorkflowInput>
|
||||
): WorkflowResponse<string> => {
|
||||
deleteUsersWorkflow.runAsStep({
|
||||
input: {
|
||||
ids: [input.userId],
|
||||
},
|
||||
})
|
||||
|
||||
const authIdentities = useRemoteQueryStep({
|
||||
entry_point: "auth_identity",
|
||||
fields: ["id"],
|
||||
variables: {
|
||||
filters: {
|
||||
app_metadata: {
|
||||
user_id: input.userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const authIdentity = transform(
|
||||
{ authIdentities, input },
|
||||
({ authIdentities }) => {
|
||||
return authIdentities[0]
|
||||
}
|
||||
)
|
||||
|
||||
when({ authIdentity }, ({ authIdentity }) => {
|
||||
return !!authIdentity
|
||||
}).then(() => {
|
||||
setAuthAppMetadataStep({
|
||||
authIdentityId: authIdentity.id,
|
||||
actorType: "user",
|
||||
value: null,
|
||||
})
|
||||
})
|
||||
|
||||
return new WorkflowResponse(input.userId)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user