/
create.ts
95 lines (80 loc) · 2.61 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Flags } from '@oclif/core'
import inquirer from 'inquirer'
import { inputAndOutputItem, TableFieldDefinition, userInputProcessor } from '@smartthings/cli-lib'
import { chooseChannel } from '../../../../lib/commands/channels-util'
import { EdgeCommand } from '../../../../lib/edge-command'
import { CreateInvitation, Invitation } from '../../../../lib/endpoints/invites'
const tableFieldDefinitions: TableFieldDefinition<Invitation>[] = [
'id',
{ path: 'metadata.name' },
'profileId', 'expiration', 'acceptUrl']
const defaultInvitationProfileId = '61a79569-e8fd-4a4d-9b9c-a4a55ccdd15e'
export default class ChannelsInvitesCreateCommand extends EdgeCommand<typeof ChannelsInvitesCreateCommand.flags> {
static description = 'create an invitation'
static flags = {
channel: Flags.string({
char: 'C',
description: 'channel id',
exclusive: ['input'],
helpValue: '<UUID>',
}),
...EdgeCommand.flags,
...inputAndOutputItem.flags,
}
async run(): Promise<void> {
const create = async (_: void, input: CreateInvitation): Promise<Invitation> => {
const { invitationId } = await this.edgeClient.invites.create(input)
const invitation = await this.edgeClient.invites.get(invitationId)
return invitation
}
await inputAndOutputItem(this, { tableFieldDefinitions }, create, userInputProcessor(this))
}
async getInputFromUser(): Promise<CreateInvitation> {
const channelId = await chooseChannel(this, 'Choose a channel:', this.flags.channel,
{ useConfigDefault: true })
const name = (await inquirer.prompt({
type: 'input',
name: 'name',
message: 'Invitation name:',
validate: input => input ? true : 'name is required',
})).name as string
const description = (await inquirer.prompt({
type: 'input',
name: 'description',
message: 'Invitation description:',
validate: input => input ? true : 'description is required',
})).description as string
const owner = (await inquirer.prompt({
type: 'input',
name: 'owner',
message: 'Invitation owner:',
validate: input => input ? true : 'owner is required',
})).owner as string
const termsUrl = (await inquirer.prompt({
type: 'input',
name: 'termsUrl',
message: 'Invitation termsUrl:',
validate: input => input ? true : 'termsUrl is required',
})).termsUrl as string
const profileId = (this.profile.defaultInvitationProfileId as string)
?? defaultInvitationProfileId
return {
resource: {
root: {
service: 'developer',
},
components: [{
id: channelId,
kind: 'channel',
}],
},
profileId,
metadata: {
name,
description,
owner,
termsUrl,
},
}
}
}