Skip to content

Commit f706bd6

Browse files
committed
Added Schedule module
1 parent 162de67 commit f706bd6

File tree

7 files changed

+116
-9
lines changed

7 files changed

+116
-9
lines changed

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { BookingModule } from "./booking/booking.module";
66
import { validateEnvs } from "./env.validation";
77
import { LocationModule } from "./location/location.module";
88
import { PrismaModule } from "./prisma/prisma.module";
9+
import { ScheduleModule } from "./schedule/schedule.module";
910
import { ServiceModule } from "./service/service.module";
1011
import { UserModule } from "./user/user.module";
1112

@@ -20,6 +21,7 @@ import { UserModule } from "./user/user.module";
2021
UserModule,
2122
LocationModule,
2223
ServiceModule,
24+
ScheduleModule,
2325
PrismaModule,
2426
BookingModule
2527
],

src/booking/booking.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class BookingController {
4545
}
4646

4747
/**
48-
* Get a user's booking with the specified `id`
48+
* Get a user's booking with the specified id
4949
*/
5050
@Get(":id")
5151
async findOne(@Param("id", ParseIntPipe) id: number, @User() user: UserEntity) {
@@ -72,7 +72,7 @@ export class BookingController {
7272
}
7373

7474
/**
75-
* Update a booking with the specified `id`
75+
* Update a booking with the specified id
7676
*/
7777
@Patch(":id")
7878
async update(@Param("id", ParseIntPipe) id: number, @Body() data: UpdateBookingDto) {
@@ -85,7 +85,7 @@ export class BookingController {
8585
}
8686

8787
/**
88-
* Delete a booking with the specified `id`
88+
* Delete a booking with the specified id
8989
*/
9090
@Delete(":id")
9191
@UseGuards(RolesGuard)

src/location/location.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class LocationController {
4444
}
4545

4646
/**
47-
* Get a location with the specified `id`
47+
* Get a location with the specified id
4848
*/
4949
@Get(":id")
5050
async findOne(@Param("id", ParseIntPipe) id: number) {
@@ -73,7 +73,7 @@ export class LocationController {
7373
}
7474

7575
/**
76-
* Update a location with the specified `id`
76+
* Update a location with the specified id
7777
*/
7878
@Patch(":id")
7979
@UseGuards(JwtAuthGuard, RolesGuard)
@@ -88,7 +88,7 @@ export class LocationController {
8888
}
8989

9090
/**
91-
* Delete a location with the specified `id`
91+
* Delete a location with the specified id
9292
*/
9393
@Delete(":id")
9494
@UseGuards(JwtAuthGuard, RolesGuard)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
BadRequestException,
3+
Body,
4+
ClassSerializerInterceptor,
5+
Controller,
6+
Get,
7+
Param,
8+
ParseIntPipe,
9+
Patch,
10+
UseGuards,
11+
UseInterceptors
12+
} from "@nestjs/common";
13+
import { ApiTags } from "@nestjs/swagger";
14+
import { UserRole } from "@prisma/client";
15+
16+
import { JwtAuthGuard } from "../auth/guards/jwt.guard";
17+
import { Roles } from "../decorators/roles.decorator";
18+
import { RolesGuard } from "../guards/roles.guard";
19+
import { getErrorMessage } from "../utils/get-error-message";
20+
import { ScheduleEntity, UpdateScheduleDto } from "./schedule.dto";
21+
import { ScheduleService } from "./schedule.service";
22+
23+
@ApiTags("Schedule")
24+
@Controller("schedule")
25+
@UseInterceptors(ClassSerializerInterceptor)
26+
export class ScheduleController {
27+
constructor(private readonly scheduleService: ScheduleService) {}
28+
29+
/**
30+
* Get a schedule for a service with the specified id
31+
*/
32+
@Get()
33+
async findAll(@Param("serviceId", ParseIntPipe) serviceId: number) {
34+
const schedule = await this.scheduleService.findSchedule({ serviceId });
35+
return schedule.map((s) => new ScheduleEntity(s));
36+
}
37+
38+
/**
39+
* Update a schedule with the specified id
40+
*/
41+
@Patch(":id")
42+
@UseGuards(JwtAuthGuard, RolesGuard)
43+
@Roles(UserRole.ADMIN, UserRole.MANAGER)
44+
async update(@Param("id", ParseIntPipe) id: number, @Body() data: UpdateScheduleDto) {
45+
try {
46+
return new ScheduleEntity(await this.scheduleService.updateSchedule(id, data));
47+
} catch (e) {
48+
console.error(getErrorMessage(e));
49+
throw new BadRequestException("Failed to update schedule.");
50+
}
51+
}
52+
}

src/schedule/schedule.module.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Module } from "@nestjs/common";
2+
3+
import { PrismaModule } from "../prisma/prisma.module";
4+
import { ScheduleController } from "./schedule.controller";
5+
import { ScheduleService } from "./schedule.service";
6+
7+
@Module({
8+
imports: [PrismaModule],
9+
providers: [ScheduleService],
10+
controllers: [ScheduleController],
11+
exports: [ScheduleService]
12+
})
13+
export class ScheduleModule {}

src/schedule/schedule.service.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Injectable } from "@nestjs/common";
2+
import { Prisma } from "@prisma/client";
3+
4+
import { PrismaService } from "../prisma/prisma.service";
5+
import { CreateScheduleDto, UpdateScheduleDto } from "./schedule.dto";
6+
7+
@Injectable()
8+
export class ScheduleService {
9+
constructor(private prisma: PrismaService) {}
10+
11+
private buildScheduleData(serviceId: number) {
12+
return new Array(7).fill(null).map(
13+
(_, index): CreateScheduleDto => ({
14+
day: index,
15+
startTime: null,
16+
endTime: null,
17+
serviceId
18+
})
19+
);
20+
}
21+
22+
async findSchedule(where: Prisma.ServiceScheduleWhereInput) {
23+
return this.prisma.serviceSchedule.findMany({
24+
where
25+
});
26+
}
27+
28+
async createScheduleForService(serviceId: number, tx?: Prisma.TransactionClient) {
29+
return (tx ?? this.prisma).serviceSchedule.createManyAndReturn({
30+
data: this.buildScheduleData(serviceId)
31+
});
32+
}
33+
34+
async updateSchedule(id: number, data: UpdateScheduleDto) {
35+
return this.prisma.serviceSchedule.update({
36+
where: { id },
37+
data
38+
});
39+
}
40+
}

src/service/service.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class ServiceController {
4242
}
4343

4444
/**
45-
* Get a service with the specified `id`
45+
* Get a service with the specified id
4646
*/
4747
@Get(":id")
4848
async findOne(@Param("id", ParseIntPipe) id: number) {
@@ -71,7 +71,7 @@ export class ServiceController {
7171
}
7272

7373
/**
74-
* Update a service with the specified `id`
74+
* Update a service with the specified id
7575
*/
7676
@Patch(":id")
7777
@UseGuards(JwtAuthGuard, RolesGuard)
@@ -86,7 +86,7 @@ export class ServiceController {
8686
}
8787

8888
/**
89-
* Delete a service with the specified `id`
89+
* Delete a service with the specified id
9090
*/
9191
@Delete(":id")
9292
@UseGuards(JwtAuthGuard, RolesGuard)

0 commit comments

Comments
 (0)