This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrants-fix.ts
More file actions
105 lines (93 loc) · 3.21 KB
/
Copy pathgrants-fix.ts
File metadata and controls
105 lines (93 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Direct SQL implementation for grant sources API
* This bypasses the schema mismatch between code and database
*/
import express from 'express';
import { db } from '../db';
import { sql } from 'drizzle-orm';
import { apiResponse } from './index';
const router = express.Router();
// Get all grant sources
router.get('/api/grants-fixed', async (req, res) => {
try {
// Direct SQL implementation to fix schema mismatch
const results = await db.execute(sql`
SELECT
id,
name,
description,
website,
categories,
available_funds as "availableFunds",
application_url as "applicationUrl",
contact_email as "contactEmail",
application_deadline as "applicationDeadline",
is_active as "isActive",
created_at as "createdAt",
updated_at as "updatedAt"
FROM grant_sources
`);
const sources = results.rows || [];
// Filter by category if provided
const category = req.query.category as string;
if (category && sources.length > 0) {
const filteredSources = sources.filter(source =>
source.categories && Array.isArray(source.categories) && source.categories.includes(category)
);
return res.json(apiResponse(true, filteredSources));
}
return res.json(apiResponse(true, sources));
} catch (error) {
console.error('Error in fixed grant sources endpoint:', error);
return res.status(500).json(apiResponse(false, null, 'Failed to fetch grant sources'));
}
});
// Get specific grant source by ID
router.get('/api/grants-fixed/:id', async (req, res) => {
try {
const sourceId = parseInt(req.params.id);
if (isNaN(sourceId)) {
return res.status(400).json(apiResponse(false, null, 'Invalid grant source ID'));
}
const results = await db.execute(sql`
SELECT
id,
name,
description,
website,
categories,
available_funds as "availableFunds",
application_url as "applicationUrl",
contact_email as "contactEmail",
application_deadline as "applicationDeadline",
is_active as "isActive",
created_at as "createdAt",
updated_at as "updatedAt"
FROM grant_sources
WHERE id = ${sourceId}
`);
if (!results.rows || results.rows.length === 0) {
return res.status(404).json(apiResponse(false, null, 'Grant source not found'));
}
return res.json(apiResponse(true, results.rows[0]));
} catch (error) {
console.error('Error fetching grant source:', error);
return res.status(500).json(apiResponse(false, null, 'Failed to fetch grant source'));
}
});
// Get available categories
router.get('/api/grants-fixed/categories', async (req, res) => {
try {
const results = await db.execute(sql`
SELECT DISTINCT UNNEST(categories) as category
FROM grant_sources
WHERE categories IS NOT NULL
ORDER BY category
`);
return res.json(apiResponse(true, results.rows.map(row => row.category)));
} catch (error) {
console.error('Error fetching grant categories:', error);
return res.status(500).json(apiResponse(false, null, 'Failed to fetch grant categories'));
}
});
export default router;