Skip to content

Commit

Permalink
Merge pull request #7495 from ever-co/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
evereq authored Feb 3, 2024
2 parents e7ecfd5 + 806fbed commit ef5483d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 56 deletions.
26 changes: 19 additions & 7 deletions packages/core/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<any> {
return await this.commandBus.execute(new WorkspaceSigninSendCodeCommand(entity, locale));
async sendWorkspaceSigninCode(
@Body() entity: UserEmailDTO,
@I18nLang() locale: LanguagesEnum
): Promise<any> {
return await this.commandBus.execute(
new WorkspaceSigninSendCodeCommand(entity, locale)
);
}

/**
Expand All @@ -185,8 +190,11 @@ export class AuthController {
async confirmWorkspaceSigninByCode(
@Query() query: Record<string, boolean>,
@Body() input: WorkspaceSigninEmailVerifyDTO
): Promise<any> {
return await this.authService.confirmWorkspaceSigninByCode(input, parseToBoolean(query.includeTeams));
): Promise<IUserSigninWorkspaceResponse> {
return await this.authService.confirmWorkspaceSigninByCode(
input,
parseToBoolean(query.includeTeams)
);
}

/**
Expand All @@ -198,8 +206,12 @@ export class AuthController {
@Post('/signin.workspace')
@Public()
@UsePipes(new ValidationPipe({ whitelist: true }))
async signinWorkspaceByToken(@Body() input: WorkspaceSigninDTO): Promise<IAuthResponse | null> {
return await this.commandBus.execute(new WorkspaceSigninVerifyTokenCommand(input));
async signinWorkspaceByToken(
@Body() input: WorkspaceSigninDTO
): Promise<IAuthResponse | null> {
return await this.commandBus.execute(
new WorkspaceSigninVerifyTokenCommand(input)
);
}

/**
Expand Down
68 changes: 21 additions & 47 deletions packages/core/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,83 +906,57 @@ export class AuthService extends SocialAuthService {
employeeId: string | null
): Promise<IOrganizationTeam[]> {
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`, p('"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")`)
p(`"${query.alias}"."prefix" AS "prefix"`)
]);

query.andWhere(
p(`"${query.alias}"."tenantId" = :tenantId`), { tenantId }
);
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<OrganizationTeam>): 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(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<OrganizationTeam>) => {
return (p(`"${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<OrganizationTeam>) => {
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(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(
p(`"${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<OrganizationTeam>) => {
return (p(`"${subQuery.alias}"."organizationId" IN `) + orgSubQuery(cb));
return (p(`"${subQuery.alias}"."organizationId" IN ` + orgSubQuery(cb)));
});
return (p(`"${query.alias}"."id" IN `) + subQuery.distinct(true).getQuery());

return (p(`"${query.alias}"."id" IN ` + subQuery.distinct(true).getQuery()));
});

query.addGroupBy(
p(`"${query.alias}"."id"`)
);
query.orderBy(
p(`"${query.alias}"."createdAt"`), 'DESC'
);
query.addGroupBy(p(`"${query.alias}"."id"`));
query.orderBy(p(`"${query.alias}"."createdAt"`), 'DESC');

return await query.getRawMany();
}
Expand Down
16 changes: 14 additions & 2 deletions packages/core/src/auth/dto/two-factor-dto.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit ef5483d

Please sign in to comment.