clean
This commit is contained in:
@@ -24,15 +24,15 @@ import { Permissions } from 'src/common/decorators/permissions.decorator';
|
||||
@ApiTags('Delivery')
|
||||
@Controller()
|
||||
export class DeliveryController {
|
||||
constructor(private readonly deliveryService: DeliveryService) {}
|
||||
constructor(private readonly deliveryService: DeliveryService) { }
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('public/delivery-methods/restaurant')
|
||||
@ApiOperation({ summary: 'Get restaurant delivery methods' })
|
||||
@ApiHeader(API_HEADER_SLUG)
|
||||
findAllDeliveryMethods(@RestId() restId: string) {
|
||||
return this.deliveryService.findAllForRestaurantId(restId);
|
||||
findAllDeliveryMethods(@RestId() restId: string) {
|
||||
return this.deliveryService.findByRestaurantId(restId);
|
||||
}
|
||||
|
||||
/*** Admin ***/
|
||||
@@ -52,7 +52,7 @@ export class DeliveryController {
|
||||
@Get('admin/delivery-methods/restaurant')
|
||||
@ApiOperation({ summary: 'Get the restaurant delivery methods' })
|
||||
findRestaurantDeliveryMethods(@RestId() restId: string) {
|
||||
return this.deliveryService.findAllForRestaurantId(restId);
|
||||
return this.deliveryService.findByRestaurantId(restId);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@@ -62,7 +62,7 @@ export class DeliveryController {
|
||||
@ApiOperation({ summary: 'Get a restaurant delivery method by delivery ID' })
|
||||
@ApiParam({ name: 'deliveryId', description: 'Delivery method ID' })
|
||||
findOne(@Param('deliveryId') deliveryId: string, @RestId() restId: string) {
|
||||
return this.deliveryService.findOne(restId, deliveryId);
|
||||
return this.deliveryService.findOneOrFail(restId, deliveryId);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
|
||||
@@ -4,23 +4,20 @@ import { Delivery } from '../entities/delivery.entity';
|
||||
import { DeliveryRepository } from '../repositories/delivery.repository';
|
||||
import { CreateDeliveryDto } from '../dto/create-delivery.dto';
|
||||
import { UpdateDeliveryDto } from '../dto/update-delivery.dto';
|
||||
import { RestRepository } from '../../restaurants/repositories/rest.repository';
|
||||
import { DeliveryMethodEnum } from '../interface/delivery';
|
||||
import { DeliveryMessage } from 'src/common/enums/message.enum';
|
||||
import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service';
|
||||
|
||||
@Injectable()
|
||||
export class DeliveryService {
|
||||
constructor(
|
||||
private readonly deliveryRepository: DeliveryRepository,
|
||||
private readonly restRepository: RestRepository,
|
||||
private readonly restService: RestaurantsService,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
async create(restId: string, createDto: CreateDeliveryDto): Promise<Delivery> {
|
||||
const restaurant = await this.restRepository.findOne({ id: restId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(DeliveryMessage.RESTAURANT_NOT_FOUND);
|
||||
}
|
||||
const restaurant = await this.restService.findOneOrFail(restId)
|
||||
|
||||
// Check if the delivery method with the same name already exists for this restaurant
|
||||
const existing = await this.deliveryRepository.findOne({
|
||||
@@ -50,11 +47,11 @@ export class DeliveryService {
|
||||
return delivery;
|
||||
}
|
||||
|
||||
async findAllForRestaurantId(restId: string): Promise<Delivery[]> {
|
||||
async findByRestaurantId(restId: string): Promise<Delivery[]> {
|
||||
return this.deliveryRepository.find({ restaurant: { id: restId } }, { orderBy: { order: 'asc' } });
|
||||
}
|
||||
|
||||
async findOne(restId: string, deliveryId: string): Promise<Delivery> {
|
||||
async findOneOrFail(restId: string, deliveryId: string): Promise<Delivery> {
|
||||
const delivery = await this.deliveryRepository.findOne({
|
||||
id: deliveryId,
|
||||
restaurant: { id: restId },
|
||||
@@ -68,15 +65,7 @@ export class DeliveryService {
|
||||
}
|
||||
|
||||
async update(restId: string, deliveryId: string, updateDto: UpdateDeliveryDto): Promise<Delivery> {
|
||||
const delivery = await this.deliveryRepository.findOne({
|
||||
restaurant: { id: restId },
|
||||
id: deliveryId,
|
||||
});
|
||||
|
||||
if (!delivery) {
|
||||
throw new NotFoundException(DeliveryMessage.DELIVERY_METHOD_NOT_FOUND);
|
||||
}
|
||||
|
||||
const delivery = await this.findOneOrFail(restId, deliveryId)
|
||||
// If method is being updated, check for conflicts
|
||||
if (updateDto.method !== undefined && updateDto.method !== delivery.method) {
|
||||
const existing = await this.deliveryRepository.findOne({
|
||||
@@ -97,14 +86,7 @@ export class DeliveryService {
|
||||
}
|
||||
|
||||
async remove(restId: string, deliveryId: string): Promise<void> {
|
||||
const delivery = await this.deliveryRepository.findOne({
|
||||
restaurant: { id: restId },
|
||||
id: deliveryId,
|
||||
});
|
||||
|
||||
if (!delivery) {
|
||||
throw new NotFoundException(DeliveryMessage.DELIVERY_METHOD_NOT_FOUND);
|
||||
}
|
||||
const delivery = await this.findOneOrFail(restId, deliveryId)
|
||||
|
||||
await this.em.removeAndFlush(delivery);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export class RestaurantsController {
|
||||
@ApiOperation({ summary: 'Get restaurant by ID from request' })
|
||||
@Get('admin/restaurants/my-restaurant')
|
||||
async findOne(@RestId() restId: string) {
|
||||
return await this.restaurantsService.findOne(restId);
|
||||
return await this.restaurantsService.findOneOrFail(restId);
|
||||
}
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@@ -96,7 +96,7 @@ export class RestaurantsController {
|
||||
@ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
|
||||
@Get('super-admin/restaurants/:id')
|
||||
findOneById(@Param('id') id: string) {
|
||||
return this.restaurantsService.findOne(id);
|
||||
return this.restaurantsService.findOneOrFail(id);
|
||||
}
|
||||
|
||||
@UseGuards(SuperAdminAuthGuard)
|
||||
|
||||
@@ -128,7 +128,7 @@ export class RestaurantsService {
|
||||
return restaurant;
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<Restaurant> {
|
||||
async findOneOrFail(id: string): Promise<Restaurant> {
|
||||
const restaurant = await this.restRepository.findOne({ id });
|
||||
|
||||
if (!restaurant) {
|
||||
|
||||
@@ -16,6 +16,6 @@ import { AuthModule } from '../auth/auth.module';
|
||||
controllers: [RestaurantsController, ScheduleController],
|
||||
providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService, RestaurantCrone],
|
||||
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)],
|
||||
exports: [RestRepository, ScheduleRepository, ScheduleService],
|
||||
exports: [RestRepository, ScheduleRepository, ScheduleService, RestaurantsService],
|
||||
})
|
||||
export class RestaurantsModule {}
|
||||
export class RestaurantsModule { }
|
||||
|
||||
Reference in New Issue
Block a user