Skip to content

Commit

Permalink
fix: #6909 eager loading query set find option issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-rocket committed Oct 31, 2023
1 parent 409c664 commit 813177d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const routes: Routes = [
path: 'main',
component: EditOrganizationMainComponent,
data: {
relations: ['tags', 'image'],
relations: ['tags'],
selectors: {
project: false,
employee: false,
Expand All @@ -53,7 +53,7 @@ const routes: Routes = [
path: 'location',
component: EditOrganizationLocationComponent,
data: {
relations: ['contact', 'image'],
relations: ['contact'],
selectors: {
project: false,
employee: false,
Expand All @@ -65,7 +65,7 @@ const routes: Routes = [
path: 'settings',
component: EditOrganizationOtherSettingsComponent,
data: {
relations: ['accountingTemplates', 'image'],
relations: ['accountingTemplates'],
selectors: {
project: false,
employee: false,
Expand Down
3 changes: 1 addition & 2 deletions apps/gauzy/src/app/pages/teams/teams.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ export class TeamsComponent extends PaginationFilterBaseComponent
'members.role',
'members.employee.user',
'tags',
'projects',
'image'
'projects'
],
where: {
organizationId,
Expand Down
87 changes: 35 additions & 52 deletions packages/core/src/organization-team/organization-team.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,94 +266,77 @@ export class OrganizationTeamService extends TenantAwareCrudService<Organization
*/
public async findAll(
options?: PaginationParams<OrganizationTeam>
): Promise<IPagination<OrganizationTeam>> {
): Promise<IPagination<IOrganizationTeam>> {
const tenantId = RequestContext.currentTenantId();
const employeeId = RequestContext.currentEmployeeId();

const members = options.where.members;
if ('members' in options.where) {
const members = options?.where.members;
if ('members' in options?.where) {
delete options.where['members'];
}

const query = this.repository.createQueryBuilder(this.alias);

// If employee has login and don't have permission to change employee
if (
employeeId &&
!RequestContext.hasPermission(
PermissionsEnum.CHANGE_SELECTED_EMPLOYEE
)
) {
if (employeeId && !RequestContext.hasPermission(PermissionsEnum.CHANGE_SELECTED_EMPLOYEE)) {
// Sub query to get only employee assigned teams
query.andWhere((cb: SelectQueryBuilder<OrganizationTeam>) => {
const subQuery = cb
.subQuery()
.select('"team"."organizationTeamId"')
.from('organization_team_employee', 'team');
subQuery.andWhere(`"${query.alias}"."tenantId" = :tenantId`, {
tenantId,
});
const subQuery = cb.subQuery().select('"team"."organizationTeamId"');
subQuery.from('organization_team_employee', 'team');

if (isNotEmpty(options) && isNotEmpty(options.where)) {
// Apply the tenant filter
subQuery.andWhere(`"${query.alias}"."tenantId" = :tenantId`, { tenantId });

if (options?.where.organizationId) {
const { organizationId } = options.where;
subQuery.andWhere(
`"${query.alias}"."organizationId" = :organizationId`,
{ organizationId }
);
subQuery.andWhere(`"${query.alias}"."organizationId" = :organizationId`, { organizationId });
}

subQuery.andWhere('"team"."employeeId" = :employeeId', {
employeeId,
});
return (
'"organization_team"."id" IN ' +
subQuery.distinct(true).getQuery()
);
subQuery.andWhere('"team"."employeeId" = :employeeId', { employeeId });
return `"organization_team"."id" IN ${subQuery.distinct(true).getQuery()}`;
});
} else {
if (isNotEmpty(members) && isNotEmpty(members['employeeId'])) {
// Sub query to get only employee assigned teams
query.andWhere((cb: SelectQueryBuilder<OrganizationTeam>) => {
const subQuery = cb
.subQuery()
.select('"team"."organizationTeamId"')
.from('organization_team_employee', 'team');
subQuery.andWhere(
`"${query.alias}"."tenantId" = :tenantId`,
{ tenantId }
);

if (isNotEmpty(options) && isNotEmpty(options.where)) {
const subQuery = cb.subQuery().select('"team"."organizationTeamId"');
subQuery.from('organization_team_employee', 'team');

// Apply the tenant filter
subQuery.andWhere(`"${query.alias}"."tenantId" = :tenantId`, { tenantId });

if (options?.where.organizationId) {
const { organizationId } = options.where;
subQuery.andWhere(
`"${query.alias}"."organizationId" = :organizationId`,
{ organizationId }
);
subQuery.andWhere(`"${query.alias}"."organizationId" = :organizationId`, { organizationId });
}

const employeeId = members['employeeId'];
subQuery.andWhere('"team"."employeeId" = :employeeId', {
employeeId,
});
return (
'"organization_team"."id" IN ' +
subQuery.distinct(true).getQuery()
);
subQuery.andWhere('"team"."employeeId" = :employeeId', { employeeId: members['employeeId'] });
return `"organization_team"."id" IN ${subQuery.distinct(true).getQuery()}`;
});
}
}

// Set query options
if (isNotEmpty(options)) {
query.setFindOptions({
skip: options.skip ? options.take * (options.skip - 1) : 0,
take: options.take ? options.take : 10,
});
query.setFindOptions({
...(options.select ? { select: options.select } : {}),
...(options.relations ? { relations: options.relations } : {}),
...(options.where ? { where: options.where } : {}),
...(options.order ? { order: options.order } : {}),
});
}

// Apply the tenant filter
query.andWhere(`"${query.alias}"."tenantId" = :tenantId`, { tenantId });

// Apply the organization filter
if (options?.where.organizationId) {
const { organizationId } = options.where;
query.andWhere(`"${query.alias}"."organizationId" = :organizationId`, { organizationId });
}

const [items, total] = await query.getManyAndCount();
return { items, total };
}
Expand Down

0 comments on commit 813177d

Please sign in to comment.