Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 14 additions & 104 deletions apps/app/src/app/(app)/[orgId]/people/[employeeId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,125 +172,35 @@ const getTrainingVideos = async (employeeId: string) => {

const getFleetPolicies = async (member: Member & { user: User }) => {
const fleet = await getFleetInstance();
const session = await auth.api.getSession({
headers: await headers(),
});
const organizationId = session?.session.activeOrganizationId;

// Try individual member's fleet label first
if (member.fleetDmLabelId) {
// Only show device if the employee has their own specific fleetDmLabelId
if (!member.fleetDmLabelId) {
console.log(
`Found individual fleetDmLabelId: ${member.fleetDmLabelId} for member: ${member.id}, member email: ${member.user?.email}`,
`No individual fleetDmLabelId found for member: ${member.id}, member email: ${member.user?.email}. No device will be shown.`,
);

try {
const deviceResponse = await fleet.get(`/labels/${member.fleetDmLabelId}/hosts`);
const device = deviceResponse.data.hosts?.[0];

if (device) {
const deviceWithPolicies = await fleet.get(`/hosts/${device.id}`);
const fleetPolicies = deviceWithPolicies.data.host.policies;
return { fleetPolicies, device };
}
} catch (error) {
console.log(
`Failed to get device using individual fleet label for member: ${member.id}`,
error,
);
}
}

// Fallback: Use organization fleet label and find device by matching criteria
if (!organizationId) {
console.log('No organizationId available for fallback device lookup');
return { fleetPolicies: [], device: null };
}

try {
const organization = await db.organization.findUnique({
where: { id: organizationId },
});
const deviceResponse = await fleet.get(`/labels/${member.fleetDmLabelId}/hosts`);
const device = deviceResponse.data.hosts?.[0];

if (!organization?.fleetDmLabelId) {
if (!device) {
console.log(
`No organization fleetDmLabelId found for fallback device lookup - member: ${member.id}`,
`No device found for fleetDmLabelId: ${member.fleetDmLabelId} for member: ${member.id}`,
);
return { fleetPolicies: [], device: null };
}

console.log(
`Using organization fleetDmLabelId: ${organization.fleetDmLabelId} as fallback for member: ${member.id}`,
);

// Get all devices from organization
const deviceResponse = await fleet.get(`/labels/${organization.fleetDmLabelId}/hosts`);
const allDevices = deviceResponse.data.hosts || [];

if (allDevices.length === 0) {
console.log('No devices found in organization fleet');
return { fleetPolicies: [], device: null };
}

// Get detailed info for all devices to help match them to the employee
const devicesWithDetails = await Promise.all(
allDevices.map(async (device: any) => {
try {
const deviceDetails = await fleet.get(`/hosts/${device.id}`);
return deviceDetails.data.host;
} catch (error) {
console.log(`Failed to get details for device ${device.id}:`, error);
return null;
}
}),
);

const validDevices = devicesWithDetails.filter(Boolean);

// Try to match device to employee by computer name containing user's name
const userName = member.user.name?.toLowerCase();
const userEmail = member.user.email?.toLowerCase();

let matchedDevice = null;

if (userName) {
// Try to find device with computer name containing user's name
matchedDevice = validDevices.find(
(device: any) =>
device.computer_name?.toLowerCase().includes(userName.split(' ')[0]) ||
device.computer_name?.toLowerCase().includes(userName.split(' ').pop()),
);
}

if (!matchedDevice && userEmail) {
// Try to find device with computer name containing part of email
const emailPrefix = userEmail.split('@')[0];
matchedDevice = validDevices.find((device: any) =>
device.computer_name?.toLowerCase().includes(emailPrefix),
);
}
const deviceWithPolicies = await fleet.get(`/hosts/${device.id}`);
const fleetPolicies = deviceWithPolicies.data.host.policies || [];

// If no specific match found and there's only one device, assume it's theirs
if (!matchedDevice && validDevices.length === 1) {
matchedDevice = validDevices[0];
console.log(`Only one device found, assigning to member: ${member.id}`);
}

if (matchedDevice) {
console.log(
`Matched device ${matchedDevice.computer_name} (ID: ${matchedDevice.id}) to member: ${member.id}`,
);
return {
fleetPolicies: matchedDevice.policies || [],
device: matchedDevice,
};
}

console.log(
`No device could be matched to member: ${member.id}. Available devices: ${validDevices.map((d: any) => d.computer_name).join(', ')}`,
);
return { fleetPolicies: [], device: null };
return { fleetPolicies, device: deviceWithPolicies.data.host };
} catch (error) {
console.error(`Failed to get fleet policies using fallback for member: ${member.id}`, error);
console.error(
`Failed to get device using individual fleet label for member: ${member.id}`,
error,
);
return { fleetPolicies: [], device: null };
}
};
Loading