diff options
Diffstat (limited to 'shared/server-commands/users/registrations-command.ts')
-rw-r--r-- | shared/server-commands/users/registrations-command.ts | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/shared/server-commands/users/registrations-command.ts b/shared/server-commands/users/registrations-command.ts new file mode 100644 index 000000000..f57f54b34 --- /dev/null +++ b/shared/server-commands/users/registrations-command.ts | |||
@@ -0,0 +1,151 @@ | |||
1 | import { pick } from '@shared/core-utils' | ||
2 | import { HttpStatusCode, ResultList, UserRegistration, UserRegistrationRequest, UserRegistrationUpdateState } from '@shared/models' | ||
3 | import { unwrapBody } from '../requests' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | export class RegistrationsCommand extends AbstractCommand { | ||
7 | |||
8 | register (options: OverrideCommandOptions & Partial<UserRegistrationRequest> & Pick<UserRegistrationRequest, 'username'>) { | ||
9 | const { password = 'password', email = options.username + '@example.com' } = options | ||
10 | const path = '/api/v1/users/register' | ||
11 | |||
12 | return this.postBodyRequest({ | ||
13 | ...options, | ||
14 | |||
15 | path, | ||
16 | fields: { | ||
17 | ...pick(options, [ 'username', 'displayName', 'channel' ]), | ||
18 | |||
19 | password, | ||
20 | |||
21 | }, | ||
22 | implicitToken: false, | ||
23 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
24 | }) | ||
25 | } | ||
26 | |||
27 | requestRegistration ( | ||
28 | options: OverrideCommandOptions & Partial<UserRegistrationRequest> & Pick<UserRegistrationRequest, 'username' | 'registrationReason'> | ||
29 | ) { | ||
30 | const { password = 'password', email = options.username + '@example.com' } = options | ||
31 | const path = '/api/v1/users/registrations/request' | ||
32 | |||
33 | return unwrapBody<UserRegistration>(this.postBodyRequest({ | ||
34 | ...options, | ||
35 | |||
36 | path, | ||
37 | fields: { | ||
38 | ...pick(options, [ 'username', 'displayName', 'channel', 'registrationReason' ]), | ||
39 | |||
40 | password, | ||
41 | |||
42 | }, | ||
43 | implicitToken: false, | ||
44 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
45 | })) | ||
46 | } | ||
47 | |||
48 | // --------------------------------------------------------------------------- | ||
49 | |||
50 | accept (options: OverrideCommandOptions & { id: number } & UserRegistrationUpdateState) { | ||
51 | const { id } = options | ||
52 | const path = '/api/v1/users/registrations/' + id + '/accept' | ||
53 | |||
54 | return this.postBodyRequest({ | ||
55 | ...options, | ||
56 | |||
57 | path, | ||
58 | fields: pick(options, [ 'moderationResponse', 'preventEmailDelivery' ]), | ||
59 | implicitToken: true, | ||
60 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | reject (options: OverrideCommandOptions & { id: number } & UserRegistrationUpdateState) { | ||
65 | const { id } = options | ||
66 | const path = '/api/v1/users/registrations/' + id + '/reject' | ||
67 | |||
68 | return this.postBodyRequest({ | ||
69 | ...options, | ||
70 | |||
71 | path, | ||
72 | fields: pick(options, [ 'moderationResponse', 'preventEmailDelivery' ]), | ||
73 | implicitToken: true, | ||
74 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
75 | }) | ||
76 | } | ||
77 | |||
78 | // --------------------------------------------------------------------------- | ||
79 | |||
80 | delete (options: OverrideCommandOptions & { | ||
81 | id: number | ||
82 | }) { | ||
83 | const { id } = options | ||
84 | const path = '/api/v1/users/registrations/' + id | ||
85 | |||
86 | return this.deleteRequest({ | ||
87 | ...options, | ||
88 | |||
89 | path, | ||
90 | implicitToken: true, | ||
91 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
92 | }) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | list (options: OverrideCommandOptions & { | ||
98 | start?: number | ||
99 | count?: number | ||
100 | sort?: string | ||
101 | search?: string | ||
102 | } = {}) { | ||
103 | const path = '/api/v1/users/registrations' | ||
104 | |||
105 | return this.getRequestBody<ResultList<UserRegistration>>({ | ||
106 | ...options, | ||
107 | |||
108 | path, | ||
109 | query: pick(options, [ 'start', 'count', 'sort', 'search' ]), | ||
110 | implicitToken: true, | ||
111 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
112 | }) | ||
113 | } | ||
114 | |||
115 | // --------------------------------------------------------------------------- | ||
116 | |||
117 | askSendVerifyEmail (options: OverrideCommandOptions & { | ||
118 | email: string | ||
119 | }) { | ||
120 | const { email } = options | ||
121 | const path = '/api/v1/users/registrations/ask-send-verify-email' | ||
122 | |||
123 | return this.postBodyRequest({ | ||
124 | ...options, | ||
125 | |||
126 | path, | ||
127 | fields: { email }, | ||
128 | implicitToken: false, | ||
129 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | verifyEmail (options: OverrideCommandOptions & { | ||
134 | registrationId: number | ||
135 | verificationString: string | ||
136 | }) { | ||
137 | const { registrationId, verificationString } = options | ||
138 | const path = '/api/v1/users/registrations/' + registrationId + '/verify-email' | ||
139 | |||
140 | return this.postBodyRequest({ | ||
141 | ...options, | ||
142 | |||
143 | path, | ||
144 | fields: { | ||
145 | verificationString | ||
146 | }, | ||
147 | implicitToken: false, | ||
148 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
149 | }) | ||
150 | } | ||
151 | } | ||