request
This commit is contained in:
@@ -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