B:BD[
4.5512] → [
4.5512:6896]
// For each cluster, fetch its cyclic recording pattern if it has one
const enrichedResults = await Promise.all(
clusterResults.map(async (clusterData) => {
if (clusterData.cyclicRecordingPatternId) {
try {
// Fetch the recording pattern
const recordingPattern = await db.select({
recordS: cyclicRecordingPattern.recordS,
sleepS: cyclicRecordingPattern.sleepS
})
.from(cyclicRecordingPattern)
.where(eq(cyclicRecordingPattern.id, clusterData.cyclicRecordingPatternId))
.then(results => results[0]);
// Add the recording pattern details to the cluster data
return {
...clusterData,
recordingPattern: recordingPattern || null
};
} catch (patternError) {
console.error("Error fetching recording pattern for cluster:", clusterData.id, patternError);
// Return cluster without pattern data if there was an error
return {
...clusterData,
recordingPattern: null
};
}
} else {
// Return cluster without pattern data if it doesn't have a pattern ID
return {
...clusterData,
recordingPattern: null
};
}
})
);
// Transform results to match the expected structure
const enrichedResults = joinedResults.map(row => ({
id: row.id,
datasetId: row.datasetId,
locationId: row.locationId,
name: row.name,
description: row.description,
createdBy: row.createdBy,
createdAt: row.createdAt,
lastModified: row.lastModified,
modifiedBy: row.modifiedBy,
active: row.active,
timezoneId: row.timezoneId,
cyclicRecordingPatternId: row.cyclicRecordingPatternId,
sampleRate: row.sampleRate,
recordingPattern: (row.recordS !== null && row.sleepS !== null) ? {
recordS: row.recordS,
sleepS: row.sleepS
} : null
}));