volleyballsimtypes 0.0.462 → 0.0.463

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/cjs/src/api/index.d.ts +2 -0
  2. package/dist/cjs/src/data/init-models.js +9 -0
  3. package/dist/cjs/src/data/models/index.d.ts +1 -0
  4. package/dist/cjs/src/data/models/index.js +1 -0
  5. package/dist/cjs/src/data/models/match-preset.d.ts +28 -0
  6. package/dist/cjs/src/data/models/match-preset.js +65 -0
  7. package/dist/cjs/src/data/models/match.d.ts +2 -1
  8. package/dist/cjs/src/data/transformers/division.js +1 -1
  9. package/dist/cjs/src/data/transformers/match-preset-override.test.d.ts +1 -0
  10. package/dist/cjs/src/data/transformers/match-preset-override.test.js +275 -0
  11. package/dist/cjs/src/data/transformers/match.d.ts +2 -1
  12. package/dist/cjs/src/data/transformers/match.js +15 -3
  13. package/dist/cjs/src/data/transformers/qualifier.js +1 -1
  14. package/dist/cjs/src/data/transformers/season.js +1 -1
  15. package/dist/cjs/src/data/transformers/tactics.d.ts +3 -2
  16. package/dist/cjs/src/data/transformers/tactics.js +36 -0
  17. package/dist/cjs/src/data/transformers/team.d.ts +7 -1
  18. package/dist/cjs/src/data/transformers/team.js +22 -4
  19. package/dist/cjs/src/data/transformers/tournament.js +1 -1
  20. package/dist/esm/src/api/index.d.ts +2 -0
  21. package/dist/esm/src/data/init-models.js +10 -1
  22. package/dist/esm/src/data/models/index.d.ts +1 -0
  23. package/dist/esm/src/data/models/index.js +1 -0
  24. package/dist/esm/src/data/models/match-preset.d.ts +28 -0
  25. package/dist/esm/src/data/models/match-preset.js +61 -0
  26. package/dist/esm/src/data/models/match.d.ts +2 -1
  27. package/dist/esm/src/data/transformers/division.js +1 -1
  28. package/dist/esm/src/data/transformers/match-preset-override.test.d.ts +1 -0
  29. package/dist/esm/src/data/transformers/match-preset-override.test.js +273 -0
  30. package/dist/esm/src/data/transformers/match.d.ts +2 -1
  31. package/dist/esm/src/data/transformers/match.js +15 -3
  32. package/dist/esm/src/data/transformers/qualifier.js +1 -1
  33. package/dist/esm/src/data/transformers/season.js +1 -1
  34. package/dist/esm/src/data/transformers/tactics.d.ts +3 -2
  35. package/dist/esm/src/data/transformers/tactics.js +36 -1
  36. package/dist/esm/src/data/transformers/team.d.ts +7 -1
  37. package/dist/esm/src/data/transformers/team.js +20 -5
  38. package/dist/esm/src/data/transformers/tournament.js +1 -1
  39. package/package.json +1 -1
@@ -45,7 +45,7 @@ function transformToObject(model) {
45
45
  id: model.competition_id,
46
46
  iteration: (0, _1.transformToIteration)(model.Iteration),
47
47
  matches: (model.CompetitionMatches ?? []).map(_1.transformToTournamentMatch),
48
- teams: (0, _1.sortTeamsByCompetitionIndex)(model.Teams ?? []).map(_1.transformToTeam),
48
+ teams: (0, _1.sortTeamsByCompetitionIndex)(model.Teams ?? []).map(team => (0, _1.transformToTeam)(team)),
49
49
  status: model.status,
50
50
  champion
51
51
  });
@@ -49,6 +49,7 @@ export type Match = Omit<DataProps<_Match>, 'sets' | 'homeTeam' | 'awayTeam' | '
49
49
  awayForm?: MatchForm[];
50
50
  isFriendly?: boolean;
51
51
  detailUnavailable?: boolean;
52
+ selectedPresetId?: string;
52
53
  };
53
54
  export type RallyEvent = DataProps<_RallyEvent> & {
54
55
  teamId?: string;
@@ -128,6 +129,7 @@ export interface ApiLineupPreset {
128
129
  orderIndex: number;
129
130
  config: Tactics;
130
131
  }
132
+ export type ApiLineupPresetSummary = Omit<ApiLineupPreset, 'config'>;
131
133
  export type Team = Omit<DataProps<_Team>, 'roster' | '_rating' | 'rating' | 'tactics'> & {
132
134
  roster: Player[];
133
135
  rating: {
@@ -1,10 +1,11 @@
1
- import { AuthIdentityModel, AuthSessionModel, AuthUserModel, BoxScoreModel, CoachModel, ScoutModel, BoxScoreTotalsModel, CompetitionChampionModel, CurrencyTransactionModel, GachaPityModel, GachaPullHistoryModel, WishlistModel, UserSettingsModel, UserDeviceTokenModel, CompetitionMatchModel, CompetitionMVPModel, CompetitionModel, CompetitionStandingsMatchModel, CompetitionStandingsModel, CompetitionTeamsModel, CountryModel, DivisionModel, DivisionSeasonModel, IterationModel, LeagueModel, MatchModel, MatchRatingModel, MatchResultModel, MatchSetModel, PerformanceStatsModel, PlayerModel, PlayerImprovementLogModel, PlayerDeclineLogModel, PlayerTeamModel, RetiredPlayerModel, PromotionMatchModel, FriendlyModel, RegionQualifierModel, NationalCountryModel, RallyModel, TacticsModel, LineupPresetModel, TeamModel, TeamReplacementModel, UserCurrencyModel, UserPlayerProgressModel, UserPullGrantModel, UserTeamModel, VPERModel, NotificationModel, AppConfigModel, LegacyTeamFlagModel } from './models';
1
+ import { AuthIdentityModel, AuthSessionModel, AuthUserModel, BoxScoreModel, CoachModel, ScoutModel, BoxScoreTotalsModel, CompetitionChampionModel, CurrencyTransactionModel, GachaPityModel, GachaPullHistoryModel, WishlistModel, UserSettingsModel, UserDeviceTokenModel, CompetitionMatchModel, CompetitionMVPModel, CompetitionModel, CompetitionStandingsMatchModel, CompetitionStandingsModel, CompetitionTeamsModel, CountryModel, DivisionModel, DivisionSeasonModel, IterationModel, LeagueModel, MatchModel, MatchRatingModel, MatchResultModel, MatchSetModel, PerformanceStatsModel, PlayerModel, PlayerImprovementLogModel, PlayerDeclineLogModel, PlayerTeamModel, RetiredPlayerModel, PromotionMatchModel, FriendlyModel, RegionQualifierModel, NationalCountryModel, RallyModel, TacticsModel, LineupPresetModel, MatchPresetModel, TeamModel, TeamReplacementModel, UserCurrencyModel, UserPlayerProgressModel, UserPullGrantModel, UserTeamModel, VPERModel, NotificationModel, AppConfigModel, LegacyTeamFlagModel } from './models';
2
2
  export function initModels(sequelize) {
3
3
  const Coach = CoachModel.initModel(sequelize);
4
4
  const Scout = ScoutModel.initModel(sequelize);
5
5
  const CurrencyTransaction = CurrencyTransactionModel.initModel(sequelize);
6
6
  const Tactics = TacticsModel.initModel(sequelize);
7
7
  const LineupPreset = LineupPresetModel.initModel(sequelize);
8
+ const MatchPreset = MatchPresetModel.initModel(sequelize);
8
9
  const AuthUser = AuthUserModel.initModel(sequelize);
9
10
  const UserCurrency = UserCurrencyModel.initModel(sequelize);
10
11
  const UserPlayerProgress = UserPlayerProgressModel.initModel(sequelize);
@@ -212,6 +213,13 @@ export function initModels(sequelize) {
212
213
  // simulator's team loads are unchanged (it reads the active config from the mirrored Tactics row).
213
214
  Team.hasMany(LineupPreset, { as: 'lineupPresets', foreignKey: 'team_id' });
214
215
  LineupPreset.belongsTo(Team, { as: 'team', foreignKey: 'team_id' });
216
+ // Per-match preset choices. Deliberately NOT part of any default scope: only the simulator's match query
217
+ // loads MatchPresets, so every other transformToMatch caller stays on the active-tactics path.
218
+ Match.hasMany(MatchPreset, { as: 'MatchPresets', foreignKey: 'match_id' });
219
+ MatchPreset.belongsTo(Match, { as: 'match', foreignKey: 'match_id' });
220
+ MatchPreset.belongsTo(Team, { as: 'team', foreignKey: 'team_id' });
221
+ MatchPreset.belongsTo(LineupPreset, { as: 'preset', foreignKey: 'preset_id' });
222
+ LineupPreset.hasMany(MatchPreset, { as: 'MatchPresets', foreignKey: 'preset_id' });
215
223
  Team.belongsTo(Country, { as: 'country', foreignKey: 'country_id' });
216
224
  Team.belongsToMany(Competition, {
217
225
  as: 'Competitions',
@@ -286,6 +294,7 @@ export function initModels(sequelize) {
286
294
  TeamReplacement,
287
295
  Tactics,
288
296
  LineupPreset,
297
+ MatchPreset,
289
298
  UserTeam,
290
299
  UserCurrency,
291
300
  UserPlayerProgress,
@@ -36,6 +36,7 @@ export * from './team';
36
36
  export * from './team-replacement';
37
37
  export * from './tactics';
38
38
  export * from './lineup-preset';
39
+ export * from './match-preset';
39
40
  export * from './user-currency';
40
41
  export * from './user-player-progress';
41
42
  export * from './gacha-pity';
@@ -36,6 +36,7 @@ export * from './team';
36
36
  export * from './team-replacement';
37
37
  export * from './tactics';
38
38
  export * from './lineup-preset';
39
+ export * from './match-preset';
39
40
  export * from './user-currency';
40
41
  export * from './user-player-progress';
41
42
  export * from './gacha-pity';
@@ -0,0 +1,28 @@
1
+ import * as Sequelize from 'sequelize';
2
+ import { Model } from 'sequelize';
3
+ import { LineupPresetId, LineupPresetModel, MatchId, MatchModel, TeamId, TeamModel } from '.';
4
+ export interface MatchPresetAttributes {
5
+ match_id: string;
6
+ team_id: string;
7
+ preset_id: string;
8
+ }
9
+ export type MatchPresetPk = 'match_id' | 'team_id';
10
+ export type MatchPresetCreationAttributes = MatchPresetAttributes;
11
+ export declare class MatchPresetModel extends Model<MatchPresetAttributes, MatchPresetCreationAttributes> implements MatchPresetAttributes {
12
+ match_id: string;
13
+ team_id: string;
14
+ preset_id: string;
15
+ match: MatchModel;
16
+ getMatch: Sequelize.BelongsToGetAssociationMixin<MatchModel>;
17
+ setMatch: Sequelize.BelongsToSetAssociationMixin<MatchModel, MatchId>;
18
+ createMatch: Sequelize.BelongsToCreateAssociationMixin<MatchModel>;
19
+ team: TeamModel;
20
+ getTeam: Sequelize.BelongsToGetAssociationMixin<TeamModel>;
21
+ setTeam: Sequelize.BelongsToSetAssociationMixin<TeamModel, TeamId>;
22
+ createTeam: Sequelize.BelongsToCreateAssociationMixin<TeamModel>;
23
+ preset: LineupPresetModel;
24
+ getPreset: Sequelize.BelongsToGetAssociationMixin<LineupPresetModel>;
25
+ setPreset: Sequelize.BelongsToSetAssociationMixin<LineupPresetModel, LineupPresetId>;
26
+ createPreset: Sequelize.BelongsToCreateAssociationMixin<LineupPresetModel>;
27
+ static initModel(sequelize: Sequelize.Sequelize): typeof MatchPresetModel;
28
+ }
@@ -0,0 +1,61 @@
1
+ import { DataTypes, Model } from 'sequelize';
2
+ export class MatchPresetModel extends Model {
3
+ static initModel(sequelize) {
4
+ return MatchPresetModel.init({
5
+ match_id: {
6
+ type: DataTypes.UUID,
7
+ allowNull: false,
8
+ primaryKey: true,
9
+ references: {
10
+ model: 'Match',
11
+ key: 'match_id'
12
+ }
13
+ },
14
+ team_id: {
15
+ type: DataTypes.UUID,
16
+ allowNull: false,
17
+ primaryKey: true,
18
+ references: {
19
+ model: 'Team',
20
+ key: 'team_id'
21
+ }
22
+ },
23
+ preset_id: {
24
+ type: DataTypes.UUID,
25
+ allowNull: false,
26
+ references: {
27
+ model: 'LineupPreset',
28
+ key: 'preset_id'
29
+ }
30
+ }
31
+ }, {
32
+ sequelize,
33
+ tableName: 'MatchPreset',
34
+ schema: 'public',
35
+ timestamps: false,
36
+ indexes: [
37
+ {
38
+ name: 'MatchPreset_pk',
39
+ unique: true,
40
+ fields: [
41
+ { name: 'match_id' },
42
+ { name: 'team_id' }
43
+ ]
44
+ },
45
+ {
46
+ name: 'MatchPreset_team_id_idx',
47
+ fields: [
48
+ { name: 'team_id' }
49
+ ]
50
+ },
51
+ {
52
+ // Keeps the LineupPreset ON DELETE CASCADE cheap.
53
+ name: 'MatchPreset_preset_id_idx',
54
+ fields: [
55
+ { name: 'preset_id' }
56
+ ]
57
+ }
58
+ ]
59
+ });
60
+ }
61
+ }
@@ -1,7 +1,7 @@
1
1
  import * as Sequelize from 'sequelize';
2
2
  import { Model } from 'sequelize';
3
3
  import { Status } from '../common';
4
- import { CompetitionMatchId, CompetitionMatchModel, MatchRatingId, MatchRatingModel, MatchResultId, MatchResultModel, MatchSetAttributes, MatchSetId, MatchSetModel, VPERAttributes, VPERId, VPERModel, TeamId, TeamModel } from '.';
4
+ import { CompetitionMatchId, CompetitionMatchModel, MatchRatingId, MatchRatingModel, MatchResultId, MatchResultModel, MatchPresetModel, MatchSetAttributes, MatchSetId, MatchSetModel, VPERAttributes, VPERId, VPERModel, TeamId, TeamModel } from '.';
5
5
  import { MatchRallyAggregates } from '../../service';
6
6
  export interface MatchAttributes {
7
7
  match_id: string;
@@ -59,6 +59,7 @@ export declare class MatchModel extends Model<MatchAttributes, MatchCreationAttr
59
59
  hasVPER: Sequelize.HasManyHasAssociationMixin<VPERModel, VPERId>;
60
60
  hasVPERs: Sequelize.HasManyHasAssociationsMixin<VPERModel, VPERId>;
61
61
  countVPERs: Sequelize.HasManyCountAssociationsMixin;
62
+ MatchPresets?: MatchPresetModel[];
62
63
  MatchSets: MatchSetModel[];
63
64
  getMatchSets: Sequelize.HasManyGetAssociationsMixin<MatchSetModel>;
64
65
  setMatchSets: Sequelize.HasManySetAssociationsMixin<MatchSetModel, MatchSetId>;
@@ -23,7 +23,7 @@ function transformToObject(model) {
23
23
  subdivision: model.subdivision,
24
24
  leagueId: model.league_id,
25
25
  seasons: (model.DivisionSeasons ?? []).map((ds) => transformToSeason(ds.competition)),
26
- teams: model.Teams != null ? model.Teams.map(transformToTeam) : undefined
26
+ teams: model.Teams != null ? model.Teams.map(team => transformToTeam(team)) : undefined
27
27
  });
28
28
  }
29
29
  export { transformToObject as transformToDivision, transformToAttributes as transformFromDivision };
@@ -0,0 +1,273 @@
1
+ import { describe, it, expect, jest } from '@jest/globals';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ import { apiTacticsConfigToTacticsAttributes, tacticsColumnsToApiDto, transformToTactics } from './tactics';
4
+ import { transformToTeam } from './team';
5
+ import { transformToMatch } from './match';
6
+ import { CourtPosition, DeclineProfileEnum, RarityEnum, RoleEnum } from '../../service';
7
+ // ─── shared mocks (model-shaped plain objects, cast through unknown) ──────────
8
+ const countryFixture = {
9
+ country_id: uuidv4(),
10
+ name: 'Test Country',
11
+ alpha_2: 'TC',
12
+ alpha_3: 'TCN',
13
+ country_code: '999',
14
+ iso_3166_2: 'TC-TEST',
15
+ region: '',
16
+ region_code: '',
17
+ sub_region: '',
18
+ sub_region_code: '',
19
+ intermediate_region: '',
20
+ intermediate_region_code: ''
21
+ };
22
+ function makePlayerModel(id) {
23
+ const stat = 50;
24
+ return {
25
+ player_id: id,
26
+ first_name: 'Test',
27
+ last_name: `P-${id.slice(0, 4)}`,
28
+ rarity: RarityEnum.COMMON,
29
+ roles: [RoleEnum.OUTSIDE_HITTER],
30
+ traits: [],
31
+ birth_age: 18,
32
+ birth_iteration: 1,
33
+ last_declined_iteration: null,
34
+ decline_profile: DeclineProfileEnum.STANDARD,
35
+ BoxScores: [],
36
+ BoxScoreTotals: null,
37
+ country: countryFixture,
38
+ PerformanceStat: {
39
+ setting: stat,
40
+ serve: stat,
41
+ spike: stat,
42
+ quick: stat,
43
+ power: stat,
44
+ awareness: stat,
45
+ attack: stat,
46
+ back_attack: stat,
47
+ reception: stat,
48
+ overhand: stat,
49
+ bump: stat,
50
+ block: stat,
51
+ read: stat,
52
+ commit: stat,
53
+ focus: stat,
54
+ defense: stat,
55
+ stamina: stat,
56
+ reflex: stat
57
+ }
58
+ };
59
+ }
60
+ // Six starters; index order is the row lineup's zone 1..6 order.
61
+ const ids = Array.from({ length: 6 }, () => uuidv4());
62
+ const teamId = uuidv4();
63
+ function rowLineup(order) {
64
+ return {
65
+ [CourtPosition.RIGHT_BACK]: order[0],
66
+ [CourtPosition.RIGHT_FRONT]: order[1],
67
+ [CourtPosition.MIDDLE_FRONT]: order[2],
68
+ [CourtPosition.LEFT_FRONT]: order[3],
69
+ [CourtPosition.LEFT_BACK]: order[4],
70
+ [CourtPosition.MIDDLE_BACK]: order[5],
71
+ bench: []
72
+ };
73
+ }
74
+ function makeTacticsRow(order) {
75
+ return {
76
+ team_id: teamId,
77
+ lineup: rowLineup(order),
78
+ libero_replacements: [],
79
+ receive_rotation_offset: false,
80
+ designated_subs: [],
81
+ substitution_band: 'TIRED',
82
+ rotation_system: '6-0',
83
+ designated_setters: [],
84
+ offensive_preferences: []
85
+ };
86
+ }
87
+ function makeTeamModel(activeOrder) {
88
+ return {
89
+ team_id: teamId,
90
+ name: 'Test Team',
91
+ short_name: 'TST',
92
+ rating: 0,
93
+ division_id: uuidv4(),
94
+ active: true,
95
+ country: countryFixture,
96
+ tactics: makeTacticsRow(activeOrder),
97
+ PlayerTeams: ids.map(id => ({ player: makePlayerModel(id) }))
98
+ };
99
+ }
100
+ // The API-DTO config for a preset whose starters are the REVERSE of the active order, so an applied
101
+ // override is visible through the hydrated lineup.
102
+ function makeConfig(order) {
103
+ return {
104
+ lineup: {
105
+ [CourtPosition.RIGHT_BACK]: order[0],
106
+ [CourtPosition.RIGHT_FRONT]: order[1],
107
+ [CourtPosition.MIDDLE_FRONT]: order[2],
108
+ [CourtPosition.LEFT_FRONT]: order[3],
109
+ [CourtPosition.LEFT_BACK]: order[4],
110
+ [CourtPosition.MIDDLE_BACK]: order[5],
111
+ bench: []
112
+ },
113
+ liberoReplacements: [],
114
+ receiveRotationOffset: false,
115
+ designatedSubs: [],
116
+ substitutionBand: 'TIRED',
117
+ rotationSystem: '6-0',
118
+ designatedSetters: [],
119
+ offensivePreferences: []
120
+ };
121
+ }
122
+ const activeOrder = ids;
123
+ const presetOrder = [...ids].reverse();
124
+ function starterAt(tactics, zone) {
125
+ return tactics.lineup[zone]?.id;
126
+ }
127
+ // ─── apiTacticsConfigToTacticsAttributes ──────────────────────────────────────
128
+ describe('apiTacticsConfigToTacticsAttributes()', () => {
129
+ it('round-trips a fully-loaded Tactics row through tacticsColumnsToApiDto', () => {
130
+ const full = {
131
+ team_id: teamId,
132
+ lineup: { ...rowLineup(activeOrder), [CourtPosition.LIBERO_ZONE]: undefined },
133
+ libero_replacements: [ids[5]],
134
+ receive_rotation_offset: true,
135
+ designated_subs: [{ starterId: ids[0], benchId: undefined, mode: 'FATIGUE', fatigueBand: 'WORN', sets: [{ mode: 'NEVER' }] }],
136
+ substitution_band: 'TIRED',
137
+ second_libero: undefined,
138
+ libero_sub: undefined,
139
+ libero_replacement_sets: [[ids[0]], [ids[1]]],
140
+ rotation_system: '5-1',
141
+ designated_setters: [ids[0]],
142
+ offensive_preferences: [{ rotation: 1, order: [ids[1], ids[2]] }],
143
+ system_sets: [{ rotationSystem: '5-1', designatedSetters: [ids[0]] }],
144
+ offensive_preference_sets: [[{ rotation: 2, order: [ids[3]] }]]
145
+ };
146
+ const dto = tacticsColumnsToApiDto(full);
147
+ const back = apiTacticsConfigToTacticsAttributes(dto, teamId);
148
+ expect(back.lineup).toEqual(full.lineup);
149
+ expect(back.libero_replacements).toEqual(full.libero_replacements);
150
+ expect(back.receive_rotation_offset).toBe(true);
151
+ expect(back.designated_subs).toEqual(full.designated_subs);
152
+ expect(back.libero_replacement_sets).toEqual(full.libero_replacement_sets);
153
+ expect(back.rotation_system).toBe('5-1');
154
+ expect(back.designated_setters).toEqual(full.designated_setters);
155
+ expect(back.offensive_preferences).toEqual(full.offensive_preferences);
156
+ expect(back.system_sets).toEqual(full.system_sets);
157
+ expect(back.offensive_preference_sets).toEqual(full.offensive_preference_sets);
158
+ });
159
+ it('hydrates through transformToTactics identically to the row path', () => {
160
+ const roster = ids.map(id => transformToTeam(makeTeamModel(activeOrder)).roster.find(p => p.id === id));
161
+ expect(roster.every(p => p != null)).toBe(true);
162
+ const teamModel = makeTeamModel(activeOrder);
163
+ const players = transformToTeam(teamModel).roster;
164
+ const fromRow = transformToTactics(makeTacticsRow(activeOrder), players);
165
+ const fromConfig = transformToTactics(apiTacticsConfigToTacticsAttributes(makeConfig(activeOrder), teamId), players);
166
+ for (const zone of [1, 2, 3, 4, 5, 6]) {
167
+ expect(starterAt(fromConfig, zone)).toBe(starterAt(fromRow, zone));
168
+ }
169
+ });
170
+ it('throws PLAYER_NOT_FOUND when the config references an off-roster player', () => {
171
+ const players = transformToTeam(makeTeamModel(activeOrder)).roster;
172
+ const drifted = makeConfig([uuidv4(), ...ids.slice(1)]);
173
+ expect(() => transformToTactics(apiTacticsConfigToTacticsAttributes(drifted, teamId), players)).toThrow('PLAYER_NOT_FOUND');
174
+ });
175
+ });
176
+ // ─── transformToTeam override / fallback ──────────────────────────────────────
177
+ describe('transformToTeam() tacticsOverride', () => {
178
+ it('uses the override config when it hydrates', () => {
179
+ const team = transformToTeam(makeTeamModel(activeOrder), undefined, {
180
+ presetId: 'preset-1',
181
+ config: makeConfig(presetOrder)
182
+ });
183
+ expect(starterAt(team.tactics, CourtPosition.RIGHT_BACK)).toBe(presetOrder[0]);
184
+ });
185
+ it('falls back to the active tactics and notifies onFallback when the override fails', () => {
186
+ const onFallback = jest.fn();
187
+ const drifted = makeConfig([uuidv4(), ...ids.slice(1)]);
188
+ const team = transformToTeam(makeTeamModel(activeOrder), undefined, {
189
+ presetId: 'preset-1',
190
+ config: drifted,
191
+ onFallback
192
+ });
193
+ expect(onFallback).toHaveBeenCalledTimes(1);
194
+ expect(onFallback.mock.calls[0][0]).toBe('preset-1');
195
+ expect(starterAt(team.tactics, CourtPosition.RIGHT_BACK)).toBe(activeOrder[0]);
196
+ });
197
+ it('keeps the active tactics when no override is given', () => {
198
+ const team = transformToTeam(makeTeamModel(activeOrder));
199
+ expect(starterAt(team.tactics, CourtPosition.RIGHT_BACK)).toBe(activeOrder[0]);
200
+ });
201
+ });
202
+ // ─── transformToMatch per-team override ───────────────────────────────────────
203
+ describe('transformToMatch() MatchPresets', () => {
204
+ const awayIds = Array.from({ length: 6 }, () => uuidv4());
205
+ const awayTeamId = uuidv4();
206
+ function makeAwayModel() {
207
+ return {
208
+ team_id: awayTeamId,
209
+ name: 'Away Team',
210
+ short_name: 'AWY',
211
+ rating: 0,
212
+ division_id: uuidv4(),
213
+ active: true,
214
+ country: countryFixture,
215
+ tactics: {
216
+ team_id: awayTeamId,
217
+ lineup: {
218
+ [CourtPosition.RIGHT_BACK]: awayIds[0],
219
+ [CourtPosition.RIGHT_FRONT]: awayIds[1],
220
+ [CourtPosition.MIDDLE_FRONT]: awayIds[2],
221
+ [CourtPosition.LEFT_FRONT]: awayIds[3],
222
+ [CourtPosition.LEFT_BACK]: awayIds[4],
223
+ [CourtPosition.MIDDLE_BACK]: awayIds[5],
224
+ bench: []
225
+ },
226
+ libero_replacements: [],
227
+ receive_rotation_offset: false,
228
+ designated_subs: [],
229
+ substitution_band: 'TIRED',
230
+ rotation_system: '6-0',
231
+ designated_setters: [],
232
+ offensive_preferences: []
233
+ },
234
+ PlayerTeams: awayIds.map(id => ({ player: makePlayerModel(id) }))
235
+ };
236
+ }
237
+ function makeMatchModel(withPreset) {
238
+ return {
239
+ match_id: uuidv4(),
240
+ home_team: teamId,
241
+ away_team: awayTeamId,
242
+ scheduled_date: new Date(),
243
+ status: 'PENDING',
244
+ HomeTeam: makeTeamModel(activeOrder),
245
+ AwayTeam: makeAwayModel(),
246
+ MatchSets: [],
247
+ VPERs: [],
248
+ MatchPresets: withPreset
249
+ ? [{ match_id: 'm', team_id: teamId, preset_id: 'preset-1', preset: { config: makeConfig(presetOrder) } }]
250
+ : undefined
251
+ };
252
+ }
253
+ it('applies the home override and leaves the away team on active tactics', () => {
254
+ const match = transformToMatch(makeMatchModel(true));
255
+ expect(starterAt(match.homeTeam.tactics, CourtPosition.RIGHT_BACK)).toBe(presetOrder[0]);
256
+ expect(starterAt(match.awayTeam.tactics, CourtPosition.RIGHT_BACK)).toBe(awayIds[0]);
257
+ });
258
+ it('behaves exactly as before when the association is absent', () => {
259
+ const match = transformToMatch(makeMatchModel(false));
260
+ expect(starterAt(match.homeTeam.tactics, CourtPosition.RIGHT_BACK)).toBe(activeOrder[0]);
261
+ expect(starterAt(match.awayTeam.tactics, CourtPosition.RIGHT_BACK)).toBe(awayIds[0]);
262
+ });
263
+ it('routes the fallback callback with the failing team id', () => {
264
+ const onPresetFallback = jest.fn();
265
+ const model = makeMatchModel(true);
266
+ model.MatchPresets[0].preset.config = makeConfig([uuidv4(), ...ids.slice(1)]);
267
+ const match = transformToMatch(model, undefined, onPresetFallback);
268
+ expect(onPresetFallback).toHaveBeenCalledTimes(1);
269
+ expect(onPresetFallback.mock.calls[0][0]).toBe(teamId);
270
+ expect(onPresetFallback.mock.calls[0][1]).toBe('preset-1');
271
+ expect(starterAt(match.homeTeam.tactics, CourtPosition.RIGHT_BACK)).toBe(activeOrder[0]);
272
+ });
273
+ });
@@ -1,5 +1,6 @@
1
1
  import { MatchAttributes, MatchModel } from '../models';
2
2
  import { Match } from '../../service';
3
+ export type PresetFallbackHandler = (teamId: string, presetId: string, err: unknown) => void;
3
4
  declare function transformToAttributes(match: Match): MatchAttributes;
4
- declare function transformToObject(model: MatchModel, currentIteration?: number): Match;
5
+ declare function transformToObject(model: MatchModel, currentIteration?: number, onPresetFallback?: PresetFallbackHandler): Match;
5
6
  export { transformToObject as transformToMatch, transformToAttributes as transformFromMatch };
@@ -11,7 +11,19 @@ function transformToAttributes(match) {
11
11
  status: match.status
12
12
  };
13
13
  }
14
- function transformToObject(model, currentIteration) {
14
+ function transformToObject(model, currentIteration, onPresetFallback) {
15
+ // The (match, team)-chosen preset override for one side, when the loader included MatchPresets with the
16
+ // preset loaded (only the simulator's match query does). Association absent = active tactics, as always.
17
+ const overrideFor = (teamId) => {
18
+ const row = (model.MatchPresets ?? []).find(mp => mp.team_id === teamId);
19
+ if (row?.preset == null)
20
+ return undefined;
21
+ return {
22
+ presetId: row.preset_id,
23
+ config: row.preset.config,
24
+ onFallback: (presetId, err) => onPresetFallback?.(teamId, presetId, err)
25
+ };
26
+ };
15
27
  const sets = (model.MatchSets ?? []).map(transformToMatchSet)
16
28
  .sort((s1, s2) => s1.order - s2.order);
17
29
  let homeScore = 0;
@@ -43,8 +55,8 @@ function transformToObject(model, currentIteration) {
43
55
  const VPERs = (model.VPERs ?? []).map(transformToVPER);
44
56
  return Match.create({
45
57
  id: model.match_id,
46
- homeTeam: transformToTeam(model.HomeTeam, currentIteration),
47
- awayTeam: transformToTeam(model.AwayTeam, currentIteration),
58
+ homeTeam: transformToTeam(model.HomeTeam, currentIteration, overrideFor(model.home_team)),
59
+ awayTeam: transformToTeam(model.AwayTeam, currentIteration, overrideFor(model.away_team)),
48
60
  scheduledDate: new Date(model.scheduled_date),
49
61
  sets,
50
62
  status: model.status,
@@ -40,7 +40,7 @@ function transformToObject(model) {
40
40
  id: model.competition_id,
41
41
  iteration: transformToIteration(model.Iteration),
42
42
  matches: (model.CompetitionMatches ?? []).map(transformToQualifierMatch),
43
- teams: sortTeamsByCompetitionIndex(model.Teams ?? []).map(transformToTeam),
43
+ teams: sortTeamsByCompetitionIndex(model.Teams ?? []).map(team => transformToTeam(team)),
44
44
  status: model.status,
45
45
  region: transformToRegion(regionQualifier),
46
46
  champion
@@ -29,7 +29,7 @@ function transformToAttributes(season) {
29
29
  };
30
30
  }
31
31
  function transformToObject(model) {
32
- const teams = sortTeamsByCompetitionIndex(model.Teams ?? []).map(transformToTeam);
32
+ const teams = sortTeamsByCompetitionIndex(model.Teams ?? []).map(team => transformToTeam(team));
33
33
  // One standing per roster team: the persisted CompetitionStandings aggregate where it exists, a zero-stat
34
34
  // row otherwise. CompetitionStandings only gains a row once a team plays (the DB trigger fires on match
35
35
  // COMPLETE), so mapping it directly dropped un-played teams from the table for the first part of a season.
@@ -2,6 +2,7 @@ import { TacticsAttributes, TacticsModel } from '../models';
2
2
  import { Player, Tactics } from '../../service';
3
3
  import type { Tactics as ApiTactics } from '../../api';
4
4
  declare function transformToAttributes(tactics: Tactics, teamId: string): TacticsAttributes;
5
- declare function transformToObject(model: TacticsModel, roster: Player[]): Tactics;
5
+ declare function transformToObject(model: TacticsAttributes, roster: Player[]): Tactics;
6
6
  declare function tacticsColumnsToApiDto(model: TacticsModel): ApiTactics;
7
- export { transformToObject as transformToTactics, transformToAttributes as transformFromTactics, tacticsColumnsToApiDto };
7
+ declare function apiTacticsConfigToAttributes(config: ApiTactics, teamId: string): TacticsAttributes;
8
+ export { transformToObject as transformToTactics, transformToAttributes as transformFromTactics, tacticsColumnsToApiDto, apiTacticsConfigToAttributes as apiTacticsConfigToTacticsAttributes };
@@ -84,6 +84,8 @@ function transformToAttributes(tactics, teamId) {
84
84
  })))
85
85
  };
86
86
  }
87
+ // Accepts TacticsAttributes (not just TacticsModel) so a per-match preset's config, shaped as column data by
88
+ // apiTacticsConfigToTacticsAttributes below, can be hydrated through the exact same validation/healing path.
87
89
  function transformToObject(model, roster) {
88
90
  const lineup = transformToLineup(model.lineup, roster);
89
91
  // A second libero is only valid as a RESERVE: it needs a starting libero (LIBERO_ZONE), the L2 player must be
@@ -219,4 +221,37 @@ function tacticsColumnsToApiDto(model) {
219
221
  offensivePreferenceSets: model.offensive_preference_sets
220
222
  };
221
223
  }
222
- export { transformToObject as transformToTactics, transformToAttributes as transformFromTactics, tacticsColumnsToApiDto };
224
+ // Inverse of tacticsColumnsToApiDto: shape a preset's stored API config (camelCase, player ids) as
225
+ // TacticsAttributes-shaped column data so transformToTactics can hydrate it against a roster through the
226
+ // exact same validation/healing path as the Tactics row. Only the top-level keys need renaming -- the jsonb
227
+ // sub-objects (designated subs, libero sub, preferences, system sets) are stored camelCase in both shapes.
228
+ // Deliberately NOT mirroring writeTacticsRow's null-clears-column rule: nothing is being UPDATEd here, so
229
+ // absent optionals simply stay undefined.
230
+ function apiTacticsConfigToAttributes(config, teamId) {
231
+ return {
232
+ team_id: teamId,
233
+ lineup: {
234
+ [CourtPosition.LIBERO_ZONE]: config.lineup[CourtPosition.LIBERO_ZONE],
235
+ [CourtPosition.LEFT_BACK]: config.lineup[CourtPosition.LEFT_BACK],
236
+ [CourtPosition.LEFT_FRONT]: config.lineup[CourtPosition.LEFT_FRONT],
237
+ [CourtPosition.MIDDLE_BACK]: config.lineup[CourtPosition.MIDDLE_BACK],
238
+ [CourtPosition.MIDDLE_FRONT]: config.lineup[CourtPosition.MIDDLE_FRONT],
239
+ [CourtPosition.RIGHT_BACK]: config.lineup[CourtPosition.RIGHT_BACK],
240
+ [CourtPosition.RIGHT_FRONT]: config.lineup[CourtPosition.RIGHT_FRONT],
241
+ bench: config.lineup.bench
242
+ },
243
+ libero_replacements: config.liberoReplacements,
244
+ receive_rotation_offset: config.receiveRotationOffset,
245
+ designated_subs: config.designatedSubs,
246
+ substitution_band: config.substitutionBand,
247
+ second_libero: config.secondLibero,
248
+ libero_sub: config.liberoSub,
249
+ libero_replacement_sets: config.liberoReplacementSets,
250
+ rotation_system: config.rotationSystem,
251
+ designated_setters: config.designatedSetters,
252
+ offensive_preferences: config.offensivePreferences,
253
+ system_sets: config.systemSets,
254
+ offensive_preference_sets: config.offensivePreferenceSets
255
+ };
256
+ }
257
+ export { transformToObject as transformToTactics, transformToAttributes as transformFromTactics, tacticsColumnsToApiDto, apiTacticsConfigToAttributes as apiTacticsConfigToTacticsAttributes };
@@ -1,5 +1,11 @@
1
1
  import { Team } from '../../service';
2
2
  import { TeamAttributes, TeamModel } from '../models';
3
+ import type { Tactics as ApiTactics } from '../../api';
4
+ export interface MatchTacticsOverride {
5
+ presetId: string;
6
+ config: ApiTactics;
7
+ onFallback?: (presetId: string, err: unknown) => void;
8
+ }
3
9
  declare function transformToAttributes(team: Team): TeamAttributes;
4
- declare function transformToObject(model: TeamModel, currentIteration?: number): Team;
10
+ declare function transformToObject(model: TeamModel, currentIteration?: number, tacticsOverride?: MatchTacticsOverride): Team;
5
11
  export { transformToObject as transformToTeam, transformToAttributes as transformFromTeam };
@@ -1,5 +1,6 @@
1
1
  import { Team } from '../../service';
2
- import { transformFromTactics, transformToCountry, transformToPlayer, transformToTactics } from '.';
2
+ import { apiTacticsConfigToTacticsAttributes, transformFromTactics, transformToCountry, transformToPlayer, transformToTactics } from '.';
3
+ import logger from '../../logger';
3
4
  function transformToAttributes(team) {
4
5
  return {
5
6
  team_id: team.id,
@@ -12,11 +13,25 @@ function transformToAttributes(team) {
12
13
  division_id: team.divisionId
13
14
  };
14
15
  }
15
- function transformToObject(model, currentIteration) {
16
+ function transformToObject(model, currentIteration, tacticsOverride) {
16
17
  const roster = (model.PlayerTeams ?? []).map((pt) => transformToPlayer(pt.player, currentIteration));
17
- const tactics = model.tactics != null && roster != null && roster.length > 0
18
- ? transformToTactics(model.tactics, roster)
19
- : undefined;
18
+ let tactics;
19
+ if (tacticsOverride != null && roster.length > 0) {
20
+ try {
21
+ tactics = transformToTactics(apiTacticsConfigToTacticsAttributes(tacticsOverride.config, model.team_id), roster);
22
+ }
23
+ catch (err) {
24
+ if (tacticsOverride.onFallback != null)
25
+ tacticsOverride.onFallback(tacticsOverride.presetId, err);
26
+ else
27
+ logger.warn(`MATCH_PRESET_FALLBACK: team=${model.team_id} preset=${tacticsOverride.presetId} failed to hydrate; using active tactics`);
28
+ }
29
+ }
30
+ if (tactics == null) {
31
+ tactics = model.tactics != null && roster.length > 0
32
+ ? transformToTactics(model.tactics, roster)
33
+ : undefined;
34
+ }
20
35
  return Team.create({
21
36
  id: model.team_id,
22
37
  name: model.name,