shipment methods
This commit is contained in:
@@ -41,16 +41,21 @@ export class AdminService {
|
|||||||
const { phone, firstName, lastName, roleId } = dto;
|
const { phone, firstName, lastName, roleId } = dto;
|
||||||
const currentAdmin = await this.adminRepository.findOne({
|
const currentAdmin = await this.adminRepository.findOne({
|
||||||
phone,
|
phone,
|
||||||
roles: { restaurant: { id: restId } },
|
|
||||||
});
|
});
|
||||||
if (currentAdmin) {
|
if (!currentAdmin) {
|
||||||
throw new ConflictException('This Phone Number is already Admin for this restaurant');
|
await this.adminRepository.findOne({
|
||||||
|
phone,
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const role = await this.em.findOne(Role, { id: roleId });
|
const role = await this.em.findOne(Role, { id: roleId });
|
||||||
if (!role) throw new Error('Role not found');
|
if (!role) throw new Error('Role not found');
|
||||||
|
|
||||||
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||||
if (!restaurant) throw new Error('Restaurant not found');
|
if (!restaurant) throw new Error('Restaurant not found');
|
||||||
const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
|
const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
|
||||||
|
|
||||||
await this.em.persistAndFlush(admin);
|
await this.em.persistAndFlush(admin);
|
||||||
return admin;
|
return admin;
|
||||||
}
|
}
|
||||||
@@ -101,7 +106,11 @@ export class AdminService {
|
|||||||
if (rest.lastName !== undefined) {
|
if (rest.lastName !== undefined) {
|
||||||
admin.lastName = rest.lastName;
|
admin.lastName = rest.lastName;
|
||||||
}
|
}
|
||||||
if (rest.phone !== undefined) {
|
if (rest.phone !== undefined && rest.phone !== admin.phone) {
|
||||||
|
const exists = await this.adminRepository.findOne({ phone: rest.phone });
|
||||||
|
if (exists) {
|
||||||
|
throw new ConflictException('This Phone Number is already Admin for this restaurant');
|
||||||
|
}
|
||||||
admin.phone = rest.phone;
|
admin.phone = rest.phone;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +135,6 @@ export class AdminService {
|
|||||||
if (existingAdminRole) {
|
if (existingAdminRole) {
|
||||||
// Update existing role
|
// Update existing role
|
||||||
existingAdminRole.role = role;
|
existingAdminRole.role = role;
|
||||||
await this.em.persistAndFlush(existingAdminRole);
|
|
||||||
} else {
|
} else {
|
||||||
// Create new AdminRole
|
// Create new AdminRole
|
||||||
const newAdminRole = this.em.create(AdminRole, {
|
const newAdminRole = this.em.create(AdminRole, {
|
||||||
@@ -135,7 +143,6 @@ export class AdminService {
|
|||||||
restaurant,
|
restaurant,
|
||||||
});
|
});
|
||||||
admin.roles.add(newAdminRole);
|
admin.roles.add(newAdminRole);
|
||||||
await this.em.persistAndFlush(newAdminRole);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,8 +155,6 @@ export class AdminService {
|
|||||||
if (!adminRole) {
|
if (!adminRole) {
|
||||||
throw new NotFoundException('Admin role not found');
|
throw new NotFoundException('Admin role not found');
|
||||||
}
|
}
|
||||||
|
return this.em.removeAndFlush(adminRole);
|
||||||
await this.em.nativeDelete(AdminRole, { id: adminRole.id });
|
|
||||||
await this.em.persistAndFlush(adminRole);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
|
||||||
|
import { ApiTags, ApiOperation, ApiOkResponse, ApiNotFoundResponse, ApiParam, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
|
import { ShipmentMethod } from '../entities/shipment-method.entity';
|
||||||
|
import { ShipmentMethodsService } from '../providers/shipment-methods.service';
|
||||||
|
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@ApiTags('admin/shipment-methods')
|
||||||
|
@Controller('admin/shipment-methods')
|
||||||
|
export class ShipmentMethodController {
|
||||||
|
constructor(private readonly shipmentMethodsService: ShipmentMethodsService) {}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
@ApiOperation({ summary: 'Get all shipment methods' })
|
||||||
|
@ApiOkResponse({
|
||||||
|
description: 'List of shipment methods',
|
||||||
|
type: [ShipmentMethod],
|
||||||
|
})
|
||||||
|
findAll() {
|
||||||
|
return this.shipmentMethodsService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get(':shipmentMethodId')
|
||||||
|
@ApiOperation({ summary: 'Get a shipment method by shipment method ID' })
|
||||||
|
@ApiParam({ name: 'shipmentMethodId', description: 'Shipment method ID' })
|
||||||
|
@ApiOkResponse({
|
||||||
|
description: 'shipment method details',
|
||||||
|
type: ShipmentMethod,
|
||||||
|
})
|
||||||
|
@ApiNotFoundResponse({ description: 'Shipment method not found' })
|
||||||
|
findOne(@Param('shipmentMethodId') shipmentMethodId: string) {
|
||||||
|
return this.shipmentMethodsService.findOne(shipmentMethodId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
|
|
||||||
|
import { ShipmentMethodRepository } from '../repositories/shipment-method.repository';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ShipmentMethodsService {
|
||||||
|
constructor(
|
||||||
|
private readonly em: EntityManager,
|
||||||
|
private readonly shipmentMethodRepository: ShipmentMethodRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
findAll() {
|
||||||
|
return this.shipmentMethodRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
findOne(id: string) {
|
||||||
|
return this.shipmentMethodRepository.findOne({ id });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { ShipmentMethod } from '../entities/shipment-method.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ShipmentMethodRepository extends EntityRepository<ShipmentMethod> {
|
||||||
|
constructor(readonly em: EntityManager) {
|
||||||
|
super(em, ShipmentMethod);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,9 @@ import { RestaurantShipmentMethodRepository } from './repositories/restaurant-sh
|
|||||||
import { JwtModule } from '@nestjs/jwt';
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
import { AuthModule } from '../auth/auth.module';
|
import { AuthModule } from '../auth/auth.module';
|
||||||
import { PublicRestaurantShipmentMethodController } from './controllers/public-restaurant-shipment-method.controller';
|
import { PublicRestaurantShipmentMethodController } from './controllers/public-restaurant-shipment-method.controller';
|
||||||
|
import { ShipmentMethodController } from './controllers/shipment-method.controller';
|
||||||
|
import { ShipmentMethodsService } from './providers/shipment-methods.service';
|
||||||
|
import { ShipmentMethodRepository } from './repositories/shipment-method.repository';
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [
|
controllers: [
|
||||||
RestaurantsController,
|
RestaurantsController,
|
||||||
@@ -27,6 +29,7 @@ import { PublicRestaurantShipmentMethodController } from './controllers/public-r
|
|||||||
PublicScheduleController,
|
PublicScheduleController,
|
||||||
RestaurantShipmentMethodController,
|
RestaurantShipmentMethodController,
|
||||||
PublicRestaurantShipmentMethodController,
|
PublicRestaurantShipmentMethodController,
|
||||||
|
ShipmentMethodController,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
RestaurantsService,
|
RestaurantsService,
|
||||||
@@ -35,12 +38,14 @@ import { PublicRestaurantShipmentMethodController } from './controllers/public-r
|
|||||||
ScheduleService,
|
ScheduleService,
|
||||||
RestaurantShipmentMethodService,
|
RestaurantShipmentMethodService,
|
||||||
RestaurantShipmentMethodRepository,
|
RestaurantShipmentMethodRepository,
|
||||||
|
ShipmentMethodsService,
|
||||||
|
ShipmentMethodRepository,
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
MikroOrmModule.forFeature([Restaurant, Schedule, ShipmentMethod, RestaurantShipmentMethod]),
|
MikroOrmModule.forFeature([Restaurant, Schedule, ShipmentMethod, RestaurantShipmentMethod]),
|
||||||
JwtModule,
|
JwtModule,
|
||||||
forwardRef(() => AuthModule),
|
forwardRef(() => AuthModule),
|
||||||
],
|
],
|
||||||
exports: [RestRepository, ScheduleRepository, ScheduleService],
|
exports: [RestRepository, ScheduleRepository, ScheduleService, ShipmentMethodsService],
|
||||||
})
|
})
|
||||||
export class RestaurantsModule {}
|
export class RestaurantsModule {}
|
||||||
|
|||||||
Reference in New Issue
Block a user