Skip to content

Cleanup #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
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
111 changes: 0 additions & 111 deletions src/controllers/adoptionController.js

This file was deleted.

82 changes: 27 additions & 55 deletions src/controllers/categoriesController.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,52 @@
import { firestore } from '../utils/db.js';
import {
applyArrayFilter,
selectFields,
generateQueryCacheKey,
getCachedQueryResult,
setCachedQueryResult
} from '../utils/controllerHelpers.js';
import { executeQuery, validateArrayParameter } from '../utils/controllerHelpers.js';

/**
* List categories with optional filtering and field selection - Optimized version
* List categories with optional filtering and field selection
*/
const listCategories = async (req, res) => {
try {
const params = req.query;
const queryBuilder = async (params) => {
const isOnlyNames = params.onlyname || typeof params.onlyname === 'string';
const hasCustomFields = params.fields && !isOnlyNames;

// Create cache key for this specific query
const queryFilters = {
category: params.category,
onlyname: isOnlyNames,
fields: params.fields
};
const cacheKey = generateQueryCacheKey('categories', queryFilters);

// Check cache first
const cachedResult = getCachedQueryResult(cacheKey);
if (cachedResult) {
res.statusCode = 200;
res.end(JSON.stringify(cachedResult));
return;
}

let query = firestore.collection('categories').orderBy('category', 'asc');

// Apply category filter using shared utility
query = applyArrayFilter(query, 'category', params.category);
// Apply category filter with validation
if (params.category) {
const categories = validateArrayParameter(params.category, 'category');
if (categories.length > 0) {
query = query.where('category', 'in', categories);
}
}

// Apply field selection
if (isOnlyNames) {
// Only select category field for names-only queries
query = query.select('category');
} else if (hasCustomFields) {
// Select only requested fields
const requestedFields = params.fields.split(',').map(f => f.trim());
query = query.select(...requestedFields);
}

// Execute query
const snapshot = await query.get();
const data = [];
return query;
};

// Process results based on response type
snapshot.forEach(doc => {
const docData = doc.data();
const dataProcessor = (data, params) => {
const isOnlyNames = params.onlyname || typeof params.onlyname === 'string';

if (isOnlyNames) {
data.push(docData.category);
} else {
// Data already filtered by select(), just return it
data.push(docData);
}
});
if (isOnlyNames) {
return data.map(item => item.category);
}

return data;
};

// Cache the result
setCachedQueryResult(cacheKey, data);
// Include onlyname and fields in cache key calculation
const customCacheKeyData = {
onlyname: req.query.onlyname || false,
fields: req.query.fields
};

// Direct response
res.statusCode = 200;
res.end(JSON.stringify(data));
} catch (error) {
console.error('Error fetching categories:', error);
res.statusCode = 500;
res.end(JSON.stringify({
errors: [{ error: 'Failed to fetch categories' }]
}));
}
await executeQuery(req, res, 'categories', queryBuilder, dataProcessor, customCacheKeyData);
};

export { listCategories };
112 changes: 0 additions & 112 deletions src/controllers/cwvtechController.js

This file was deleted.

33 changes: 5 additions & 28 deletions src/controllers/geosController.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
import { firestore } from '../utils/db.js';
import { handleControllerError, generateQueryCacheKey, getCachedQueryResult, setCachedQueryResult } from '../utils/controllerHelpers.js';
import { executeQuery } from '../utils/controllerHelpers.js';

/**
* List all geographic locations from database
*/
const listGeos = async (req, res) => {
try {
// Generate cache key for this query
const cacheKey = generateQueryCacheKey('geos', { orderBy: 'mobile_origins' });
const queryBuilder = async () => {
return firestore.collection('geos').orderBy('mobile_origins', 'desc').select('geo');
};

// Check cache first
const cachedResult = getCachedQueryResult(cacheKey);
if (cachedResult) {
res.statusCode = 200;
res.end(JSON.stringify(cachedResult));
return;
}

const snapshot = await firestore.collection('geos').orderBy('mobile_origins', 'desc').select('geo').get();
const data = [];

// Extract only the 'geo' property from each document
snapshot.forEach(doc => {
data.push(doc.data());
});

// Cache the result
setCachedQueryResult(cacheKey, data);

res.statusCode = 200;
res.end(JSON.stringify(data));
} catch (error) {
handleControllerError(res, error, 'fetching geographic locations');
}
await executeQuery(req, res, 'geos', queryBuilder);
};

export {
Expand Down
Loading
Loading