-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsaas.controller.ts
More file actions
312 lines (295 loc) · 11.5 KB
/
Copy pathsaas.controller.ts
File metadata and controls
312 lines (295 loc) · 11.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {
BadRequestException,
Body,
Controller,
Get,
Inject,
Injectable,
Param,
Post,
Put,
Query,
UseInterceptors,
} from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { SkipThrottle } from '@nestjs/throttler';
import { UseCaseType } from '../../common/data-injection.tokens.js';
import { Timeout } from '../../decorators/timeout.decorator.js';
import { CompanyInfoEntity } from '../../entities/company-info/company-info.entity.js';
import { SaasUsualUserRegisterDS } from '../../entities/user/application/data-structures/usual-register-user.ds.js';
import { FoundUserDto } from '../../entities/user/dto/found-user.dto.js';
import { ExternalRegistrationProviderEnum } from '../../entities/user/enums/external-registration-provider.enum.js';
import { UserEntity } from '../../entities/user/user.entity.js';
import { InTransactionEnum } from '../../enums/in-transaction.enum.js';
import { Messages } from '../../exceptions/text/messages.js';
import { SentryInterceptor } from '../../interceptors/sentry.interceptor.js';
import { SuccessResponse } from './data-structures/common-responce.ds.js';
import { RegisterCompanyWebhookDS } from './data-structures/register-company.ds.js';
import { RegisteredCompanyDS } from './data-structures/registered-company.ds.js';
import { SaasRegisterUserWithGithub } from './data-structures/saas-register-user-with-github.js';
import { SaasSAMLUserRegisterDS } from './data-structures/saas-saml-user-register.ds.js';
import { SaasRegisterUserWithGoogleDS } from './data-structures/sass-register-user-with-google.js';
import {
ICompanyRegistration,
ICreateConnectionForHostedDb,
IDeleteConnectionForHostedDb,
IFreezeConnectionsInCompany,
IGetUserInfo,
ILoginUserWithGitHub,
ILoginUserWithGoogle,
ISaaSGetCompanyInfoByUserId,
ISaaSGetUsersCountInCompany,
ISaasDemoRegisterUser,
ISaasGetUsersInfosByEmail,
ISaasRegisterUser,
ISaasSAMLRegisterUser,
ISuspendUsers,
ISuspendUsersOverLimit,
} from './use-cases/saas-use-cases.interface.js';
import { CreatedConnectionDTO } from '../../entities/connection/application/dto/created-connection.dto.js';
import { CreateConnectionForHostedDbDto } from './data-structures/create-connecttion-for-selfhosted-db.dto.js';
import { DeleteConnectionForHostedDbDto } from './data-structures/delete-connection-for-hosted-db.dto.js';
@UseInterceptors(SentryInterceptor)
@SkipThrottle()
@Timeout()
@Controller('saas')
@ApiBearerAuth()
@ApiTags('saas')
@Injectable()
export class SaasController {
constructor(
@Inject(UseCaseType.SAAS_COMPANY_REGISTRATION)
private readonly companyRegistrationUseCase: ICompanyRegistration,
@Inject(UseCaseType.SAAS_GET_USER_INFO)
private readonly getUserInfoUseCase: IGetUserInfo,
@Inject(UseCaseType.SAAS_SAAS_GET_USERS_INFOS_BY_EMAIL)
private readonly getUsersInfosByEmailUseCase: ISaasGetUsersInfosByEmail,
@Inject(UseCaseType.SAAS_USUAL_REGISTER_USER)
private readonly usualRegisterUserUseCase: ISaasRegisterUser,
@Inject(UseCaseType.SAAS_DEMO_USER_REGISTRATION)
private readonly demoRegisterUserUseCase: ISaasDemoRegisterUser,
@Inject(UseCaseType.SAAS_LOGIN_USER_WITH_GOOGLE)
private readonly loginUserWithGoogleUseCase: ILoginUserWithGoogle,
@Inject(UseCaseType.SAAS_LOGIN_USER_WITH_GITHUB)
private readonly loginUserWithGithubUseCase: ILoginUserWithGitHub,
@Inject(UseCaseType.SAAS_REGISTER_USER_WITH_SAML)
private readonly registerUserWithSamlUseCase: ISaasSAMLRegisterUser,
@Inject(UseCaseType.SAAS_SUSPEND_USERS)
private readonly suspendUsersUseCase: ISuspendUsers,
@Inject(UseCaseType.SAAS_SUSPEND_USERS_OVER_LIMIT)
private readonly suspendUsersOverLimitUseCase: ISuspendUsersOverLimit,
@Inject(UseCaseType.SAAS_GET_COMPANY_INFO_BY_USER_ID)
private readonly getCompanyInfoByUserIdUseCase: ISaaSGetCompanyInfoByUserId,
@Inject(UseCaseType.SAAS_GET_USERS_COUNT_IN_COMPANY)
private readonly getUsersCountInCompanyByIdUseCase: ISaaSGetUsersCountInCompany,
@Inject(UseCaseType.FREEZE_CONNECTIONS_IN_COMPANY)
private readonly freezeConnectionsInCompanyUseCase: IFreezeConnectionsInCompany,
@Inject(UseCaseType.UNFREEZE_CONNECTIONS_IN_COMPANY)
private readonly unfreezeConnectionsInCompanyUseCase: IFreezeConnectionsInCompany,
@Inject(UseCaseType.SAAS_CREATE_CONNECTION_FOR_HOSTED_DB)
private readonly createConnectionForHostedDbUseCase: ICreateConnectionForHostedDb,
@Inject(UseCaseType.SAAS_DELETE_CONNECTION_FOR_HOSTED_DB)
private readonly deleteConnectionForHostedDbUseCase: IDeleteConnectionForHostedDb,
) {}
@ApiOperation({ summary: 'Company registered webhook' })
@ApiBody({ type: RegisterCompanyWebhookDS })
@ApiResponse({
status: 201,
description: 'The company has been successfully created.',
type: RegisteredCompanyDS,
})
@Post('/company/registered')
async companyRegistered(
@Body('userId') registrarUserId: string,
@Body('companyId') companyId: string,
@Body('companyName') companyName: string,
): Promise<RegisteredCompanyDS> {
const result = await this.companyRegistrationUseCase.execute({ companyId, registrarUserId, companyName });
return result;
}
@ApiOperation({ summary: 'Get user info by id webhook' })
@ApiBody({ type: RegisterCompanyWebhookDS })
@ApiResponse({
status: 200,
})
@Get('/user/:userId')
async getUserInfo(@Param('userId') userId: string, @Query('companyId') companyId: string): Promise<UserEntity> {
return await this.getUserInfoUseCase.execute({ userId, companyId });
}
@ApiOperation({ summary: 'Get users infos by email webhook' })
@ApiBody({ type: Array<RegisterCompanyWebhookDS> })
@ApiResponse({
status: 200,
})
@Get('/users/email/:userEmail')
async getUsersInfoByEmail(
@Param('userEmail') userEmail: string,
@Query('externalProvider') externalProvider: ExternalRegistrationProviderEnum,
): Promise<Array<UserEntity>> {
return await this.getUsersInfosByEmailUseCase.execute({ userEmail, externalProvider });
}
@ApiOperation({ summary: 'User register webhook' })
@ApiBody({ type: SaasUsualUserRegisterDS })
@ApiResponse({
status: 201,
description: 'User has been successfully registered.',
type: FoundUserDto,
})
@Post('user/register')
async usualUserRegister(
@Body('email') email: string,
@Body('password') password: string,
@Body('gclidValue') gclidValue: string,
@Body('name') name: string,
@Body('companyId') companyId: string,
@Body('companyName') companyName: string,
): Promise<FoundUserDto> {
if (!companyId) {
throw new BadRequestException(Messages.COMPANY_ID_MISSING);
}
return await this.usualRegisterUserUseCase.execute({ email, password, gclidValue, name, companyId, companyName });
}
@ApiOperation({ summary: 'Register demo user register webhook' })
@ApiBody({ type: SaasUsualUserRegisterDS })
@ApiResponse({
status: 201,
description: 'Demo user account has been successfully registered.',
type: FoundUserDto,
})
@Post('user/demo/register')
async registerDemoUserAccount(
@Body('email') email: string,
@Body('gclidValue') gclidValue: string,
@Body('companyId') companyId: string,
@Body('companyName') companyName: string,
): Promise<FoundUserDto> {
return await this.demoRegisterUserUseCase.execute({ email, gclidValue, companyId, companyName });
}
@ApiOperation({ summary: 'Login or create user with google webhook' })
@ApiBody({ type: SaasRegisterUserWithGoogleDS })
@ApiResponse({
status: 201,
})
@Post('user/google/login')
async loginUserWithGoogle(
@Body('email') email: string,
@Body('name') name: string,
@Body('glidCookieValue') glidCookieValue: string,
@Body('ipAddress') ipAddress: string,
@Body('userAgent') userAgent: string,
): Promise<UserEntity> {
return await this.loginUserWithGoogleUseCase.execute(
{ email, name, glidCookieValue, ipAddress, userAgent },
InTransactionEnum.OFF,
);
}
@ApiOperation({ summary: 'Login or create user with github webhook' })
@ApiBody({ type: SaasRegisterUserWithGithub })
@ApiResponse({
status: 201,
})
@Post('user/github/login')
async loginUserWithGithub(
@Body('email') email: string,
@Body('name') name: string,
@Body('githubId') githubId: number,
@Body('glidCookieValue') glidCookieValue: string,
@Body('ipAddress') ipAddress: string,
@Body('userAgent') userAgent: string,
): Promise<UserEntity> {
return await this.loginUserWithGithubUseCase.execute({
email,
name,
githubId,
glidCookieValue,
ipAddress,
userAgent,
});
}
@ApiOperation({ summary: 'Suspending users' })
@Put('/company/:companyId/users/suspend')
async suspendUsers(
@Body('emailsToSuspend') emailsToSuspend: Array<string>,
@Body('companyId') companyId: string,
): Promise<SuccessResponse> {
await this.suspendUsersUseCase.execute({ emailsToSuspend, companyId });
return { success: true };
}
@ApiOperation({ summary: 'Suspending users' })
@Put('/company/:companyId/users/suspend-above-limit')
async suspendUsersOverLimit(@Body('companyId') companyId: string): Promise<SuccessResponse> {
await this.suspendUsersOverLimitUseCase.execute(companyId);
return { success: true };
}
@ApiOperation({ summary: 'Get company info by user id' })
@ApiResponse({
status: 200,
})
@Get('/user/:userId/company')
async getCompanyInfoByUserId(@Param('userId') userId: string): Promise<CompanyInfoEntity> {
return await this.getCompanyInfoByUserIdUseCase.execute(userId);
}
@ApiOperation({ summary: 'Users count in company by company id' })
@Get('/company/:companyId/users/count')
async getUsersCountInCompany(@Param('companyId') companyId: string): Promise<{ count: number }> {
const usersCount = await this.getUsersCountInCompanyByIdUseCase.execute(companyId);
return { count: usersCount };
}
@ApiOperation({ summary: 'Freeze paid connections in companies webhook' })
@Put('/company/freeze-connections')
async freezeConnectionsInCompany(@Body('companyIds') companyIds: Array<string>) {
return await this.freezeConnectionsInCompanyUseCase.execute({ companyIds });
}
@ApiOperation({ summary: 'Unfreeze paid connections in companies webhook' })
@Put('/company/unfreeze-connections')
async unfreezeConnectionsInCompany(@Body('companyIds') companyIds: Array<string>) {
return await this.unfreezeConnectionsInCompanyUseCase.execute({ companyIds });
}
@ApiOperation({ summary: 'Register user with SAML' })
@ApiBody({ type: SaasSAMLUserRegisterDS })
@ApiResponse({
status: 201,
})
@Post('user/saml/login')
async registerUserWithSaml(
@Body('email') email: string,
@Body('name') name: string,
@Body('companyId') companyId: string,
@Body('samlConfigId') samlConfigId: string,
@Body('samlNameId') samlNameId: string,
@Body('samlAttributes') samlAttributes: Record<string, any>,
): Promise<UserEntity> {
return await this.registerUserWithSamlUseCase.execute({
email,
name,
companyId,
samlConfigId,
samlNameId,
samlAttributes,
});
}
@ApiOperation({ summary: 'Created connection of hosted database' })
@ApiBody({ type: CreateConnectionForHostedDbDto })
@ApiResponse({
status: 201,
type: CreatedConnectionDTO,
})
@Post('/connection/hosted')
async createConnectionForHostedDb(
@Body() connectionData: CreateConnectionForHostedDbDto,
): Promise<CreatedConnectionDTO> {
return await this.createConnectionForHostedDbUseCase.execute(connectionData);
}
@ApiOperation({ summary: 'Delete connection of hosted database' })
@ApiBody({ type: DeleteConnectionForHostedDbDto })
@ApiResponse({
status: 201,
type: CreatedConnectionDTO,
})
@Post('/connection/hosted/delete')
async deleteConnectionForHostedDb(
@Body() deleteConnectionData: DeleteConnectionForHostedDbDto,
): Promise<CreatedConnectionDTO> {
return await this.deleteConnectionForHostedDbUseCase.execute(deleteConnectionData);
}
}