Skip to content

Commit

Permalink
fix: user organization for me user
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-rocket committed Dec 31, 2024
1 parent 31646a1 commit ca545bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ import { FindMeQueryDTO } from "../../user/dto";
*/
export class FindMeUserOrganizationDTO extends IntersectionType(
PickType(FindMeQueryDTO, ['includeEmployee'] as const),
) { }
) {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller, HttpStatus, Get, Query, UseGuards, HttpCode, Delete, Param } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { parseToBoolean } from '@gauzy/common';
import { IUserOrganization, IPagination, ID } from '@gauzy/contracts';
import { CrudController, PaginationParams } from './../core/crud';
import { UUIDValidationPipe } from './../shared/pipes';
Expand Down Expand Up @@ -42,7 +43,7 @@ export class UserOrganizationController extends CrudController<UserOrganization>
@Query() params: PaginationParams<UserOrganization>,
@Query() query: FindMeUserOrganizationDTO
): Promise<IPagination<IUserOrganization>> {
return await this.userOrganizationService.findUserOrganizations(params, query.includeEmployee);
return await this.userOrganizationService.findUserOrganizations(params, parseToBoolean(query.includeEmployee));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,32 @@ export class UserOrganizationService extends TenantAwareCrudService<UserOrganiza

// Extract user IDs from the items array
const userIds = items
.filter((organization: IUserOrganization) => organization.user) // Filter out user organizations without a user object
.map((organization: IUserOrganization) => organization.user.id);
.filter((organization: IUserOrganization) => organization?.user) // Filter out user organizations without a user object
.map((organization: IUserOrganization) => organization?.user?.id) || [];

// Fetch all employee details in bulk for the extracted user IDs
const employees = await this.employeeService.findEmployeesByUserIds(userIds, tenantId);

// Map employee details to a dictionary for easier lookup
const employeeMap = new Map<string, Employee>();
employees.forEach((employee: Employee) => {
employeeMap.set(employee.userId, employee);
// If user ID is available, add employee details to the map
if (employee.userId) {
// Add employee details to the map
employeeMap.set(employee.userId, employee);
}
});

// Merge employee details into each user organization object
const itemsWithEmployees = items.map((organization: UserOrganization) => {
const employee = employeeMap.get(organization.user.id);
return { ...organization, user: { ...organization.user, employee } };
// If user ID is available, fetch employee details
if (organization.user.id) {
// Fetch employee details using the user ID
const employee = employeeMap.get(organization.user.id);
return { ...organization, user: { ...organization.user, employee } };
}
// If user ID is not available, return the original organization object
return { ...organization };
});

// Return paginated result with employee details
Expand Down
9 changes: 2 additions & 7 deletions packages/core/src/lib/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,9 @@ export class UserService extends TenantAwareCrudService<User> {

console.log('findMe found User with Id:', user.id);

// If 'includeEmployee' is set to true, fetch employee details associated with the user.
// Fetch employee details if 'includeEmployee' is true
if (options.includeEmployee) {
const relations: any = {};

// Include organization relation if 'includeOrganization' is true
if (options.includeOrganization) {
relations.organization = true;
}
const relations = options.includeOrganization ? { organization: true } : [];

employee = await this._employeeService.findOneByUserId(user.id, { relations });
}
Expand Down

0 comments on commit ca545bd

Please sign in to comment.