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