Merge branch 'development' into 'devops-staging'
feat: add endpoint get product history price See merge request empatnusabangsa/ppob/ppob-backend!44
This commit is contained in:
commit
698ce1c95e
|
@ -1,7 +1,6 @@
|
|||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { ProductCategories } from '../entities/product-category.entity';
|
||||
import { ProductHistoryPrice } from '../entities/product-history-price.entity';
|
||||
|
||||
@Injectable()
|
||||
|
@ -34,4 +33,49 @@ export class ProductHistoryPriceService {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
async findOneByProductId(
|
||||
page: number,
|
||||
productId: string,
|
||||
supplierId: string,
|
||||
) {
|
||||
try {
|
||||
const query = this.productHistoryPriceService
|
||||
.createQueryBuilder('product_history_price')
|
||||
.leftJoin('product_history_price.product', 'product')
|
||||
.where({ product: productId })
|
||||
.andWhere('product_history_price.endDate IS NULL');
|
||||
|
||||
if (supplierId !== 'null' && supplierId) {
|
||||
query.andWhere('product.supplier = :supplierId', {
|
||||
supplierId: supplierId,
|
||||
});
|
||||
}
|
||||
|
||||
const data = await query
|
||||
.orderBy('product_history_price.createdAt', 'DESC')
|
||||
.skip(page * 10)
|
||||
.take(10)
|
||||
.getMany();
|
||||
|
||||
const totalData = await query.getCount();
|
||||
|
||||
return {
|
||||
data,
|
||||
count: totalData,
|
||||
};
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'Product History Price not found',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Put,
|
||||
Param,
|
||||
Controller,
|
||||
Delete,
|
||||
ParseUUIDPipe,
|
||||
Get,
|
||||
HttpStatus,
|
||||
Param,
|
||||
ParseUUIDPipe,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Request,
|
||||
} from '@nestjs/common';
|
||||
|
@ -179,6 +179,25 @@ export class ProductController {
|
|||
};
|
||||
}
|
||||
|
||||
@Get('price-history/:id')
|
||||
async findPriceHistoryByProductId(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
@Query('page') page: number,
|
||||
@Query('supplier') supplier: string,
|
||||
) {
|
||||
const data = await this.productHistoryPriceService.findOneByProductId(
|
||||
page,
|
||||
id,
|
||||
supplier,
|
||||
);
|
||||
|
||||
return {
|
||||
...data,
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
|
|
Loading…
Reference in New Issue
Block a user