Skip to content

Commit

Permalink
Add user identity delete example
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x authored Feb 9, 2024
1 parent f8ca650 commit 0a561f5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
113 changes: 113 additions & 0 deletions examples/delete-user-identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import type { AcsCredentialsListResponse } from '@seamapi/http/connect'
import { type Builder, type Command, type Describe } from 'landlubber'

import type { Handler } from './index.js'

interface Options {
userIdentityId: string
}

export const command: Command = 'delete-user-identity userIdentityId'

export const describe: Describe = 'Delete a user identity'

export const builder: Builder = {
userIdentityId: {
type: 'string',
describe: 'User identity id to delete',
},
}

export const handler: Handler<Options> = async ({
userIdentityId,
seam,
logger,
}) => {
const waitForEvent =
(eventType: string, idKey: string) =>
async (resource: any): Promise<void> => {
// Application architecture specific logic that waits for an event matching
// event.event_type === eventType && event[idKey] === resource[idKey]
logger.info({ eventType, idKey, resource }, 'Got event')
}

const clientSessions = await seam.clientSessions.list({
// param not implemented
user_identity_id: userIdentityId,
})

for (const clientSession of clientSessions) {
await seam.clientSessions.delete({
client_session_id: clientSession.client_session_id,
})
}

const enrollmentAutomations =
await seam.userIdentities.enrollmentAutomations.list({
user_identity_id: userIdentityId,
})

for (const enrollmentAutomation of enrollmentAutomations) {
// endpoint not implemented
await seam.userIdentities.enrollmentAutomations.delete({
enrollment_automation_id: enrollmentAutomation.enrollment_automation_id,
})
}

await Promise.all(
enrollmentAutomations.map(
// event not implemented
waitForEvent('enrollment_automation.deleted', 'enrollment_automation_id'),
),
)

const phones = await seam.phones.list({
owner_user_identity_id: userIdentityId,
})

for (const phone of phones) {
await seam.phones.deactivate({
device_id: phone.device_id,
})
}

// event not implemented
await Promise.all(phones.map(waitForEvent('phone.deactivated', 'device_id')))

for (const phone of phones) {
await seam.devices.delete({
device_id: phone.device_id,
})
}

const acsUsers = await seam.acs.users.list({
user_identity_id: userIdentityId,
})

const deletedCredentials: AcsCredentialsListResponse['acs_credentials'] = []

for (const acsUser of acsUsers) {
const credentials = await seam.acs.credentials.list({
acs_user_id: acsUser.acs_user_id,
})

for (const credential of credentials) {
await seam.acs.credentials.delete({
acs_credential_id: credential.acs_credential_id,
})
}

deletedCredentials.concat(credentials)
}

// event not implemented
await Promise.all(
deletedCredentials.map(
waitForEvent('acs_credential.deleted', 'acs_credential_id'),
),
)

await seam.userIdentities.delete({
user_identity_id: userIdentityId,
})
}
3 changes: 2 additions & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SeamHttp } from '@seamapi/http/connect'
import * as locks from './locks.js'
import * as unlock from './unlock.js'
import * as workspace from './workspace.js'
import * as deleteUserIdentity from './delete-user-identity.js'

export type Handler<Options = EmptyOptions> = DefaultHandler<Options, Context>

Expand All @@ -24,7 +25,7 @@ interface ClientContext {
seam: SeamHttp
}

const commands = [locks, unlock, workspace]
const commands = [deleteUserIdentity, locks, unlock, workspace]

const createAppContext: MiddlewareFunction = async (argv) => {
const apiKey = argv['api-key']
Expand Down

0 comments on commit 0a561f5

Please sign in to comment.