ZYT3JRERMYXLMJHLPZYQHAINVMPQLBKGGN7A4C7OTVZDY42ZLTKQC
// First, verify that the user owns the dataset
console.log("Checking dataset ownership for datasetId:", datasetId);
const datasetCheck = await db.select()
.from(dataset)
.where(eq(dataset.id, datasetId))
.limit(1);
console.log("Dataset check result:", datasetCheck);
if (datasetCheck.length === 0) {
console.log("Dataset not found:", datasetId);
return c.json({
error: "Dataset not found"
}, 404);
}
if (datasetCheck[0].owner !== userId) {
console.log("User doesn't own dataset. Owner:", datasetCheck[0].owner, "User:", userId);
return c.json({
error: "You don't have permission to access this dataset"
}, 403);
}
return c.json({
data: results
});
} catch (error) {
console.error("Error fetching locations:", error);
return c.json(
{
error: "Failed to fetch locations",
details: error instanceof Error ? error.message : String(error),
},
500
);
}
});
// locations API route
app.get("/api/clusters", authenticate, async (c) => {
try {
// Get the JWT payload (user info)
const jwtPayload = (c as any).jwtPayload as JWTPayload;
const userId = jwtPayload.sub; // Subject claim usually contains the user ID
// Get the cluster ID from query parameter
const locationId = c.req.query("locationId");
console.log("Cluster API called with locationId:", locationId, "userId:", userId);
if (!locationId) {
console.log("Missing locationId in request");
return c.json({
error: "Missing required query parameter: locationId"
}, 400);
}
// Connect to the database
const sql = neon(c.env.DATABASE_URL);
const db = drizzle(sql);
// Query clustears for the specified location
console.log("Querying clusters for locationId:", locationId);
const results = await db.select()
.from(cluster)
.where(eq(cluster.locationId, locationId));
console.log("Found", results.length, "cluster for location", locationId);
}, (table) => ({
nameIdx: index("idx_dataset_name").on(table.name),
ownerIdx: index("idx_dataset_owner").on(table.owner),
activeIdx: index("idx_dataset_active").on(table.active),
publicIdx: index("idx_dataset_public").on(table.public),
}));
}, (table) => ([
index("idx_dataset_name").on(table.name),
index("idx_dataset_owner").on(table.owner),
index("idx_dataset_active").on(table.active),
index("idx_dataset_public").on(table.public),
]));
}, (table) => ({
nameIdx: index("idx_location_name").on(table.name),
datasetIdx: index("idx_location_dataset").on(table.datasetId),
activeIdx: index("idx_location_active").on(table.active),
datasetActiveIdx: index("idx_location_dataset_active").on(table.datasetId, table.active),
}));
}, (table) => ([
index("idx_location_name").on(table.name),
index("idx_location_dataset").on(table.datasetId),
index("idx_location_active").on(table.active),
index("idx_location_dataset_active").on(table.datasetId, table.active),
]));
}, (table) => ({
datasetIdx: index("idx_cluster_dataset").on(table.datasetId),
activeIdx: index("idx_cluster_active").on(table.active),
datasetActiveIdx: index("idx_cluster_dataset_active").on(table.datasetId, table.active),
recordingPatternIdx: index("idx_cluster_recording_pattern").on(table.cyclicRecordingPatternId),
}));
}, (table) => ([
index("idx_cluster_dataset").on(table.datasetId),
index("idx_cluster_active").on(table.active),
index("idx_cluster_dataset_active").on(table.datasetId, table.active),
index("idx_cluster_recording_pattern").on(table.cyclicRecordingPatternId),
]));
}, (table) => ({
locationIdx: index("idx_file_location").on(table.locationId),
activeIdx: index("idx_file_active").on(table.active),
timestampLocalIdx: index("idx_file_timestamp_local").on(table.timestampLocal),
clusterIdx: index("idx_file_cluster").on(table.clusterId),
maybeSolarNightIdx: index("idx_file_maybe_solar_night").on(table.maybeSolarNight),
maybeCivilNightIdx: index("idx_file_maybe_civil_night").on(table.maybeCivilNight),
}));
}, (table) => ([
index("idx_file_location").on(table.locationId),
index("idx_file_active").on(table.active),
index("idx_file_timestamp_local").on(table.timestampLocal),
index("idx_file_cluster").on(table.clusterId),
index("idx_file_maybe_solar_night").on(table.maybeSolarNight),
index("idx_file_maybe_civil_night").on(table.maybeCivilNight),
]));
}, (table) => ({
pk: primaryKey({columns: [table.fileId, table.datasetId]}),
fileIdx: index("idx_file_dataset_file").on(table.fileId),
datasetIdx: index("idx_file_dataset_dataset").on(table.datasetId),
}));
}, (table) => ([
primaryKey({columns: [table.fileId, table.datasetId]}),
index("idx_file_dataset_file").on(table.fileId),
index("idx_file_dataset_dataset").on(table.datasetId),
]));
}, (table) => ({
fileIdx: index("idx_selection_file").on(table.fileId),
datasetIdx: index("idx_selection_dataset").on(table.datasetId),
activeIdx: index("idx_selection_active").on(table.active),
approvedIdx: index("idx_selection_approved").on(table.approved),
isSolarNightIdx: index("idx_selection_is_solar_night").on(table.isSolarNight),
isCivilNightIdx: index("idx_selection_is_civil_night").on(table.isCivilNight),
moonPhaseIdx: index("idx_selection_moon_phase").on(table.moonPhase),
}, (table) => ([
index("idx_selection_file").on(table.fileId),
index("idx_selection_dataset").on(table.datasetId),
index("idx_selection_active").on(table.active),
index("idx_selection_approved").on(table.approved),
index("idx_selection_is_solar_night").on(table.isSolarNight),
index("idx_selection_is_civil_night").on(table.isCivilNight),
index("idx_selection_moon_phase").on(table.moonPhase),
}, (table) => ({
speciesIdx: index("idx_call_type_species").on(table.speciesId),
labelIdx: index("idx_call_type_label").on(table.label),
}));
}, (table) => ([
index("idx_call_type_species").on(table.speciesId),
index("idx_call_type_label").on(table.label),
]));
}, (table) => ({
selectionIdIdx: index("idx_label_selection_id").on(table.selectionId),
speciesIdIdx: index("idx_label_species_id").on(table.speciesId),
}));
}, (table) => ([
index("idx_label_selection_id").on(table.selectionId),
index("idx_label_species_id").on(table.speciesId),
]));
}, (table) => ({
pk: primaryKey({columns: [table.speciesId, table.datasetId]}),
speciesIdx: index("idx_species_dataset_species").on(table.speciesId),
datasetIdx: index("idx_species_dataset_dataset").on(table.datasetId),
}));
}, (table) => ([
primaryKey({columns: [table.speciesId, table.datasetId]}),
index("idx_species_dataset_species").on(table.speciesId),
index("idx_species_dataset_dataset").on(table.datasetId),
]));
}, (table) => ({
uniqueAccessGrant: unique().on(table.datasetId, table.role, table.permission, table.userId),
uniqueRolePermission: uniqueIndex("idx_unique_role_permission")
}, (table) => ([
unique().on(table.datasetId, table.role, table.permission, table.userId),
uniqueIndex("idx_unique_role_permission")
datasetIdx: index("idx_access_grant_dataset").on(table.datasetId),
roleIdx: index("idx_access_grant_role").on(table.role),
permissionIdx: index("idx_access_grant_permission").on(table.permission),
userIdx: index("idx_access_grant_user").on(table.userId),
activeIdx: index("idx_access_grant_active").on(table.active),
}));
index("idx_access_grant_dataset").on(table.datasetId),
index("idx_access_grant_role").on(table.role),
index("idx_access_grant_permission").on(table.permission),
index("idx_access_grant_user").on(table.userId),
index("idx_access_grant_active").on(table.active),
]));
-- Need an index on cluster(location) because I need to get all clusters for any given location