Skip to content

Commit

Permalink
complete query of products for admin users
Browse files Browse the repository at this point in the history
  • Loading branch information
bahram1249 committed Feb 20, 2024
1 parent 7d82cf3 commit 2518461
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 8 deletions.
8 changes: 4 additions & 4 deletions apps/e-commerce/src/admin/product/product.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { User } from '@rahino/database/models/core/user.entity';
export class ProductController {
constructor(private service: ProductService) {}
@ApiOperation({ description: 'show all products' })
// @CheckPermission({ permissionSymbol: 'ecommerce.admin.products.getall' })
@CheckPermission({ permissionSymbol: 'ecommerce.admin.products.getall' })
@Get('/')
@ApiQuery({
name: 'filter',
Expand All @@ -55,11 +55,11 @@ export class ProductController {
@CheckPermission({ permissionSymbol: 'ecommerce.admin.products.getone' })
@Get('/:id')
@HttpCode(HttpStatus.OK)
async findById(@Param('id') entityId: bigint) {
return await this.service.findById(entityId);
async findById(@GetUser() user: User, @Param('id') entityId: bigint) {
return await this.service.findById(user, entityId);
}
@ApiOperation({ description: 'create product by admin' })
//@CheckPermission({ permissionSymbol: 'ecommerce.admin.products.create' })
@CheckPermission({ permissionSymbol: 'ecommerce.admin.products.create' })
@Post('/')
@HttpCode(HttpStatus.CREATED)
async create(@GetUser() user: User, @Body() dto: ProductDto) {
Expand Down
208 changes: 205 additions & 3 deletions apps/e-commerce/src/admin/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { ECCity } from '@rahino/database/models/ecommerce-eav/ec-city.entity';
import { ECNeighborhood } from '@rahino/database/models/ecommerce-eav/ec-neighborhood.entity';
import { ECVariationPrice } from '@rahino/database/models/ecommerce-eav/ec-variation-prices';
import { inventoryStatusService } from '@rahino/ecommerce/inventory/inventory-status.service';
import { Attachment } from '@rahino/database/models/core/attachment.entity';

@Injectable()
export class ProductService {
Expand Down Expand Up @@ -302,6 +303,12 @@ export class ProductService {
],
required: false,
},
{
attributes: ['id', 'fileName'],
model: Attachment,
as: 'attachments',
required: false,
},
])
.subQuery(false)
.limit(filter.limit)
Expand All @@ -320,14 +327,20 @@ export class ProductService {
};
}

async findById(id: bigint) {
async findById(user: User, id: bigint) {
const vendorResult = await this.userVendorService.findAll(
user,
this.listFilter,
);

const vendorIds = vendorResult.result.map((vendor) => vendor.id);
const product = await this.repository.findOne(
new QueryOptionsBuilder()
.attributes([
'id',
'title',
'sku',
'description',
// 'description',
'slug',
'entityTypeId',
'colorBased',
Expand Down Expand Up @@ -356,7 +369,190 @@ export class ProductService {
model: EAVEntityType,
as: 'entityType',
},
{
attributes: [
'attributeId',
[
Sequelize.fn(
'isnull',
Sequelize.col('productAttributeValues.val'),
Sequelize.col('productAttributeValues.attributeValue.value'),
),
'val',
],
[Sequelize.col('attributeValueId'), 'attributeValueId'],
],
model: EAVEntityAttributeValue,
as: 'productAttributeValues',
include: [
{
attributes: ['id', 'name', 'attributeTypeId'],
model: EAVAttribute,
as: 'attribute',
},
{
attributes: ['id', 'attributeId', 'value'],
model: EAVAttributeValue,
as: 'attributeValue',
},
],
required: false,
},
{
attributes: [
'id',
'productId',
'vendorId',
'colorId',
'guaranteeId',
'guaranteeMonthId',
'buyPrice',
'qty',
'onlyProvinceId',
'vendorAddressId',
'weight',
'inventoryStatusId',
'description',
],
model: ECInventory,
as: 'inventories',
where: {
vendorId: {
[Op.in]: vendorIds,
},
},
include: [
{
attributes: ['id', 'name'],
model: ECInventoryStatus,
as: 'inventoryStatus',
},
{
attributes: ['id', 'name'],
model: ECVendor,
as: 'vendor',
},
{
attributes: ['id', 'name', 'hexCode'],
model: ECColor,
as: 'color',
},
{
attributes: ['id', 'name'],
model: ECGuarantee,
as: 'guarantee',
},
{
attributes: ['id', 'name'],
model: ECGuaranteeMonth,
as: 'guaranteeMonth',
},
{
attributes: ['id', 'name'],
model: ECProvince,
as: 'onlyProvince',
},
{
attributes: ['id', 'vendorId', 'addressId'],
model: ECVendorAddress,
as: 'vendorAddress',
include: [
{
attributes: [
'id',
'name',
'latitude',
'longitude',
'provinceId',
'cityId',
'neighborhoodId',
'street',
'alley',
'plaque',
'floorNumber',
],
model: ECAddress,
as: 'address',
include: [
{
attributes: ['id', 'name'],
model: ECProvince,
as: 'province',
},
{
attributes: ['id', 'name'],
model: ECCity,
as: 'city',
},
{
attributes: ['id', 'name'],
model: ECNeighborhood,
as: 'neighborhood',
},
],
},
{
attributes: ['id', 'name'],
model: ECVendor,
as: 'vendor',
},
],
},
{
attributes: ['price'],
model: ECInventoryPrice,
as: 'firstPrice',
include: [
{
attributes: ['id', 'name'],
model: ECVariationPrice,
as: 'variationPrice',
},
],
where: Sequelize.where(
Sequelize.fn(
'isnull',
Sequelize.col('inventories.firstPrice.isDeleted'),
0,
),
{
[Op.eq]: 0,
},
),
},
{
attributes: ['price'],
model: ECInventoryPrice,
as: 'secondaryPrice',
include: [
{
attributes: ['id', 'name'],
model: ECVariationPrice,
as: 'variationPrice',
},
],
where: Sequelize.where(
Sequelize.fn(
'isnull',
Sequelize.col('inventories.secondaryPrice.isDeleted'),
0,
),
{
[Op.eq]: 0,
},
),
},
],
required: false,
},
{
attributes: ['id', 'fileName'],
model: Attachment,
as: 'attachments',
required: false,
},
])
.subQuery(false)
.filter(
Sequelize.where(
Sequelize.fn('isnull', Sequelize.col('ECProduct.isDeleted'), 0),
Expand All @@ -368,6 +564,12 @@ export class ProductService {
.filter({
id: id,
})
.order([
{ model: ECInventory, as: 'inventories' },
{ model: ECVendor, as: 'vendor' },
'priorityOrder',
'asc',
])
.build(),
);
if (!product) {
Expand Down Expand Up @@ -507,7 +709,7 @@ export class ProductService {
}

return {
result: await this.findById(product.id),
result: (await this.findById(user, product.id)).result,
};
}

Expand Down
Loading

0 comments on commit 2518461

Please sign in to comment.