Skip to content

Commit b097ad5

Browse files
authored
Merge pull request #1853 from rocket-admin/backend_tests
feat(saas): add endpoint to retrieve companies associated with a user's email
2 parents 7e6c052 + ec69410 commit b097ad5

5 files changed

Lines changed: 71 additions & 0 deletions

File tree

backend/src/common/data-injection.tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export enum UseCaseType {
113113
SAAS_GET_USER_INFO = 'SAAS_GET_USER_INFO',
114114
SAAS_USUAL_REGISTER_USER = 'SAAS_USUAL_REGISTER_USER',
115115
SAAS_USUAL_LOGIN_USER = 'SAAS_USUAL_LOGIN_USER',
116+
SAAS_GET_USER_EMAIL_COMPANIES = 'SAAS_GET_USER_EMAIL_COMPANIES',
116117
SAAS_DEMO_USER_REGISTRATION = 'SAAS_DEMO_USER_REGISTRATION',
117118
SAAS_LOGIN_USER_WITH_GOOGLE = 'SAAS_LOGIN_USER_WITH_GOOGLE',
118119
SAAS_LOGIN_USER_WITH_GITHUB = 'SAAS_LOGIN_USER_WITH_GITHUB',

backend/src/microservices/saas-microservice/saas.controller.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nes
1515
import { SkipThrottle } from '@nestjs/throttler';
1616
import { UseCaseType } from '../../common/data-injection.tokens.js';
1717
import { Timeout } from '../../decorators/timeout.decorator.js';
18+
import { FoundUserEmailCompaniesInfoDs } from '../../entities/company-info/application/data-structures/found-company-info.ds.js';
1819
import { CompanyInfoEntity } from '../../entities/company-info/company-info.entity.js';
1920
import { CreatedConnectionDTO } from '../../entities/connection/application/dto/created-connection.dto.js';
2021
import { SaasUsualUserRegisterDS } from '../../entities/user/application/data-structures/usual-register-user.ds.js';
@@ -23,6 +24,7 @@ import { ExternalRegistrationProviderEnum } from '../../entities/user/enums/exte
2324
import { UserEntity } from '../../entities/user/user.entity.js';
2425
import { InTransactionEnum } from '../../enums/in-transaction.enum.js';
2526
import { Messages } from '../../exceptions/text/messages.js';
27+
import { ValidationHelper } from '../../helpers/validators/validation-helper.js';
2628
import { SentryInterceptor } from '../../interceptors/sentry.interceptor.js';
2729
import { CreatedConnectionResponse, SuccessResponse } from './data-structures/common-responce.ds.js';
2830
import { CreateConnectionForHostedDbDto } from './data-structures/create-connecttion-for-selfhosted-db.dto.js';
@@ -51,6 +53,7 @@ import {
5153
ISaaSGetCompanyInfoByUserId,
5254
ISaaSGetUsersCountInCompany,
5355
ISaasDemoRegisterUser,
56+
ISaasGetUserEmailCompanies,
5457
ISaasGetUsersInfosByEmail,
5558
ISaasRegisterUser,
5659
ISaasSAMLRegisterUser,
@@ -79,6 +82,8 @@ export class SaasController {
7982
private readonly usualRegisterUserUseCase: ISaasRegisterUser,
8083
@Inject(UseCaseType.SAAS_USUAL_LOGIN_USER)
8184
private readonly usualLoginUserUseCase: ISaasUsualLoginUser,
85+
@Inject(UseCaseType.SAAS_GET_USER_EMAIL_COMPANIES)
86+
private readonly getUserEmailCompaniesUseCase: ISaasGetUserEmailCompanies,
8287
@Inject(UseCaseType.SAAS_DEMO_USER_REGISTRATION)
8388
private readonly demoRegisterUserUseCase: ISaasDemoRegisterUser,
8489
@Inject(UseCaseType.SAAS_LOGIN_USER_WITH_GOOGLE)
@@ -194,6 +199,19 @@ export class SaasController {
194199
);
195200
}
196201

202+
@ApiOperation({ summary: 'Get companies where a user with this email is registered' })
203+
@ApiResponse({
204+
status: 200,
205+
description: 'Companies where a user with this email is registered.',
206+
type: FoundUserEmailCompaniesInfoDs,
207+
isArray: true,
208+
})
209+
@Get('user/email/:email/companies')
210+
async getUserEmailCompanies(@Param('email') email: string): Promise<Array<FoundUserEmailCompaniesInfoDs>> {
211+
ValidationHelper.validateOrThrowHttpExceptionEmail(email);
212+
return await this.getUserEmailCompaniesUseCase.execute(email);
213+
}
214+
197215
@ApiOperation({ summary: 'Register demo user register webhook' })
198216
@ApiBody({ type: SaasUsualUserRegisterDS })
199217
@ApiResponse({

backend/src/microservices/saas-microservice/saas.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { LoginWithGoogleUseCase } from './use-cases/login-with-google.use.case.j
2121
import { RegisteredCompanyWebhookUseCase } from './use-cases/register-company-webhook.use.case.js';
2222
import { SaasRegisterDemoUserAccountUseCase } from './use-cases/register-demo-user-account.use.case.js';
2323
import { SaaSRegisterUserWIthSamlUseCase } from './use-cases/register-user-with-saml-use.case.js';
24+
import { SaasGetUserEmailCompaniesUseCase } from './use-cases/saas-get-user-email-companies.use.case.js';
2425
import { SaasUsualLoginUseCase } from './use-cases/saas-usual-login.use.case.js';
2526
import { SaasUsualRegisterUseCase } from './use-cases/saas-usual-register-user.use.case.js';
2627
import { SuspendUsersUseCase } from './use-cases/suspend-users.use.case.js';
@@ -51,6 +52,10 @@ import { UpdateHostedConnectionPasswordUseCase } from './use-cases/update-hosted
5152
provide: UseCaseType.SAAS_USUAL_LOGIN_USER,
5253
useClass: SaasUsualLoginUseCase,
5354
},
55+
{
56+
provide: UseCaseType.SAAS_GET_USER_EMAIL_COMPANIES,
57+
useClass: SaasGetUserEmailCompaniesUseCase,
58+
},
5459
{
5560
provide: UseCaseType.SAAS_LOGIN_USER_WITH_GOOGLE,
5661
useClass: LoginWithGoogleUseCase,
@@ -130,6 +135,7 @@ export class SaasModule {
130135
{ path: 'saas/users/email/:userEmail', method: RequestMethod.GET },
131136
{ path: 'saas/user/register', method: RequestMethod.POST },
132137
{ path: 'saas/user/login', method: RequestMethod.POST },
138+
{ path: 'saas/user/email/:email/companies', method: RequestMethod.GET },
133139
{ path: 'saas/user/demo/register', method: RequestMethod.POST },
134140
{ path: 'saas/user/google/login', method: RequestMethod.POST },
135141
{ path: 'saas/user/github/login', method: RequestMethod.POST },
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
2+
import AbstractUseCase from '../../../common/abstract-use.case.js';
3+
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
4+
import { BaseType } from '../../../common/data-injection.tokens.js';
5+
import { FoundUserEmailCompaniesInfoDs } from '../../../entities/company-info/application/data-structures/found-company-info.ds.js';
6+
import { Messages } from '../../../exceptions/text/messages.js';
7+
import { ISaasGetUserEmailCompanies } from './saas-use-cases.interface.js';
8+
9+
/**
10+
* Returns the companies a given email is registered in. This is the SaaS microservice bridge for the
11+
* open-source `GET /my/email/:email` endpoint (GetUserEmailCompaniesUseCase) — duplicated here on
12+
* purpose so rocketadmin-saas can expose the same lookup (used by the multi-company login picker)
13+
* through its own `/saas/*` surface. The open-source endpoint is left untouched.
14+
*/
15+
@Injectable()
16+
export class SaasGetUserEmailCompaniesUseCase
17+
extends AbstractUseCase<string, Array<FoundUserEmailCompaniesInfoDs>>
18+
implements ISaasGetUserEmailCompanies
19+
{
20+
constructor(
21+
@Inject(BaseType.GLOBAL_DB_CONTEXT)
22+
protected _dbContext: IGlobalDatabaseContext,
23+
) {
24+
super();
25+
}
26+
27+
protected async implementation(userEmail: string): Promise<Array<FoundUserEmailCompaniesInfoDs>> {
28+
const foundCompanies = await this._dbContext.companyInfoRepository.findCompanyInfosByUserEmail(
29+
userEmail.toLowerCase(),
30+
);
31+
if (!foundCompanies.length) {
32+
throw new HttpException(
33+
{
34+
message: Messages.COMPANIES_USER_EMAIL_NOT_FOUND,
35+
},
36+
HttpStatus.BAD_REQUEST,
37+
);
38+
}
39+
return foundCompanies.map(({ id, name }) => ({ id, name }));
40+
}
41+
}

backend/src/microservices/saas-microservice/use-cases/saas-use-cases.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FoundUserEmailCompaniesInfoDs } from '../../../entities/company-info/application/data-structures/found-company-info.ds.js';
12
import { CompanyInfoEntity } from '../../../entities/company-info/company-info.entity.js';
23
import { CreatedConnectionDTO } from '../../../entities/connection/application/dto/created-connection.dto.js';
34
import { SaaSRegisterDemoUserAccountDS } from '../../../entities/user/application/data-structures/demo-user-account-register.ds.js';
@@ -45,6 +46,10 @@ export interface ISaasUsualLoginUser {
4546
execute(userData: UsualLoginDs, inTransaction?: InTransactionEnum): Promise<FoundUserDto>;
4647
}
4748

49+
export interface ISaasGetUserEmailCompanies {
50+
execute(userEmail: string): Promise<Array<FoundUserEmailCompaniesInfoDs>>;
51+
}
52+
4853
export interface ISaasDemoRegisterUser {
4954
execute(userData: SaaSRegisterDemoUserAccountDS): Promise<FoundUserDto>;
5055
}

0 commit comments

Comments
 (0)