first (part 3: src directory)
Deploy Iddaai Backend / build-and-deploy (push) Successful in 33s

This commit is contained in:
2026-04-16 15:12:27 +03:00
parent 2f0b85a0c7
commit 182f4aae16
125 changed files with 22552 additions and 0 deletions
+173
View File
@@ -0,0 +1,173 @@
import { Injectable, Logger } from '@nestjs/common';
import { PrismaService } from '../../database/prisma.service';
import { Sport } from '@prisma/client';
@Injectable()
export class LeaguesService {
private readonly logger = new Logger(LeaguesService.name);
constructor(private readonly prisma: PrismaService) {}
/**
* Get all countries
*/
async findAllCountries() {
return this.prisma.country.findMany({
orderBy: { name: 'asc' },
});
}
/**
* Get country by ID
*/
async findCountryById(id: string) {
return this.prisma.country.findUnique({
where: { id },
include: { leagues: true },
});
}
/**
* Get all leagues
*/
async findAllLeagues(sport?: Sport) {
return this.prisma.league.findMany({
where: sport ? { sport } : undefined,
include: { country: true },
orderBy: { name: 'asc' },
});
}
/**
* Get league by ID
*/
async findLeagueById(id: string) {
return this.prisma.league.findUnique({
where: { id },
include: { country: true },
});
}
/**
* Get leagues by country
*/
async findLeaguesByCountry(countryId: string, sport?: Sport) {
return this.prisma.league.findMany({
where: {
countryId,
...(sport ? { sport } : {}),
},
include: { country: true },
orderBy: { name: 'asc' },
});
}
/**
* Get all teams
*/
async findAllTeams(sport?: Sport, search?: string) {
return this.prisma.team.findMany({
where: {
...(sport ? { sport } : {}),
...(search ? { name: { contains: search, mode: 'insensitive' } } : {}),
},
orderBy: { name: 'asc' },
take: 100,
});
}
/**
* Get team by ID
*/
async findTeamById(id: string) {
return this.prisma.team.findUnique({
where: { id },
});
}
/**
* Search teams by name
*/
async searchTeams(name: string, sport?: Sport) {
return this.prisma.team.findMany({
where: {
name: { contains: name, mode: 'insensitive' },
...(sport ? { sport } : {}),
},
take: 20,
});
}
/**
* Get team's matches (past + upcoming)
*/
async getTeamRecentMatches(teamId: string, limit: number = 50) {
return this.prisma.match.findMany({
where: {
OR: [{ homeTeamId: teamId }, { awayTeamId: teamId }],
},
include: {
homeTeam: true,
awayTeam: true,
league: { include: { country: true } },
},
orderBy: { mstUtc: 'desc' },
take: limit,
});
}
/**
* Get head-to-head matches between two teams
*/
async getHeadToHead(teamId1: string, teamId2: string, limit: number = 10) {
const matches = await this.prisma.match.findMany({
where: {
OR: [
{ homeTeamId: teamId1, awayTeamId: teamId2 },
{ homeTeamId: teamId2, awayTeamId: teamId1 },
],
state: 'postGame', // Finished matches are stored as "postGame"
},
include: {
homeTeam: true,
awayTeam: true,
league: true,
},
orderBy: { mstUtc: 'desc' },
take: limit,
});
// Calculate statistics
let team1Wins = 0;
let team2Wins = 0;
let draws = 0;
matches.forEach((match) => {
const homeScore = Number(match.scoreHome ?? -1);
const awayScore = Number(match.scoreAway ?? -1);
// Skip matches without scores
if (homeScore === -1 || awayScore === -1) return;
const isTeam1Home = match.homeTeamId === teamId1;
if (homeScore === awayScore) {
draws++;
} else if (
(isTeam1Home && homeScore > awayScore) ||
(!isTeam1Home && awayScore > homeScore)
) {
team1Wins++;
} else {
team2Wins++;
}
});
return {
matches,
team1Wins,
team2Wins,
draws,
};
}
}