Skip to content

Commit 19ec7f8

Browse files
refactor: streamline fleet policy retrieval logic in getFleetPolicies function (#1489)
- Simplified the logic for fetching fleet policies by removing unnecessary checks and fallback mechanisms. - Enhanced error handling and logging for better clarity when no devices are found or when fetching fails. - Ensured that only devices associated with the specific fleetDmLabelId are considered for the member, improving the accuracy of device matching. These changes improve the maintainability and performance of the fleet policy retrieval process. Co-authored-by: Mariano Fuentes <[email protected]>
1 parent 3d08d90 commit 19ec7f8

File tree

1 file changed

+14
-104
lines changed
  • apps/app/src/app/(app)/[orgId]/people/[employeeId]

1 file changed

+14
-104
lines changed

apps/app/src/app/(app)/[orgId]/people/[employeeId]/page.tsx

Lines changed: 14 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -172,125 +172,35 @@ const getTrainingVideos = async (employeeId: string) => {
172172

173173
const getFleetPolicies = async (member: Member & { user: User }) => {
174174
const fleet = await getFleetInstance();
175-
const session = await auth.api.getSession({
176-
headers: await headers(),
177-
});
178-
const organizationId = session?.session.activeOrganizationId;
179175

180-
// Try individual member's fleet label first
181-
if (member.fleetDmLabelId) {
176+
// Only show device if the employee has their own specific fleetDmLabelId
177+
if (!member.fleetDmLabelId) {
182178
console.log(
183-
`Found individual fleetDmLabelId: ${member.fleetDmLabelId} for member: ${member.id}, member email: ${member.user?.email}`,
179+
`No individual fleetDmLabelId found for member: ${member.id}, member email: ${member.user?.email}. No device will be shown.`,
184180
);
185-
186-
try {
187-
const deviceResponse = await fleet.get(`/labels/${member.fleetDmLabelId}/hosts`);
188-
const device = deviceResponse.data.hosts?.[0];
189-
190-
if (device) {
191-
const deviceWithPolicies = await fleet.get(`/hosts/${device.id}`);
192-
const fleetPolicies = deviceWithPolicies.data.host.policies;
193-
return { fleetPolicies, device };
194-
}
195-
} catch (error) {
196-
console.log(
197-
`Failed to get device using individual fleet label for member: ${member.id}`,
198-
error,
199-
);
200-
}
201-
}
202-
203-
// Fallback: Use organization fleet label and find device by matching criteria
204-
if (!organizationId) {
205-
console.log('No organizationId available for fallback device lookup');
206181
return { fleetPolicies: [], device: null };
207182
}
208183

209184
try {
210-
const organization = await db.organization.findUnique({
211-
where: { id: organizationId },
212-
});
185+
const deviceResponse = await fleet.get(`/labels/${member.fleetDmLabelId}/hosts`);
186+
const device = deviceResponse.data.hosts?.[0];
213187

214-
if (!organization?.fleetDmLabelId) {
188+
if (!device) {
215189
console.log(
216-
`No organization fleetDmLabelId found for fallback device lookup - member: ${member.id}`,
190+
`No device found for fleetDmLabelId: ${member.fleetDmLabelId} for member: ${member.id}`,
217191
);
218192
return { fleetPolicies: [], device: null };
219193
}
220194

221-
console.log(
222-
`Using organization fleetDmLabelId: ${organization.fleetDmLabelId} as fallback for member: ${member.id}`,
223-
);
224-
225-
// Get all devices from organization
226-
const deviceResponse = await fleet.get(`/labels/${organization.fleetDmLabelId}/hosts`);
227-
const allDevices = deviceResponse.data.hosts || [];
228-
229-
if (allDevices.length === 0) {
230-
console.log('No devices found in organization fleet');
231-
return { fleetPolicies: [], device: null };
232-
}
233-
234-
// Get detailed info for all devices to help match them to the employee
235-
const devicesWithDetails = await Promise.all(
236-
allDevices.map(async (device: any) => {
237-
try {
238-
const deviceDetails = await fleet.get(`/hosts/${device.id}`);
239-
return deviceDetails.data.host;
240-
} catch (error) {
241-
console.log(`Failed to get details for device ${device.id}:`, error);
242-
return null;
243-
}
244-
}),
245-
);
246-
247-
const validDevices = devicesWithDetails.filter(Boolean);
248-
249-
// Try to match device to employee by computer name containing user's name
250-
const userName = member.user.name?.toLowerCase();
251-
const userEmail = member.user.email?.toLowerCase();
252-
253-
let matchedDevice = null;
254-
255-
if (userName) {
256-
// Try to find device with computer name containing user's name
257-
matchedDevice = validDevices.find(
258-
(device: any) =>
259-
device.computer_name?.toLowerCase().includes(userName.split(' ')[0]) ||
260-
device.computer_name?.toLowerCase().includes(userName.split(' ').pop()),
261-
);
262-
}
263-
264-
if (!matchedDevice && userEmail) {
265-
// Try to find device with computer name containing part of email
266-
const emailPrefix = userEmail.split('@')[0];
267-
matchedDevice = validDevices.find((device: any) =>
268-
device.computer_name?.toLowerCase().includes(emailPrefix),
269-
);
270-
}
195+
const deviceWithPolicies = await fleet.get(`/hosts/${device.id}`);
196+
const fleetPolicies = deviceWithPolicies.data.host.policies || [];
271197

272-
// If no specific match found and there's only one device, assume it's theirs
273-
if (!matchedDevice && validDevices.length === 1) {
274-
matchedDevice = validDevices[0];
275-
console.log(`Only one device found, assigning to member: ${member.id}`);
276-
}
277-
278-
if (matchedDevice) {
279-
console.log(
280-
`Matched device ${matchedDevice.computer_name} (ID: ${matchedDevice.id}) to member: ${member.id}`,
281-
);
282-
return {
283-
fleetPolicies: matchedDevice.policies || [],
284-
device: matchedDevice,
285-
};
286-
}
287-
288-
console.log(
289-
`No device could be matched to member: ${member.id}. Available devices: ${validDevices.map((d: any) => d.computer_name).join(', ')}`,
290-
);
291-
return { fleetPolicies: [], device: null };
198+
return { fleetPolicies, device: deviceWithPolicies.data.host };
292199
} catch (error) {
293-
console.error(`Failed to get fleet policies using fallback for member: ${member.id}`, error);
200+
console.error(
201+
`Failed to get device using individual fleet label for member: ${member.id}`,
202+
error,
203+
);
294204
return { fleetPolicies: [], device: null };
295205
}
296206
};

0 commit comments

Comments
 (0)