Spaces:
Paused
Paused
File size: 6,161 Bytes
8e71870 a18f585 8e71870 3555971 8e71870 3555971 8e71870 3555971 8e71870 3555971 a18f585 3555971 a18f585 8e71870 3555971 8e71870 3555971 8e71870 137905c 8e71870 abdeed6 8e71870 137905c 8e71870 abdeed6 | 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /**
* Provider Controller - Admin Oversight of Service Providers (PostgreSQL)
*/
const { getPool } = require('../db');
const { eventBus, EVENTS } = require('../services/eventBus');
/**
* List all providers with filters
*/
async function list(req, res) {
const { provider_type, verification_status, district, limit = 50, offset = 0 } = req.query;
const pool = getPool();
if (!pool) {
console.error('[Provider List Error] No database pool available');
return res.status(500).json({ error: 'Database not connected' });
}
try {
// Explicit selection to avoid ambiguity and handle missing location data
let query = `
SELECT
p.id,
p.user_id,
p.provider_type,
p.bio,
p.verification_status,
p.created_at,
u.name as user_name,
u.email as user_email,
COALESCE(up.city, 'Jhargram') as district
FROM provider_profiles p
INNER JOIN users u ON p.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
WHERE 1=1
`;
const params = [];
let paramCounter = 1;
if (provider_type) {
query += ` AND p.provider_type = $${paramCounter++}`;
params.push(provider_type);
}
if (verification_status) {
query += ` AND p.verification_status = $${paramCounter++}`;
params.push(verification_status);
}
if (district) {
// Filter by city in profile or mention in bio
query += ` AND (up.city ILIKE $${paramCounter++} OR p.bio ILIKE $${paramCounter++})`;
params.push(`%${district}%`, `%${district}%`);
}
query += ` ORDER BY p.created_at DESC LIMIT $${paramCounter++} OFFSET $${paramCounter++}`;
params.push(parseInt(limit), parseInt(offset));
const result = await pool.query(query, params);
res.json({
success: true,
count: result.rows.length,
data: result.rows
});
} catch (error) {
console.error('[Provider List Query Error]', {
message: error.message,
stack: error.stack,
params: { provider_type, verification_status, district, limit, offset }
});
res.status(500).json({
success: false,
error: 'Internal Server Error',
message: error.message
});
}
}
/**
* Verify or Update Provider Status
*/
async function verify(req, res) {
const { id } = req.params;
const { verification_status, comments } = req.body;
const pool = getPool();
if (!pool) return res.status(500).json({ error: 'Database not connected' });
try {
const query = 'UPDATE provider_profiles SET verification_status = $2, updated_at = NOW() WHERE id = $1';
await pool.query(query, [id, verification_status]);
// Emit event
if (verification_status === 'verified') {
eventBus.emit(EVENTS.PROVIDER_VERIFIED, { provider_id: id });
}
res.json({ success: true, message: `Provider status updated to ${verification_status}` });
} catch (error) {
console.error('[Provider Verify Error]', error.message);
res.status(500).json({ error: 'Failed to update provider status' });
}
}
/**
* Create Provider Profile (admin)
*/
async function create(req, res) {
const { user_id, provider_type, bio } = req.body;
const pool = getPool();
if (!pool) return res.status(500).json({ error: 'Database not connected' });
if (!user_id || !provider_type) {
return res.status(400).json({ error: 'user_id and provider_type are required' });
}
try {
const result = await pool.query(
`INSERT INTO provider_profiles (user_id, provider_type, bio, verification_status)
VALUES ($1, $2, $3, 'pending')
RETURNING *`,
[user_id, provider_type, bio || '']
);
res.status(201).json({ success: true, data: result.rows[0] });
} catch (error) {
console.error('[Provider Create Error]', error.message);
res.status(500).json({ error: 'Failed to create provider' });
}
}
/**
* Update Provider Profile
*/
async function update(req, res) {
const { id } = req.params;
const { provider_type, bio, verification_status } = req.body;
const pool = getPool();
if (!pool) return res.status(500).json({ error: 'Database not connected' });
try {
const updates = [];
const params = [id];
let paramCounter = 2;
if (provider_type !== undefined) {
updates.push(`provider_type = $${paramCounter++}`);
params.push(provider_type);
}
if (bio !== undefined) {
updates.push(`bio = $${paramCounter++}`);
params.push(bio);
}
if (verification_status !== undefined) {
const validStatuses = ['pending', 'verified', 'rejected'];
if (!validStatuses.includes(verification_status)) {
return res.status(400).json({ error: `Invalid verification_status. Must be one of: ${validStatuses.join(', ')}` });
}
updates.push(`verification_status = $${paramCounter++}`);
params.push(verification_status);
}
if (updates.length === 0) {
return res.status(400).json({ error: 'No fields to update' });
}
updates.push('updated_at = NOW()');
const query = `UPDATE provider_profiles SET ${updates.join(', ')} WHERE id = $1 RETURNING *`;
const result = await pool.query(query, params);
if (result.rows.length === 0) {
return res.status(404).json({ error: 'Provider not found' });
}
res.json({ success: true, data: result.rows[0] });
} catch (error) {
console.error('[Provider Update Error]', error.message);
res.status(500).json({ error: 'Failed to update provider' });
}
}
/**
* Delete Provider Profile
*/
async function remove(req, res) {
const { id } = req.params;
const pool = getPool();
if (!pool) return res.status(500).json({ error: 'Database not connected' });
try {
await pool.query('DELETE FROM provider_profiles WHERE id = $1', [id]);
res.json({ success: true, message: 'Provider deleted' });
} catch (error) {
console.error('[Provider Delete Error]', error.message);
res.status(500).json({ error: 'Failed to delete provider' });
}
}
module.exports = { list, verify, create, update, remove };
|