Skip to content

Commit

Permalink
Merge pull request #21 from p-society/crudapi
Browse files Browse the repository at this point in the history
review
  • Loading branch information
majorbruteforce authored Dec 25, 2024
2 parents f8e754a + 5c3eb96 commit f2e1fc3
Show file tree
Hide file tree
Showing 43 changed files with 1,140 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import { ReactionsModule } from './services/apis/reactions/reactions.module';
import { ProfilesModule } from './services/apis/profiles/profiles.module';
import { QueueModule } from './services/bullmq/queue.module';
import { BullModule } from '@nestjs/bullmq';
import { MatchesModule } from './services/apis/matches/matches.module';
import { SquadModule } from './services/apis/squad/squad.module';
import { SquadPlayerModule } from './services/apis/squadPlayer/squadPlayer.module';
import { TeamModule } from './services/apis/team/team.module';
import { TeamPlayerModule } from './services/apis/teamPlayer/teamPlayer.module';
import { GenerateOtpModule } from './services/apis/otp/generateOtp.module';
import { MailerModule } from './services/apis/mailer/mailer.module';

Expand Down Expand Up @@ -43,6 +48,11 @@ import { MailerModule } from './services/apis/mailer/mailer.module';
AuthModule,
UsersModule,
AdapterModule,
MatchesModule,
SquadModule,
SquadPlayerModule,
TeamModule,
TeamPlayerModule,
ProfilesModule,
ReactionsModule,
GenerateOtpModule,
Expand Down
7 changes: 7 additions & 0 deletions src/constants/branches-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum Branches {
Cse = 'CSE',
Etc = 'ETC',
Eee = 'EEE',
It = 'IT',
Ce = 'CE',
}
16 changes: 8 additions & 8 deletions src/constants/sports-enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum SportsEnum {
enum Sports {
Football = 'Football',
Cricket = 'Cricket',
Badminton = 'Badminton',
Expand All @@ -7,12 +7,12 @@ enum SportsEnum {
}

export const SportsEnumList = [
SportsEnum.Football,
SportsEnum.Badminton,
SportsEnum.Basketball,
SportsEnum.Cricket,
SportsEnum.Football,
SportsEnum.Volleyball,
Sports.Football,
Sports.Badminton,
Sports.Basketball,
Sports.Cricket,
Sports.Football,
Sports.Volleyball,
];

export default SportsEnum;
export default Sports;
27 changes: 27 additions & 0 deletions src/services/apis/matches/constants/MatchEnums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export enum MatchStatus {
Live = 'LIVE',
Upcoming = 'UPCOMING',
Concluded = 'CONCLUDED',
Paused = 'PAUSED',
Postponed = 'POSTPONED',
Cancelled = 'CANCELLED',
}

export enum MatchOutcome {
Team1 = 'TEAM1',
Team2 = 'TEAM2',
Draw = 'DRAW',
NoResult = 'NO_RESULT',
}

export enum MatchIncompletionReason {
BadWeather = 'BAD_WEATHER',
InsufficientTime = 'INSUFFICIENT_TIME',
Other = 'OTHER',
}

export enum MatchStage {
LEAGUE = 'LEAGUE',
QUALIFIER = 'QUALIFIER',
FINAL = 'FINAL',
}
99 changes: 99 additions & 0 deletions src/services/apis/matches/dto/matches.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { z } from 'zod';
import { Types } from 'mongoose';
import {
MatchIncompletionReason,
MatchOutcome,
MatchStage,
MatchStatus,
} from '../constants/MatchEnums';

const StageEnum = z.nativeEnum(MatchStage);
const StatusEnum = z.nativeEnum(MatchStatus);
const OutcomeEnum = z.nativeEnum(MatchOutcome);
const IncompletionReasonEnum = z.nativeEnum(MatchIncompletionReason);

export const CreateMatchesValidation = z.object({
scheduledFor: z.string(),
team1: z.string().refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid team1 ID',
}),
team2: z.string().refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid team2 ID',
}),
sport: z.string().refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid sport ID',
}),
stage: StageEnum,
status: StatusEnum,
outcome: OutcomeEnum.optional(),
reasonForIncompletion: IncompletionReasonEnum.optional(),
createdBy: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid creator ID',
})
.optional(),
createdAt: z.date().optional(),
updatedAt: z.date().optional(),
deleted: z.boolean().optional().default(false),
deletedBy: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid deleter ID',
})
.optional(),
deletedAt: z.date().optional(),
});

export const PatchMatchesValidation = z.object({
scheduledFor: z.string().optional(),
team1: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid team1 ID',
})
.optional(),
team2: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid team2 ID',
})
.optional(),
sport: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid sport ID',
})
.optional(),
stage: StageEnum.optional(),
status: StatusEnum.optional(),
outcome: OutcomeEnum.optional(),
reasonForIncompletion: IncompletionReasonEnum.optional(),
updatedAt: z.date().optional(),
createdAt: z.date().optional(),
deleted: z.boolean().optional(),
deletedBy: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid deleter ID',
})
.optional(),
deletedAt: z.date().optional(),
});

export const RemoveMatchesValidation = z.object({
id: z.string().refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid matches ID',
}),
deletedBy: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid deleter ID',
})
.optional(),
deletedAt: z.date().optional(),
});

export type CreateMatchesDto = z.infer<typeof CreateMatchesValidation>;
export type PatchMatchesDto = z.infer<typeof PatchMatchesValidation>;
export type RemoveMatchesDto = z.infer<typeof RemoveMatchesValidation>;
18 changes: 18 additions & 0 deletions src/services/apis/matches/matches.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MatchesController } from './matches.controller';

describe('MatchesController', () => {
let controller: MatchesController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [MatchesController],
}).compile();

controller = module.get<MatchesController>(MatchesController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
48 changes: 48 additions & 0 deletions src/services/apis/matches/matches.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
} from '@nestjs/common';
import { MatchesService } from './matches.service';
import { ModifyBody, setCreatedBy } from 'src/decorators/ModifyBody.decorator';
import { User } from '../users/decorator/user.decorator';
import { Matches } from './schemas/matches.schema';

@Controller('matches')
export class MatchesController {
constructor(private readonly matchesService: MatchesService) {}

@Get()
async find(@Query() query: Record<string, any>) {
return await this.matchesService._find(query);
}

@Get('/:id?')
async get(@Query() query: Record<string, any>, @Param('id') id: string) {
return await this.matchesService._get(id, query);
}

@Post()
async create(@ModifyBody(setCreatedBy()) createMatchesDto: Matches) {
return await this.matchesService._create(createMatchesDto);
}

@Patch('/:id?')
async patch(
@Query() query,
@Body() patchMatchesDto: Partial<Matches>,
@Param('id') id,
) {
return await this.matchesService._patch(id, patchMatchesDto, query);
}

@Delete('/:id?')
async delete(@Param('id') id, @Query() query, @User() user) {
return await this.matchesService._remove(id, query, user);
}
}
14 changes: 14 additions & 0 deletions src/services/apis/matches/matches.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MatchesController } from './matches.controller';
import { MatchesService } from './matches.service';
import { Matches, MatchesSchema } from './schemas/matches.schema';
@Module({
imports: [
MongooseModule.forFeature([{ name: Matches.name, schema: MatchesSchema }]),
],
controllers: [MatchesController],
providers: [MatchesService],
exports: [MatchesService],
})
export class MatchesModule {}
18 changes: 18 additions & 0 deletions src/services/apis/matches/matches.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MatchesService } from './matches.service';

describe('MatchesService', () => {
let service: MatchesService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MatchesService],
}).compile();

service = module.get<MatchesService>(MatchesService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
15 changes: 15 additions & 0 deletions src/services/apis/matches/matches.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { GlobalService } from 'src/common/global-service';
import { Matches, MatchesDocument } from './schemas/matches.schema';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

@Injectable()
export class MatchesService extends GlobalService<Matches, MatchesDocument> {
constructor(
@InjectModel(Matches.name)
private readonly matchesModel: Model<MatchesDocument>,
) {
super(matchesModel);
}
}
71 changes: 71 additions & 0 deletions src/services/apis/matches/schemas/matches.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Types } from 'mongoose';
import {
MatchIncompletionReason,
MatchOutcome,
MatchStage,
MatchStatus,
} from '../constants/MatchEnums';
import { Teams } from '../../team/schemas/team.schema';

export type MatchesDocument = HydratedDocument<Matches>;

@Schema({
timestamps: true,
})
export class Matches {
@Prop({ type: Date, required: true })
scheduledFor: Date;

@Prop({ type: Date })
concludedAt: Date;

@Prop({
type: Types.ObjectId,
ref: Teams.name,
required: true,
})
team1: Types.ObjectId;

@Prop({
type: Types.ObjectId,
ref: Teams.name,
required: true,
})
team2: Types.ObjectId;

@Prop({
type: Types.ObjectId,
// ref: Sports.name // Include when sports entity is defined
required: true,
})
sport: Types.ObjectId;

@Prop({
type: String,
enum: Object.values(MatchStage),
required: true,
})
stage: MatchStage;

@Prop({
type: String,
enum: Object.values(MatchStatus),
default: MatchStatus.Upcoming,
})
status: MatchStatus;

@Prop({
type: String,
enum: Object.values(MatchOutcome),
})
outcome: MatchOutcome;

@Prop({
type: String,
enum: Object.values(MatchIncompletionReason),
})
reasonForIncompletion: MatchIncompletionReason;
}

export const MatchesSchema = SchemaFactory.createForClass(Matches);
6 changes: 3 additions & 3 deletions src/services/apis/profiles/dto/profiles.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { date, z } from 'zod';
import { Types } from 'mongoose';
import SportsEnum from 'src/constants/sports-enum';
import Sports from 'src/constants/sports-enum';

export const CreateProfilesValidation = z.object({
sport: z.nativeEnum(SportsEnum),
sport: z.nativeEnum(Sports),
user: z.string().refine((val) => Types.ObjectId.isValid(val), {
message: 'Invalid user ID',
}),
Expand Down Expand Up @@ -32,7 +32,7 @@ export const CreateProfilesValidation = z.object({
});

export const PatchProfilesValidation = z.object({
sport: z.nativeEnum(SportsEnum).optional(),
sport: z.nativeEnum(Sports).optional(),
user: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
Expand Down
Loading

0 comments on commit f2e1fc3

Please sign in to comment.