because I got bored of customising my CV for every job
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(scripts): add organization role randomization script

- Create script to add additional organization roles (Lead, Senior Member, Moderator, Contributor, Intern)
- Randomize existing user-organization role assignments
- Support for role distribution analysis and reporting

+119
+119
apps/server/src/scripts/randomize-roles.ts
··· 1 + import { PrismaClient } from "@prisma/client"; 2 + 3 + const prisma = new PrismaClient(); 4 + 5 + const additionalRoles = [ 6 + { id: 'lead_role_id', name: 'Lead', description: 'Team lead with management responsibilities' }, 7 + { id: 'senior_role_id', name: 'Senior Member', description: 'Senior member with additional privileges' }, 8 + { id: 'moderator_role_id', name: 'Moderator', description: 'Content and community moderator' }, 9 + { id: 'contributor_role_id', name: 'Contributor', description: 'Active contributor to projects' }, 10 + { id: 'intern_role_id', name: 'Intern', description: 'Intern with limited access' }, 11 + ]; 12 + 13 + const allRoles = [ 14 + 'member_role_id', 15 + 'admin_role_id', 16 + 'guest_role_id', 17 + 'lead_role_id', 18 + 'senior_role_id', 19 + 'moderator_role_id', 20 + 'contributor_role_id', 21 + 'intern_role_id', 22 + ]; 23 + 24 + async function addAdditionalRoles() { 25 + console.log('Adding additional organization roles...'); 26 + 27 + for (const role of additionalRoles) { 28 + try { 29 + await prisma.organizationRole.upsert({ 30 + where: { id: role.id }, 31 + update: role, 32 + create: role, 33 + }); 34 + console.log(`✅ Added role: ${role.name}`); 35 + } catch (error) { 36 + console.log(`❌ Error adding role ${role.name}:`, error); 37 + } 38 + } 39 + } 40 + 41 + async function randomizeUserRoles() { 42 + console.log('\nRandomizing user roles in organizations...'); 43 + 44 + // Get all user organizations 45 + const userOrganizations = await prisma.userOrganization.findMany({ 46 + include: { 47 + user: true, 48 + organization: true, 49 + }, 50 + }); 51 + 52 + console.log(`Found ${userOrganizations.length} user-organization relationships`); 53 + 54 + // Randomize roles for each user-organization relationship 55 + for (const userOrg of userOrganizations) { 56 + // Get a random role (weighted towards member/contributor roles) 57 + const roleWeights = { 58 + 'member_role_id': 30, 59 + 'contributor_role_id': 25, 60 + 'senior_role_id': 15, 61 + 'lead_role_id': 10, 62 + 'moderator_role_id': 10, 63 + 'admin_role_id': 5, 64 + 'guest_role_id': 3, 65 + 'intern_role_id': 2, 66 + }; 67 + 68 + const weightedRoles = Object.entries(roleWeights).flatMap(([roleId, weight]) => 69 + Array(weight).fill(roleId) 70 + ); 71 + 72 + const randomRoleId = weightedRoles[Math.floor(Math.random() * weightedRoles.length)]; 73 + 74 + try { 75 + await prisma.userOrganization.update({ 76 + where: { id: userOrg.id }, 77 + data: { organizationRoleId: randomRoleId }, 78 + }); 79 + 80 + console.log(`✅ Updated ${userOrg.user.name} in ${userOrg.organization.name} to role: ${randomRoleId}`); 81 + } catch (error) { 82 + console.log(`❌ Error updating role for ${userOrg.user.name}:`, error); 83 + } 84 + } 85 + } 86 + 87 + async function showRoleDistribution() { 88 + console.log('\n📊 Current role distribution:'); 89 + 90 + const roleStats = await prisma.userOrganization.groupBy({ 91 + by: ['organizationRoleId'], 92 + _count: { 93 + organizationRoleId: true, 94 + }, 95 + }); 96 + 97 + for (const stat of roleStats) { 98 + const role = await prisma.organizationRole.findUnique({ 99 + where: { id: stat.organizationRoleId }, 100 + }); 101 + console.log(` ${role?.name || stat.organizationRoleId}: ${stat._count.organizationRoleId} users`); 102 + } 103 + } 104 + 105 + async function main() { 106 + try { 107 + await addAdditionalRoles(); 108 + await randomizeUserRoles(); 109 + await showRoleDistribution(); 110 + 111 + console.log('\n🎉 Role randomization completed!'); 112 + } catch (error) { 113 + console.error('❌ Error during role randomization:', error); 114 + } finally { 115 + await prisma.$disconnect(); 116 + } 117 + } 118 + 119 + main();