request
This commit is contained in:
@@ -5,6 +5,9 @@ import { UpdateRequestDto } from './dto/update-request.dto';
|
||||
import { AuthGuard } from '../auth/guards/auth.guard';
|
||||
import { UserId } from 'src/common/decorators';
|
||||
import { FindRequestsDto } from './dto/find-requests.dto';
|
||||
import { AdminAuthGuard } from '../auth/guards/adminAuth.guard';
|
||||
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
||||
import { PermissionEnum } from 'src/common/enums/permission.enum';
|
||||
|
||||
@Controller()
|
||||
export class RequestController {
|
||||
@@ -30,8 +33,8 @@ export class RequestController {
|
||||
|
||||
@Patch('public/request/:id')
|
||||
@UseGuards(AuthGuard)
|
||||
update(@Param('id') id: string, @Body() updateRequestDto: UpdateRequestDto, @UserId() userId: string) {
|
||||
return this.requestService.update(id, updateRequestDto, userId);
|
||||
update(@Param('id') id: string, @Body() dto: UpdateRequestDto, @UserId() userId: string) {
|
||||
return this.requestService.update(id, dto, userId);
|
||||
}
|
||||
|
||||
@Delete('public/request/:id')
|
||||
@@ -39,4 +42,33 @@ export class RequestController {
|
||||
remove(@Param('id') id: string, @UserId() userId: string) {
|
||||
return this.requestService.remove(id, userId);
|
||||
}
|
||||
|
||||
//-----------------ADmin Routes -------------------
|
||||
@Get('admin/request')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@Permissions(PermissionEnum.VIEW_REQUESTS)
|
||||
findAllAsAdmin(@Query() dto: FindRequestsDto) {
|
||||
return this.requestService.findAllAsAdmin(dto);
|
||||
}
|
||||
|
||||
@Get('admin/request/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@Permissions(PermissionEnum.VIEW_REQUESTS)
|
||||
findOneAsAdmin(@Param('id') id: string) {
|
||||
return this.requestService.findOneAsAdmin(id);
|
||||
}
|
||||
|
||||
@Patch('public/request/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@Permissions(PermissionEnum.UPDATE_REQUEST)
|
||||
updateRequestAsAdmin(@Param('id') id: string, @Body() dto: UpdateRequestDto) {
|
||||
return this.requestService.updateAsAdmin(id, dto);
|
||||
}
|
||||
|
||||
@Delete('public/request/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@Permissions(PermissionEnum.DELETE_REQUEST)
|
||||
removeRequestAsAdmin(@Param('id') id: string) {
|
||||
return this.requestService.removeAsAdmin(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export class RequestService {
|
||||
private readonly requestRepository: RequestRepository,
|
||||
private readonly userService: UserService,
|
||||
private readonly productService: ProductService,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
async create(createRequestDto: CreateRequestDto, userId: string) {
|
||||
const { items } = createRequestDto;
|
||||
@@ -53,6 +53,10 @@ export class RequestService {
|
||||
return this.requestRepository.findAllPaginated({ userId, ...dto });
|
||||
}
|
||||
|
||||
findAllAsAdmin(dto: FindRequestsDto) {
|
||||
return this.requestRepository.findAllPaginated(dto);
|
||||
}
|
||||
|
||||
async findOne(id: string, userId: string) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id, user: { id: userId } },
|
||||
@@ -64,6 +68,17 @@ export class RequestService {
|
||||
return request;
|
||||
}
|
||||
|
||||
async findOneAsAdmin(id: string) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id },
|
||||
{ populate: ['items', 'items.product', 'user'] },
|
||||
);
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
async update(id: string, updateRequestDto: UpdateRequestDto, userId: string) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id, user: { id: userId } },
|
||||
@@ -73,6 +88,46 @@ export class RequestService {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
|
||||
if (request.status !== RequestStatusEnum.PENDING) {
|
||||
throw new BadRequestException('Only pending requests can be updated');
|
||||
}
|
||||
|
||||
const { items } = updateRequestDto;
|
||||
if (items?.length) {
|
||||
return this.em.transactional(async (em) => {
|
||||
request.items.removeAll();
|
||||
await em.flush();
|
||||
|
||||
for (const item of items) {
|
||||
const product = await this.productService.findOneOrFail(item.productId);
|
||||
const requestItem = em.create(RequestItem, {
|
||||
request,
|
||||
product,
|
||||
quantity: item.quantity,
|
||||
attributes: item.attributes,
|
||||
description: item.description,
|
||||
attachments: item.attachments,
|
||||
});
|
||||
request.items.add(requestItem);
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
return request;
|
||||
});
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
async updateAsAdmin(id: string, updateRequestDto: UpdateRequestDto) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id },
|
||||
{ populate: ['items', 'items.product', 'user'] },
|
||||
);
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
|
||||
const { items } = updateRequestDto;
|
||||
if (items?.length) {
|
||||
return this.em.transactional(async (em) => {
|
||||
@@ -114,4 +169,16 @@ export class RequestService {
|
||||
await this.em.flush();
|
||||
return request;
|
||||
}
|
||||
|
||||
async removeAsAdmin(id: string) {
|
||||
const request = await this.requestRepository.findOne(
|
||||
{ id },
|
||||
);
|
||||
if (!request) {
|
||||
throw new NotFoundException('Request not found');
|
||||
}
|
||||
request.deletedAt = new Date();
|
||||
await this.em.flush();
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user