@@ -4,8 +4,8 @@ import { OrganizationOutput, OrganizationProcess } from './organization.process.
44import { UsersOutput , UsersProcess } from './user.process.js' ;
55import { seedTeamConfigs , teamCreateInput } from '../factories/teams.factory.js' ;
66import { SeedProcess } from '../processes/seed-process.js' ;
7+ import type { FullUser } from '../context.js' ;
78
8- const EXPECTED_TEAM_COUNT = 20 ;
99const MIN_LEADS_PER_TEAM = 1 ;
1010const MAX_LEADS_PER_TEAM = 3 ;
1111const MIN_MEMBERS_PER_TEAM = 8 ;
@@ -25,10 +25,6 @@ export class TeamProcess extends SeedProcess<TeamInput, TeamOutput> {
2525 }
2626
2727 async run ( { organization, admins, heads, leadership, members, teamTypes } : TeamInput ) : Promise < TeamOutput > {
28- if ( seedTeamConfigs . length !== EXPECTED_TEAM_COUNT ) {
29- throw new Error ( `TeamProcess expected ${ EXPECTED_TEAM_COUNT } teams but found ${ seedTeamConfigs . length } .` ) ;
30- }
31-
3228 if ( admins . length === 0 || heads . length === 0 || leadership . length === 0 || members . length === 0 ) {
3329 throw new Error ( 'TeamProcess requires admins, heads, leadership, and members to create teams.' ) ;
3430 }
@@ -44,40 +40,57 @@ export class TeamProcess extends SeedProcess<TeamInput, TeamOutput> {
4440 return acc ;
4541 } , { } ) ;
4642
47- const possibleHeads = [ ...heads , ...admins , ...leadership ] ;
48- const possibleLeads = [ ...leadership , ...heads , ...admins ] ;
49-
50- const teams = await Promise . all (
51- seedTeamConfigs . map ( ( config , index ) => {
52- const head = possibleHeads [ index % possibleHeads . length ] ;
53-
54- if ( ! head ) {
55- throw new Error ( 'TeamProcess could not find a head for a team.' ) ;
56- }
57-
58- const leadPool = possibleLeads . filter ( ( user ) => user . userId !== head . userId ) ;
59-
60- const leads = this . faker . helpers . arrayElements (
61- leadPool ,
62- this . faker . number . int ( {
63- min : MIN_LEADS_PER_TEAM ,
64- max : MAX_LEADS_PER_TEAM
65- } )
66- ) ;
67-
68- const teamMembers = this . faker . helpers . arrayElements (
69- members ,
70- this . faker . number . int ( {
71- min : MIN_MEMBERS_PER_TEAM ,
72- max : MAX_MEMBERS_PER_TEAM
73- } )
74- ) ;
75-
76- return this . prisma . team . create ( {
77- data : teamCreateInput ( this . faker , organization . organizationId , head , leads , teamMembers , teamTypesByName , config )
78- } ) ;
79- } )
80- ) ;
43+ const leadershipCandidates = [ ...heads , ...admins , ...leadership ] ;
44+
45+ if ( leadershipCandidates . length < seedTeamConfigs . length ) {
46+ throw new Error (
47+ `Not enough head candidates (${ leadershipCandidates . length } ) for ${ seedTeamConfigs . length } teams.`
48+ ) ;
49+ }
50+
51+ const usedLeadIds = new Set < string > ( ) ;
52+
53+ const getLeadsForTeam = ( head : FullUser ) : FullUser [ ] => {
54+ const unusedLeadPool = leadershipCandidates . filter (
55+ ( user ) => user . userId !== head . userId && ! usedLeadIds . has ( user . userId )
56+ ) ;
57+
58+ const fallbackLeadPool = leadershipCandidates . filter ( ( user ) => user . userId !== head . userId ) ;
59+ const leadPool = unusedLeadPool . length >= MIN_LEADS_PER_TEAM ? unusedLeadPool : fallbackLeadPool ;
60+
61+ if ( leadPool . length < MIN_LEADS_PER_TEAM ) {
62+ throw new Error ( 'TeamProcess could not find enough leads for a team.' ) ;
63+ }
64+
65+ const leads = this . faker . helpers . arrayElements (
66+ leadPool ,
67+ this . faker . number . int ( {
68+ min : MIN_LEADS_PER_TEAM ,
69+ max : Math . min ( MAX_LEADS_PER_TEAM , leadPool . length )
70+ } )
71+ ) ;
72+
73+ leads . forEach ( ( lead ) => usedLeadIds . add ( lead . userId ) ) ;
74+
75+ return leads ;
76+ } ;
77+
78+ const teamCreateInputs = seedTeamConfigs . map ( ( config , index ) => {
79+ const head = leadershipCandidates [ index ] ;
80+ const leads = getLeadsForTeam ( head ) ;
81+
82+ const teamMembers = this . faker . helpers . arrayElements (
83+ members ,
84+ this . faker . number . int ( {
85+ min : MIN_MEMBERS_PER_TEAM ,
86+ max : MAX_MEMBERS_PER_TEAM
87+ } )
88+ ) ;
89+
90+ return teamCreateInput ( this . faker , organization . organizationId , head , leads , teamMembers , teamTypesByName , config ) ;
91+ } ) ;
92+
93+ const teams = await Promise . all ( teamCreateInputs . map ( ( data ) => this . prisma . team . create ( { data } ) ) ) ;
8194
8295 const teamsByName = teams . reduce < Record < string , Team > > ( ( acc , team ) => {
8396 acc [ team . teamName ] = team ;
@@ -92,4 +105,4 @@ export class TeamProcess extends SeedProcess<TeamInput, TeamOutput> {
92105
93106 return { teams, financeTeam, teamsByName } ;
94107 }
95- }
108+ }
0 commit comments