fix(rbac): add migration to fix seeded users roles and patch notifications store
This commit is contained in:
parent
9b389e1c9f
commit
617bb5ab39
4 changed files with 37 additions and 6 deletions
|
|
@ -46,10 +46,10 @@ func main() {
|
||||||
|
|
||||||
// Try multiple paths
|
// Try multiple paths
|
||||||
paths := []string{
|
paths := []string{
|
||||||
"migrations/017_create_tickets_table.sql",
|
"migrations/023_ensure_seeded_admins_roles.sql",
|
||||||
"backend/migrations/017_create_tickets_table.sql",
|
"backend/migrations/023_ensure_seeded_admins_roles.sql",
|
||||||
"../migrations/017_create_tickets_table.sql",
|
"../migrations/023_ensure_seeded_admins_roles.sql",
|
||||||
"/home/yamamoto/lab/gohorsejobs/backend/migrations/017_create_tickets_table.sql",
|
"/home/yamamoto/lab/gohorsejobs/backend/migrations/023_ensure_seeded_admins_roles.sql",
|
||||||
}
|
}
|
||||||
|
|
||||||
var content []byte
|
var content []byte
|
||||||
|
|
|
||||||
20
backend/migrations/023_ensure_seeded_admins_roles.sql
Normal file
20
backend/migrations/023_ensure_seeded_admins_roles.sql
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
-- Migration: Ensure Seeded Users Have Roles
|
||||||
|
-- Description: Fixes missing roles for seeded users (admins, recruiters, candidates) due to previous seeder/migration race conditions.
|
||||||
|
|
||||||
|
-- 1. Fix Admins
|
||||||
|
INSERT INTO user_roles (user_id, role)
|
||||||
|
SELECT id, 'admin' FROM users WHERE identifier IN ('takeshi_yamamoto', 'kenji', 'wile_e_coyote')
|
||||||
|
ON CONFLICT (user_id, role) DO NOTHING;
|
||||||
|
|
||||||
|
-- 2. Fix Recruiters
|
||||||
|
INSERT INTO user_roles (user_id, role)
|
||||||
|
SELECT id, 'recruiter' FROM users WHERE identifier = 'maria_santos'
|
||||||
|
ON CONFLICT (user_id, role) DO NOTHING;
|
||||||
|
|
||||||
|
-- 3. Fix Candidates (including legacy ones)
|
||||||
|
INSERT INTO user_roles (user_id, role)
|
||||||
|
SELECT id, 'candidate' FROM users WHERE identifier IN (
|
||||||
|
'paulo_santos', 'maria_email',
|
||||||
|
'ana_silva', 'carlos_santos', 'maria_oliveira', 'pedro_costa', 'juliana_ferreira'
|
||||||
|
)
|
||||||
|
ON CONFLICT (user_id, role) DO NOTHING;
|
||||||
|
|
@ -21,8 +21,8 @@ export const useNotificationsStore = create<NotificationsState>((set, get) => ({
|
||||||
try {
|
try {
|
||||||
const data = await notificationsApi.list();
|
const data = await notificationsApi.list();
|
||||||
set({
|
set({
|
||||||
notifications: data,
|
notifications: data || [],
|
||||||
unreadCount: data.filter((n) => !n.readAt).length,
|
unreadCount: (data || []).filter((n) => !n.readAt).length,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch notifications", error);
|
console.error("Failed to fetch notifications", error);
|
||||||
|
|
|
||||||
|
|
@ -190,6 +190,17 @@ export async function seedUsers() {
|
||||||
cand.bio
|
cand.bio
|
||||||
]);
|
]);
|
||||||
console.log(` ✓ Legacy candidate created: ${cand.email}`);
|
console.log(` ✓ Legacy candidate created: ${cand.email}`);
|
||||||
|
|
||||||
|
// Fix: Insert role into user_roles
|
||||||
|
const result = await pool.query('SELECT id FROM users WHERE identifier = $1', [cand.identifier]);
|
||||||
|
if (result.rows[0]) {
|
||||||
|
const userId = result.rows[0].id;
|
||||||
|
await pool.query(`
|
||||||
|
INSERT INTO user_roles (user_id, role)
|
||||||
|
VALUES ($1, 'candidate')
|
||||||
|
ON CONFLICT (user_id, role) DO NOTHING
|
||||||
|
`, [userId]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue