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

Add delete user identity example #52

Closed
wants to merge 17 commits into from
Closed
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
175 changes: 175 additions & 0 deletions examples/delete-user-identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import type { Builder, Command, Describe } from 'landlubber'

import type {
AcsCredentialsGetResponse,
AcsUsersGetResponse,
EventsListParams,
EventsListResponse,
PhonesListResponse,
UserIdentitiesEnrollmentAutomationsGetResponse,
} from '@seamapi/http/connect'

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 = async (
eventType: SeamEventType,
eventFilter: (event: SeamEvent) => boolean,
): Promise<void> => {
let events: SeamEvent[] = []
do {
events = await seam.events.list({ event_type: eventType })
} while (!events.some(eventFilter))
}

const waitForAcsUserDeleted = async (acsUser: AcsUser): Promise<void> => {
await waitForEvent(
'acs_user.deleted',
(event) =>
'acs_user_id' in event && event.acs_user_id === acsUser.acs_user_id,
)
}

const waitForAcsCredentialDeleted = async (
acsCredential: AcsCredential,
): Promise<void> => {
await waitForEvent(
'acs_credential.deleted',
(event) =>
'acs_credential_id' in event &&
event.acs_credential_id === acsCredential.acs_credential_id,
)
}

const waitForPhoneDeactivated = async (phone: Phone): Promise<void> => {
await waitForEvent(
'phone.deactivated',
(event) => 'device_id' in event && event.device_id === phone.device_id,
)
}

const waitForEnrollmentAutomationDeleted = async (
enrollmentAutomation: EnrollmentAutomation,
): Promise<void> => {
await waitForEvent(
'enrollment_automation.deleted',
(event) =>
'enrollment_automation_id' in event &&
event.enrollment_automation_id ===
enrollmentAutomation.enrollment_automation_id,
)
}

const userIdentity = await seam.userIdentities.get({
user_identity_id: userIdentityId,
})

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

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

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

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

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

await Promise.all(credentials.map(waitForAcsCredentialDeleted))

await seam.acs.users.delete({ acs_user_id: acsUser.acs_user_id })
}

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

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

await Promise.all(
enrollmentAutomations.map(waitForEnrollmentAutomationDeleted),
)

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,
})
}

await Promise.all(phones.map(waitForPhoneDeactivated))

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,
})
}

await Promise.all(credentials.map(waitForAcsCredentialDeleted))

await seam.acs.users.delete({ acs_user_id: acsUser.acs_user_id })
}

await Promise.all(acsUsers.map(waitForAcsUserDeleted))

await seam.userIdentities.delete({
user_identity_id: userIdentityId,
})

logger.info({ userIdentity }, 'Deleted user identity')
}

type AcsUser = AcsUsersGetResponse['acs_user']
type AcsCredential = AcsCredentialsGetResponse['acs_credential']
type EnrollmentAutomation =
UserIdentitiesEnrollmentAutomationsGetResponse['enrollment_automation']
type Phone = PhonesListResponse['phones'][number]
type SeamEvent = EventsListResponse['events'][number]
type SeamEventType = EventsListParams['event_type']
3 changes: 2 additions & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import landlubber, {

import { SeamHttp } from '@seamapi/http/connect'

import * as deleteUserIdentity from './delete-user-identity.js'
import * as locks from './locks.js'
import * as unlock from './unlock.js'
import * as workspace from './workspace.js'
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
44 changes: 29 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"npm": ">= 9.0.0"
},
"peerDependencies": {
"@seamapi/types": "^1.121.0",
"@seamapi/types": "^^1.123.1",
"type-fest": "^4.0.0"
},
"peerDependenciesMeta": {
Expand All @@ -103,7 +103,7 @@
},
"devDependencies": {
"@seamapi/fake-seam-connect": "^1.44.2",
"@seamapi/types": "1.121.0",
"@seamapi/types": "^1.123.1",
"@types/eslint": "^8.44.2",
"@types/node": "^20.8.10",
"ava": "^5.0.1",
Expand Down
Loading