From 1fd58aeaddee8749c27357a4a900314a89a45577 Mon Sep 17 00:00:00 2001 From: RAHUL RATHORE <41804588+rahul-rocket@users.noreply.github.com> Date: Sat, 3 Feb 2024 10:52:01 +0530 Subject: [PATCH 1/3] fix: added missing include teams query parameter --- packages/core/src/auth/auth.controller.ts | 26 ++++++++++++++------ packages/core/src/auth/dto/two-factor-dto.ts | 16 ++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/core/src/auth/auth.controller.ts b/packages/core/src/auth/auth.controller.ts index 0d31a9e6c6e..3f6c2619050 100644 --- a/packages/core/src/auth/auth.controller.ts +++ b/packages/core/src/auth/auth.controller.ts @@ -47,7 +47,7 @@ export class AuthController { private readonly authService: AuthService, private readonly userService: UserService, private readonly commandBus: CommandBus - ) {} + ) { } /** * Check if the user is authenticated. @@ -169,8 +169,13 @@ export class AuthController { @Post('/signin.email') @Public() @UsePipes(new ValidationPipe({ transform: true })) - async sendWorkspaceSigninCode(@Body() entity: UserEmailDTO, @I18nLang() locale: LanguagesEnum): Promise { - return await this.commandBus.execute(new WorkspaceSigninSendCodeCommand(entity, locale)); + async sendWorkspaceSigninCode( + @Body() entity: UserEmailDTO, + @I18nLang() locale: LanguagesEnum + ): Promise { + return await this.commandBus.execute( + new WorkspaceSigninSendCodeCommand(entity, locale) + ); } /** @@ -185,8 +190,11 @@ export class AuthController { async confirmWorkspaceSigninByCode( @Query() query: Record, @Body() input: WorkspaceSigninEmailVerifyDTO - ): Promise { - return await this.authService.confirmWorkspaceSigninByCode(input, parseToBoolean(query.includeTeams)); + ): Promise { + return await this.authService.confirmWorkspaceSigninByCode( + input, + parseToBoolean(query.includeTeams) + ); } /** @@ -198,8 +206,12 @@ export class AuthController { @Post('/signin.workspace') @Public() @UsePipes(new ValidationPipe({ whitelist: true })) - async signinWorkspaceByToken(@Body() input: WorkspaceSigninDTO): Promise { - return await this.commandBus.execute(new WorkspaceSigninVerifyTokenCommand(input)); + async signinWorkspaceByToken( + @Body() input: WorkspaceSigninDTO + ): Promise { + return await this.commandBus.execute( + new WorkspaceSigninVerifyTokenCommand(input) + ); } /** diff --git a/packages/core/src/auth/dto/two-factor-dto.ts b/packages/core/src/auth/dto/two-factor-dto.ts index 9836c396914..af13bff5fff 100644 --- a/packages/core/src/auth/dto/two-factor-dto.ts +++ b/packages/core/src/auth/dto/two-factor-dto.ts @@ -1,13 +1,25 @@ -import { IntersectionType } from "@nestjs/swagger"; +import { ApiPropertyOptional, IntersectionType } from "@nestjs/swagger"; +import { IsBoolean, IsOptional } from "class-validator"; import { IUserCodeInput, IUserEmailInput, IUserTokenInput } from "@gauzy/contracts"; import { UserCodeDTO, UserEmailDTO, UserTokenDTO } from "../../user/dto"; +/** + * + */ export class WorkspaceSigninEmailVerifyDTO extends IntersectionType( UserEmailDTO, UserCodeDTO, -) implements IUserEmailInput, IUserCodeInput { } +) implements IUserEmailInput, IUserCodeInput { + @ApiPropertyOptional({ type: () => Boolean }) + @IsOptional() + @IsBoolean() + readonly includeTeams: boolean; +} +/** + * + */ export class WorkspaceSigninDTO extends IntersectionType( UserEmailDTO, UserTokenDTO, From 09f5d45ba8df8430d866be55d51a6e893ac1628c Mon Sep 17 00:00:00 2001 From: RAHUL RATHORE <41804588+rahul-rocket@users.noreply.github.com> Date: Sat, 3 Feb 2024 11:23:05 +0530 Subject: [PATCH 2/3] fix: get teams for workspace login --- packages/core/src/auth/auth.service.ts | 77 +++++++++----------------- 1 file changed, 25 insertions(+), 52 deletions(-) diff --git a/packages/core/src/auth/auth.service.ts b/packages/core/src/auth/auth.service.ts index 30ea1753506..24d8e73e48a 100644 --- a/packages/core/src/auth/auth.service.ts +++ b/packages/core/src/auth/auth.service.ts @@ -906,83 +906,56 @@ export class AuthService extends SocialAuthService { employeeId: string | null ): Promise { const query = this.typeOrmOrganizationTeamRepository.createQueryBuilder("organization_team"); - query.innerJoin('organization_team_employee', - p("team_member"), - p('"team_member"."organizationTeamId" = "organization_team"."id"') - ); + query.innerJoin('organization_team_employee', "team_member", '"team_member"."organizationTeamId" = "organization_team"."id"'); query.select([ - p(`"${query.alias}"."id" AS "team_id"`), - p(`"${query.alias}"."name" AS "team_name"`), - p(`"${query.alias}"."logo" AS "team_logo"`), - p(`COALESCE(COUNT("team_member"."id"), 0) AS "team_member_count"`), - p(`"${query.alias}"."profile_link" AS "profile_link"`), - p(`"${query.alias}"."prefix" AS "prefix")`) + `"${query.alias}"."id" AS "team_id"`, + `"${query.alias}"."name" AS "team_name"`, + `"${query.alias}"."logo" AS "team_logo"`, + `COALESCE(COUNT("team_member"."id"), 0) AS "team_member_count"`, + `"${query.alias}"."profile_link" AS "profile_link"`, + `"${query.alias}"."prefix" AS "prefix"` ]); - query.andWhere( - p(`"${query.alias}"."tenantId" = :tenantId`), { tenantId } - ); + query.andWhere(`"${query.alias}"."tenantId" = :tenantId`, { tenantId }); + query.andWhere(`"${query.alias}"."isActive" = :isActive`, { isActive: true }); + query.andWhere(`"${query.alias}"."isArchived" = :isArchived`, { isArchived: false }); // Sub Query to get only assigned teams for specific organizations const orgSubQuery = (cb: SelectQueryBuilder): string => { - const subQuery = cb.subQuery() - .select( - p('"user_organization"."organizationId"') - ).from( - p("user_organization"), - p("user_organization") - ); - subQuery.andWhere( - p(`"${subQuery.alias}"."isActive" = true`) - ); - subQuery.andWhere( - p(`"${subQuery.alias}"."userId" = :userId`), { userId } - ); - subQuery.andWhere( - p(`"${subQuery.alias}"."tenantId" = :tenantId`), { tenantId } - ); + const subQuery = cb.subQuery().select('"user_organization"."organizationId"').from("user_organization", "user_organization"); + subQuery.andWhere(`"${subQuery.alias}"."isActive" = :isActive`, { isActive: true }); + subQuery.andWhere(`"${subQuery.alias}"."isArchived" = :isArchived`, { isArchived: false }); + subQuery.andWhere(`"${subQuery.alias}"."userId" = :userId`, { userId }); + subQuery.andWhere(`"${subQuery.alias}"."tenantId" = :tenantId`, { tenantId }); return subQuery.distinct(true).getQuery(); }; // Sub Query to get only assigned teams for specific organizations query.andWhere((cb: SelectQueryBuilder) => { - return (p(`"${query.alias}"."organizationId" IN `) + orgSubQuery(cb)); + return (`"${query.alias}"."organizationId" IN ` + orgSubQuery(cb)); }); // Sub Query to get only assigned teams for a specific employee for specific tenant query.andWhere((cb: SelectQueryBuilder) => { - const subQuery = cb.subQuery() - .select( - p('"organization_team_employee"."organizationTeamId"') - ) - .from( - p("organization_team_employee"), - p("organization_team_employee") - ); - subQuery.andWhere( - p(`"${subQuery.alias}"."tenantId" = :tenantId`), { tenantId } - ); + const subQuery = cb.subQuery().select('"organization_team_employee"."organizationTeamId"').from("organization_team_employee", "organization_team_employee"); + subQuery.andWhere(`"${subQuery.alias}"."tenantId" = :tenantId`, { tenantId }); + subQuery.andWhere(`"${subQuery.alias}"."isActive" = :isActive`, { isActive: true }); + subQuery.andWhere(`"${subQuery.alias}"."isArchived" = :isArchived`, { isArchived: false }); if (isNotEmpty(employeeId)) { - subQuery.andWhere( - p(`"${subQuery.alias}"."employeeId" = :employeeId`), { employeeId } - ); + subQuery.andWhere(`"${subQuery.alias}"."employeeId" = :employeeId`, { employeeId }); } // Sub Query to get only assigned teams for specific organizations subQuery.andWhere((cb: SelectQueryBuilder) => { - return (p(`"${subQuery.alias}"."organizationId" IN `) + orgSubQuery(cb)); + return (`"${subQuery.alias}"."organizationId" IN ` + orgSubQuery(cb)); }); - return (p(`"${query.alias}"."id" IN `) + subQuery.distinct(true).getQuery()); + return (`"${query.alias}"."id" IN ` + subQuery.distinct(true).getQuery()); }); - query.addGroupBy( - p(`"${query.alias}"."id"`) - ); - query.orderBy( - p(`"${query.alias}"."createdAt"`), 'DESC' - ); + query.addGroupBy(`"${query.alias}"."id"`); + query.orderBy(`"${query.alias}"."createdAt"`, 'DESC'); return await query.getRawMany(); } From e30780e7958c8aeb2bafe7e7820f963394d7da48 Mon Sep 17 00:00:00 2001 From: RAHUL RATHORE <41804588+rahul-rocket@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:15:01 +0530 Subject: [PATCH 3/3] fix: query parsing for mysql & postgresql --- packages/core/src/auth/auth.service.ts | 51 +++++++++++++------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/core/src/auth/auth.service.ts b/packages/core/src/auth/auth.service.ts index 24d8e73e48a..ac371b0dc4a 100644 --- a/packages/core/src/auth/auth.service.ts +++ b/packages/core/src/auth/auth.service.ts @@ -906,56 +906,57 @@ export class AuthService extends SocialAuthService { employeeId: string | null ): Promise { const query = this.typeOrmOrganizationTeamRepository.createQueryBuilder("organization_team"); - query.innerJoin('organization_team_employee', "team_member", '"team_member"."organizationTeamId" = "organization_team"."id"'); + query.innerJoin(`organization_team_employee`, `team_member`, p('"team_member"."organizationTeamId" = "organization_team"."id"')); query.select([ - `"${query.alias}"."id" AS "team_id"`, - `"${query.alias}"."name" AS "team_name"`, - `"${query.alias}"."logo" AS "team_logo"`, - `COALESCE(COUNT("team_member"."id"), 0) AS "team_member_count"`, - `"${query.alias}"."profile_link" AS "profile_link"`, - `"${query.alias}"."prefix" AS "prefix"` + p(`"${query.alias}"."id" AS "team_id"`), + p(`"${query.alias}"."name" AS "team_name"`), + p(`"${query.alias}"."logo" AS "team_logo"`), + p(`COALESCE(COUNT("team_member"."id"), 0) AS "team_member_count"`), + p(`"${query.alias}"."profile_link" AS "profile_link"`), + p(`"${query.alias}"."prefix" AS "prefix"`) ]); - query.andWhere(`"${query.alias}"."tenantId" = :tenantId`, { tenantId }); - query.andWhere(`"${query.alias}"."isActive" = :isActive`, { isActive: true }); - query.andWhere(`"${query.alias}"."isArchived" = :isArchived`, { isArchived: false }); + query.andWhere(p(`"${query.alias}"."tenantId" = :tenantId`), { tenantId }); + query.andWhere(p(`"${query.alias}"."isActive" = :isActive`), { isActive: true }); + query.andWhere(p(`"${query.alias}"."isArchived" = :isArchived`), { isArchived: false }); // Sub Query to get only assigned teams for specific organizations const orgSubQuery = (cb: SelectQueryBuilder): string => { - const subQuery = cb.subQuery().select('"user_organization"."organizationId"').from("user_organization", "user_organization"); - subQuery.andWhere(`"${subQuery.alias}"."isActive" = :isActive`, { isActive: true }); - subQuery.andWhere(`"${subQuery.alias}"."isArchived" = :isArchived`, { isArchived: false }); - subQuery.andWhere(`"${subQuery.alias}"."userId" = :userId`, { userId }); - subQuery.andWhere(`"${subQuery.alias}"."tenantId" = :tenantId`, { tenantId }); + const subQuery = cb.subQuery().select(p('"user_organization"."organizationId"')).from("user_organization", "user_organization"); + subQuery.andWhere(p(`"${subQuery.alias}"."isActive" = :isActive`), { isActive: true }); + subQuery.andWhere(p(`"${subQuery.alias}"."isArchived" = :isArchived`), { isArchived: false }); + subQuery.andWhere(p(`"${subQuery.alias}"."userId" = :userId`), { userId }); + subQuery.andWhere(p(`"${subQuery.alias}"."tenantId" = :tenantId`), { tenantId }); return subQuery.distinct(true).getQuery(); }; // Sub Query to get only assigned teams for specific organizations query.andWhere((cb: SelectQueryBuilder) => { - return (`"${query.alias}"."organizationId" IN ` + orgSubQuery(cb)); + return (p(`"${query.alias}"."organizationId" IN ` + orgSubQuery(cb))); }); // Sub Query to get only assigned teams for a specific employee for specific tenant query.andWhere((cb: SelectQueryBuilder) => { - const subQuery = cb.subQuery().select('"organization_team_employee"."organizationTeamId"').from("organization_team_employee", "organization_team_employee"); - subQuery.andWhere(`"${subQuery.alias}"."tenantId" = :tenantId`, { tenantId }); - subQuery.andWhere(`"${subQuery.alias}"."isActive" = :isActive`, { isActive: true }); - subQuery.andWhere(`"${subQuery.alias}"."isArchived" = :isArchived`, { isArchived: false }); + const subQuery = cb.subQuery().select(p('"organization_team_employee"."organizationTeamId"')).from("organization_team_employee", "organization_team_employee"); + subQuery.andWhere(p(`"${subQuery.alias}"."isActive" = :isActive`), { isActive: true }); + subQuery.andWhere(p(`"${subQuery.alias}"."isArchived" = :isArchived`), { isArchived: false }); + subQuery.andWhere(p(`"${subQuery.alias}"."tenantId" = :tenantId`), { tenantId }); if (isNotEmpty(employeeId)) { - subQuery.andWhere(`"${subQuery.alias}"."employeeId" = :employeeId`, { employeeId }); + subQuery.andWhere(p(`"${subQuery.alias}"."employeeId" = :employeeId`), { employeeId }); } // Sub Query to get only assigned teams for specific organizations subQuery.andWhere((cb: SelectQueryBuilder) => { - return (`"${subQuery.alias}"."organizationId" IN ` + orgSubQuery(cb)); + return (p(`"${subQuery.alias}"."organizationId" IN ` + orgSubQuery(cb))); }); - return (`"${query.alias}"."id" IN ` + subQuery.distinct(true).getQuery()); + + return (p(`"${query.alias}"."id" IN ` + subQuery.distinct(true).getQuery())); }); - query.addGroupBy(`"${query.alias}"."id"`); - query.orderBy(`"${query.alias}"."createdAt"`, 'DESC'); + query.addGroupBy(p(`"${query.alias}"."id"`)); + query.orderBy(p(`"${query.alias}"."createdAt"`), 'DESC'); return await query.getRawMany(); }