Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion core/src/franchise/franchise.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import {forwardRef, Module} from "@nestjs/common";
import {JwtModule} from "@nestjs/jwt";
import {TypeOrmModule} from "@nestjs/typeorm";
import {
AnalyticsModule, config, EventsModule, NotificationModule,
} from "@sprocketbot/common";

import {FranchiseLeadershipRole} from "../database/authorization/franchise_leadership_role";
import {FranchiseLeadershipSeat} from "../database/authorization/franchise_leadership_seat";
import {FranchiseStaffRole} from "../database/authorization/franchise_staff_role";
import {FranchiseStaffSeat} from "../database/authorization/franchise_staff_seat";
import {FranchiseLeadershipAppointment} from "../database/franchise/franchise_leadership_appointment";
import {FranchiseStaffAppointment} from "../database/franchise/franchise_staff_appointment";
import {Player} from "../database/franchise/player/player.model";
import {RosterRole} from "../database/franchise/roster_role";
import {RosterSlot} from "../database/franchise/roster_slot";
import {Team} from "../database/franchise/team/team.model";
import {DatabaseModule} from "../database";
import {EloConnectorModule} from "../elo/elo-connector";
import {GameModule} from "../game";
import {MLE_Player} from "../database/mledb/Player.model";
import {MLE_Team} from "../database/mledb/Team.model";
import {MLE_TeamToCaptain} from "../database/mledb/TeamToCaptain.model";
import {LeagueToSkillGroup} from "../database/mledb-bridge/league_to_skill_group.model";
import {PlayerToPlayer} from "../database/mledb-bridge/player_to_player.model";
import {TeamToFranchise} from "../database/mledb-bridge/team_to_franchise.model";
import {MledbInterfaceModule} from "../mledb";
import {OrganizationModule} from "../organization/organization.module";
import {UtilModule} from "../util/util.module";
Expand All @@ -22,11 +39,32 @@ import {
import {PlayerService} from "./player";
import {PlayerController} from "./player/player.controller";
import {PlayerResolver} from "./player/player.resolver";
import {RosterAuthorityService} from "./roster-authority.service";
import {TeamService} from "./team/team.service";

const rosterAuthorityOrm = TypeOrmModule.forFeature([
MLE_Player,
MLE_Team,
MLE_TeamToCaptain,
TeamToFranchise,
LeagueToSkillGroup,
PlayerToPlayer,
Player,
RosterRole,
RosterSlot,
Team,
FranchiseStaffAppointment,
FranchiseLeadershipAppointment,
FranchiseStaffRole,
FranchiseStaffSeat,
FranchiseLeadershipRole,
FranchiseLeadershipSeat,
]);

@Module({
imports: [
DatabaseModule,
rosterAuthorityOrm,
UtilModule,
NotificationModule,
EventsModule,
Expand All @@ -41,6 +79,7 @@ import {TeamService} from "./team/team.service";
],
providers: [
PlayerService,
RosterAuthorityService,
GameSkillGroupService,
GameSkillGroupResolver,
FranchiseService,
Expand All @@ -49,7 +88,7 @@ import {TeamService} from "./team/team.service";
PlayerResolver,
TeamService,
],
exports: [PlayerService, FranchiseService, GameSkillGroupService, TeamService],
exports: [PlayerService, FranchiseService, GameSkillGroupService, TeamService, RosterAuthorityService],
controllers: [FranchiseController, GameSkillGroupController, PlayerController],
})
export class FranchiseModule {}
7 changes: 7 additions & 0 deletions core/src/franchise/franchise/franchise.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {FranchiseProfile} from "$db/franchise/franchise_profile/franchise_profil
import {MledbPlayerService} from "../../mledb";
import {MemberService} from "../../organization";
import {PlayerService} from "../player";
import {RosterAuthorityService} from "../roster-authority.service";

@Injectable()
export class FranchiseService {
Expand All @@ -23,6 +24,7 @@ export class FranchiseService {
private readonly sprocketMemberService: MemberService,
@InjectRepository(FranchiseProfile)
private readonly franchiseProfileRepository: Repository<FranchiseProfile>,
private readonly rosterAuthorityService: RosterAuthorityService,
) {}

async getFranchiseProfile(franchiseId: number): Promise<FranchiseProfile> {
Expand Down Expand Up @@ -62,6 +64,11 @@ export class FranchiseService {
}

async getPlayerFranchisesByUserId(userId: number): Promise<CoreOutput<CoreEndpoint.GetPlayerFranchises>> {
const fromSprocket = await this.rosterAuthorityService.getPlayerFranchisesFromSprocket(userId);
if (fromSprocket.length > 0) {
return fromSprocket;
}

const mlePlayer = await this.mledbPlayerService.getMlePlayerBySprocketUser(userId);

const playerId = mlePlayer.id;
Expand Down
43 changes: 41 additions & 2 deletions core/src/franchise/player/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {PlatformService} from "../../game";
import {OrganizationService} from "../../organization";
import {MemberService} from "../../organization/member/member.service";
import {GameSkillGroupService} from "../game-skill-group";
import {RosterAuthorityService} from "../roster-authority.service";
import type {RankdownJwtPayload} from "./player.types";
import type {CreatePlayerTuple} from "./player.types";

Expand Down Expand Up @@ -83,8 +84,19 @@ export class PlayerService {
private readonly eloConnectorService: EloConnectorService,
private readonly platformService: PlatformService,
private readonly analyticsService: AnalyticsService,
private readonly rosterAuthorityService: RosterAuthorityService,
) {}

private async syncRosterAuthorityAfterMleSave(mlePlayerId: number): Promise<void> {
try {
await this.rosterAuthorityService.syncFromMlePlayerId(mlePlayerId);
} catch (err) {
this.logger.warn(
`Roster authority sync failed for MLE player ${mlePlayerId}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}

async getPlayer(query: FindOneOptions<Player>): Promise<Player> {
this.logger.debug(`getPlayer: ${JSON.stringify(query)}`);
return this.playerRepository.findOneOrFail(query);
Expand Down Expand Up @@ -339,6 +351,11 @@ export class PlayerService {
throw new Error(`Tried updating player with MLEID: ${mleid}, but that MLEID does not yet exist.`);
}
await runner.commitTransaction();

const mleAfter = await this.mle_playerRepository.findOne({where: {mleid} });
if (mleAfter) {
await this.syncRosterAuthorityAfterMleSave(mleAfter.id);
}
} catch (e) {
await runner.rollbackTransaction();
this.logger.error(e);
Expand Down Expand Up @@ -375,9 +392,10 @@ export class PlayerService {
});

if (runner) {
await runner.manager.save(player);
await runner.manager.save(MLE_Player, updatedPlayer);
} else {
await this.mle_playerRepository.save(player);
await this.mle_playerRepository.save(updatedPlayer);
await this.syncRosterAuthorityAfterMleSave(updatedPlayer.id);
}

return updatedPlayer;
Expand Down Expand Up @@ -434,6 +452,7 @@ export class PlayerService {
await runner.manager.save(ptpBridge);
} else {
await this.ptpRepo.save(ptpBridge);
await this.syncRosterAuthorityAfterMleSave(player.id);
}

return player;
Expand Down Expand Up @@ -788,6 +807,7 @@ export class PlayerService {
});

await this.mle_playerRepository.save(newMlePlayer);
await this.syncRosterAuthorityAfterMleSave(newMlePlayer.id);

this.logger.debug(`Player ${playerDelta.playerId}: Salary update complete`);
}
Expand Down Expand Up @@ -842,6 +862,7 @@ export class PlayerService {
const mlePlayer = await this.getMlePlayerBySprocketPlayer(sprocPlayerId);
mlePlayer.salary = salary;
await this.mle_playerRepository.save(mlePlayer);
await this.syncRosterAuthorityAfterMleSave(mlePlayer.id);
return mlePlayer;
}

Expand Down Expand Up @@ -872,6 +893,7 @@ export class PlayerService {
}

await this.mle_playerRepository.save(player);
await this.syncRosterAuthorityAfterMleSave(player.id);

return player;
}
Expand Down Expand Up @@ -907,6 +929,7 @@ export class PlayerService {
});

await this.mle_playerRepository.save(player);
await this.syncRosterAuthorityAfterMleSave(player.id);

// Move player to Waiver Wire
// TODO fix later when we abstract away from MLE
Expand Down Expand Up @@ -954,6 +977,7 @@ export class PlayerService {
});

await this.mle_playerRepository.save(player);
await this.syncRosterAuthorityAfterMleSave(player.id);
return player;
}

Expand Down Expand Up @@ -1268,6 +1292,19 @@ export class PlayerService {
await runner.commitTransaction();
this.logger.log(`Transaction committed successfully`);

const rlPlayers = await this.playerRepository.find({
where: {
member: {user: {id: user.id} },
skillGroup: {game: {id: 7} },
},
});
for (const pl of rlPlayers) {
const bridge = await this.ptpRepo.findOne({where: {sprocketPlayerId: pl.id} });
if (bridge) {
await this.syncRosterAuthorityAfterMleSave(bridge.mledPlayerId);
}
}

const result = `Successfully created/updated user with ID ${user.id}.`;
this.logger.log(`=== INTAKE USER COMPLETED ===`);
this.logger.log(`Result: ${result}`);
Expand Down Expand Up @@ -1332,6 +1369,7 @@ export class PlayerService {
});
mlePlayer.discordId = newAcct;
await this.mle_playerRepository.save(mlePlayer);
await this.syncRosterAuthorityAfterMleSave(mlePlayer.id);

// Then, follow up with Sprocket.
const uaa = await this.userAuthRepository.findOneOrFail({
Expand All @@ -1351,6 +1389,7 @@ export class PlayerService {
});
mlePlayer.teamName = newTeam;
await this.mle_playerRepository.save(mlePlayer);
await this.syncRosterAuthorityAfterMleSave(mlePlayer.id);
}

async changePlayerName(mleid: number, newName: string): Promise<void> {
Expand Down
Loading
Loading