update shipment method

This commit is contained in:
2025-11-23 16:45:15 +03:30
parent 08e2da0e5c
commit 0d72333c9c
4 changed files with 36 additions and 28 deletions
@@ -19,6 +19,6 @@ export class PublicRestaurantShipmentMethodController {
type: [RestaurantShipmentMethod],
})
findAll(@RestId() restId: string) {
return this.restaurantShipmentMethodService.findAll(restId);
return this.restaurantShipmentMethodService.findAllForRestaurantId(restId);
}
}
@@ -35,30 +35,30 @@ export class RestaurantShipmentMethodController {
}
@Get()
@ApiOperation({ summary: 'Get all restaurant shipment methods' })
@ApiOperation({ summary: 'Get the restaurant shipment methods' })
@ApiOkResponse({
description: 'List of restaurant shipment methods',
type: [RestaurantShipmentMethod],
})
findAll(@RestId() restId: string) {
return this.restaurantShipmentMethodService.findAll(restId);
return this.restaurantShipmentMethodService.findAllForRestaurantId(restId);
}
@Get(':shipmentMethodId')
@Get(':RestaurantShipmentMethodId')
@ApiOperation({ summary: 'Get a restaurant shipment method by shipment method ID' })
@ApiParam({ name: 'shipmentMethodId', description: 'Shipment method ID' })
@ApiParam({ name: 'RestaurantShipmentMethodId', description: 'RestaurantRestaurantShipmentMethodId method ID' })
@ApiOkResponse({
description: 'Restaurant shipment method details',
type: RestaurantShipmentMethod,
})
@ApiNotFoundResponse({ description: 'Restaurant shipment method not found' })
findOne(@Param('shipmentMethodId') shipmentMethodId: string, @RestId() restId: string) {
return this.restaurantShipmentMethodService.findOne(restId, shipmentMethodId);
findOne(@Param('RestaurantShipmentMethodId') RestaurantShipmentMethodId: string, @RestId() restId: string) {
return this.restaurantShipmentMethodService.findOne(restId, RestaurantShipmentMethodId);
}
@Patch(':shipmentMethodId')
@Patch(':RestaurantShipmentMethodId')
@ApiOperation({ summary: 'Update a restaurant shipment method' })
@ApiParam({ name: 'shipmentMethodId', description: 'Shipment method ID' })
@ApiParam({ name: 'RestaurantShipmentMethodId', description: 'Shipment method ID' })
@ApiBody({ type: UpdateRestaurantShipmentMethodDto })
@ApiOkResponse({
description: 'Restaurant shipment method updated successfully',
@@ -66,19 +66,19 @@ export class RestaurantShipmentMethodController {
})
@ApiNotFoundResponse({ description: 'Restaurant shipment method not found' })
update(
@Param('shipmentMethodId') shipmentMethodId: string,
@Param('RestaurantShipmentMethodId') RestaurantShipmentMethodId: string,
@Body() updateDto: UpdateRestaurantShipmentMethodDto,
@RestId() restId: string,
) {
return this.restaurantShipmentMethodService.update(restId, shipmentMethodId, updateDto);
return this.restaurantShipmentMethodService.update(restId, RestaurantShipmentMethodId, updateDto);
}
@Delete(':shipmentMethodId')
@Delete(':RestaurantShipmentMethodId')
@ApiOperation({ summary: 'Delete a restaurant shipment method' })
@ApiParam({ name: 'shipmentMethodId', description: 'Shipment method ID' })
@ApiParam({ name: 'RestaurantShipmentMethodId', description: 'Shipment method ID' })
@ApiOkResponse({ description: 'Restaurant shipment method deleted successfully' })
@ApiNotFoundResponse({ description: 'Restaurant shipment method not found' })
remove(@Param('shipmentMethodId') shipmentMethodId: string, @RestId() restId: string) {
return this.restaurantShipmentMethodService.remove(restId, shipmentMethodId);
remove(@Param('RestaurantShipmentMethodId') RestaurantShipmentMethodId: string, @RestId() restId: string) {
return this.restaurantShipmentMethodService.remove(restId, RestaurantShipmentMethodId);
}
}
@@ -49,24 +49,26 @@ export class RestaurantShipmentMethodService {
return restaurantShipmentMethod;
}
async findAll(restId: string): Promise<RestaurantShipmentMethod[]> {
async findAllForRestaurantId(restId: string): Promise<RestaurantShipmentMethod[]> {
return this.restaurantShipmentMethodRepository.find(
{ restaurant: { id: restId } },
{ populate: ['shipmentMethod'] },
);
}
async findOne(restId: string, shipmentMethodId: string): Promise<RestaurantShipmentMethod> {
async findOne(restId: string, restaurantShipmentMethodId: string): Promise<RestaurantShipmentMethod> {
const restaurantShipmentMethod = await this.restaurantShipmentMethodRepository.findOne(
{
id: restaurantShipmentMethodId,
restaurant: { id: restId },
shipmentMethod: { id: shipmentMethodId },
},
{ populate: ['shipmentMethod'] },
);
if (!restaurantShipmentMethod) {
throw new NotFoundException(`روش ارسال با شناسه ${shipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`);
throw new NotFoundException(
`Restaurant shipment method with ID ${restaurantShipmentMethodId} not found for restaurant with ID ${restId}`,
);
}
return restaurantShipmentMethod;
@@ -74,16 +76,18 @@ export class RestaurantShipmentMethodService {
async update(
restId: string,
shipmentMethodId: string,
restaurantShipmentMethodId: string,
updateDto: UpdateRestaurantShipmentMethodDto,
): Promise<RestaurantShipmentMethod> {
const restaurantShipmentMethod = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
shipmentMethod: { id: shipmentMethodId },
id: restaurantShipmentMethodId,
});
if (!restaurantShipmentMethod) {
throw new NotFoundException(`روش ارسال با شناسه ${shipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`);
throw new NotFoundException(
`روش ارسال با شناسه ${restaurantShipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`,
);
}
const updateData: Partial<RestaurantShipmentMethod> = {};
@@ -99,7 +103,10 @@ export class RestaurantShipmentMethodService {
}
// If shipmentMethodId is being updated, we need to check for conflicts
if (updateDto.shipmentMethodId !== undefined && updateDto.shipmentMethodId !== shipmentMethodId) {
if (
updateDto.shipmentMethodId !== undefined &&
updateDto.shipmentMethodId !== restaurantShipmentMethod.shipmentMethod.id
) {
const newShipmentMethod = await this.em.findOne(ShipmentMethod, { id: updateDto.shipmentMethodId });
if (!newShipmentMethod) {
throw new NotFoundException(`روش ارسال با شناسه ${updateDto.shipmentMethodId} یافت نشد.`);
@@ -108,7 +115,7 @@ export class RestaurantShipmentMethodService {
// Check if the new relationship already exists
const existing = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
shipmentMethod: { id: updateDto.shipmentMethodId },
shipmentMethod: { id: { $ne: restaurantShipmentMethodId } },
});
if (existing) {
@@ -127,14 +134,16 @@ export class RestaurantShipmentMethodService {
return restaurantShipmentMethod;
}
async remove(restId: string, shipmentMethodId: string): Promise<void> {
async remove(restId: string, restaurantShipmentMethodId: string): Promise<void> {
const restaurantShipmentMethod = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
shipmentMethod: { id: shipmentMethodId },
id: restaurantShipmentMethodId,
});
if (!restaurantShipmentMethod) {
throw new NotFoundException(`روش ارسال با شناسه ${shipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`);
throw new NotFoundException(
`روش ارسال با شناسه ${restaurantShipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`,
);
}
await this.em.removeAndFlush(restaurantShipmentMethod);
@@ -8,4 +8,3 @@ export class RestaurantShipmentMethodRepository extends EntityRepository<Restaur
super(em, RestaurantShipmentMethod);
}
}