Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/controllers/producers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ export class ProducersController {
}),
query: Joi.object({
page: Joi.number().min(1).optional(),
pageSize: Joi.number().min(1).optional()
pageSize: Joi.number().min(1).optional(),
orderBy: Joi.string().valid('currentPrice', 'name').optional()
})
})
])
Expand All @@ -294,7 +295,8 @@ export class ProducersController {
const options: ProducerProductOptions = {
page: Number(req.query.page) || -1,
size: Number(req.query.pageSize) || -1,
populate: ['producerProduct.productSpec', 'producerProduct.productionUnit', 'producerProduct_productSpec.images']
populate: ['producerProduct.productSpec', 'producerProduct.productionUnit', 'producerProduct_productSpec.images'],
orderBy: req.query.orderBy as any // as any is fine here because Joi validates the value
};

const producerProducts = await container.producerProductGateway.findAll({ producerId }, options);
Expand Down
4 changes: 2 additions & 2 deletions src/gateways/AddressGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class AddressGateway {

const totalItemsQb = qb.clone();

// Paginate
void qb.offset(pagination.offset).limit(pagination.limit);
// Order & Paginate
void qb.orderBy({ id: 'DESC' }).offset(pagination.offset).limit(pagination.limit);

// Fetch results and map them
const [totalItems, addresses] = await Promise.all([totalItemsQb.getCount(), qb.getResultList()]);
Expand Down
7 changes: 4 additions & 3 deletions src/gateways/CarrierGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class CarrierGateway {

const totalItemsQb = qb.clone();

// Paginate
void qb.offset(paginataion.offset).limit(paginataion.limit);
// Order & Paginate
void qb.orderBy({ licensePlate: 'ASC' }).offset(paginataion.offset).limit(paginataion.limit);

// Fetch results and map them
const [totalResults, carriers] = await Promise.all([totalItemsQb.getCount(), qb.getResultList()]);
Expand All @@ -49,7 +49,8 @@ export class CarrierGateway {
{
populate: ['productionUnit'],
limit: paginataion.limit,
offset: paginataion.offset
offset: paginataion.offset,
orderBy: { licensePlate: 'ASC' }
}
),
this.repository.count({ productionUnit: { producer: { user: producerId } }, deletedAt: null })
Expand Down
3 changes: 2 additions & 1 deletion src/gateways/CartItemGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class CartItemGateway {
{
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec'],
limit: pagination.limit,
offset: pagination.offset
offset: pagination.offset,
orderBy: { producerProduct: { productSpec: { name: 'ASC' } } }
}
),
this.repository.count({ consumer: { user: consumerId } })
Expand Down
3 changes: 2 additions & 1 deletion src/gateways/CategoryGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class CategoryGateway {

const totalResultsQb = qb.clone();

void qb.leftJoinAndSelect('category.image', 'image').limit(pagination.limit).offset(pagination.offset);
// Order and Pagination
void qb.leftJoinAndSelect('category.image', 'image').orderBy({ name: 'ASC' }).limit(pagination.limit).offset(pagination.offset);

const [categories, totalResults] = await Promise.all([qb.getResult(), totalResultsQb.count()]);
return {
Expand Down
12 changes: 10 additions & 2 deletions src/gateways/ConsumerGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ConsumerGateway {
public async findAll(options: PaginatedOptions): Promise<BaseItems<Consumer>> {
const pagination = paginate(options);
const [consumers, totalResults] = await Promise.all([
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset }),
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset, orderBy: { user: { name: 'ASC' } } }),
this.repository.count()
]);

Expand Down Expand Up @@ -77,7 +77,15 @@ export class ConsumerGateway {
public async findAllWithDeletedAt(options: PaginatedOptions): Promise<BaseItems<Consumer>> {
const pagination = paginate(options);
const [consumers, totalResults] = await Promise.all([
this.repository.find({}, { filters: { [SOFT_DELETABLE_FILTER]: false }, limit: pagination.limit, offset: pagination.offset }),
this.repository.find(
{},
{
filters: { [SOFT_DELETABLE_FILTER]: false },
limit: pagination.limit,
offset: pagination.offset,
orderBy: { user: { name: 'ASC' } }
}
),
this.repository.count({}, { filters: { [SOFT_DELETABLE_FILTER]: false } })
]);

Expand Down
2 changes: 1 addition & 1 deletion src/gateways/FieldGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class FieldGateway {
const pagination = paginate(options);

const [fields, totalResults] = await Promise.all([
this.repository.find({ categories: categoryId }, { limit: pagination.limit, offset: pagination.offset }),
this.repository.find({ categories: categoryId }, { limit: pagination.limit, offset: pagination.offset, orderBy: { name: 'ASC' } }),
this.repository.count({ categories: categoryId })
]);

Expand Down
17 changes: 11 additions & 6 deletions src/gateways/OrderGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class OrderGateway {
fields: ['shippingAddress'],
limit: pagination.limit,
offset: pagination.offset,
populate: ['items', 'items.shipment.events.status', 'shippingAddress', 'items.producerProduct.producer']
populate: ['items', 'items.shipment.events.status', 'shippingAddress', 'items.producerProduct.producer'],
orderBy: { id: 'DESC' }
}
),
this.repository.count({ id: { $in: orderIds } })
Expand All @@ -57,7 +58,8 @@ export class OrderGateway {
populate: ['items', 'items.shipment.events.status', 'shippingAddress'],
fields: ['shippingAddress'],
limit: pagination.limit,
offset: pagination.offset
offset: pagination.offset,
orderBy: { id: 'DESC' }
}
),
this.repository.count({ consumer: { user: consumerId } })
Expand Down Expand Up @@ -85,7 +87,8 @@ export class OrderGateway {
'items.producerProduct.productSpec',
'items.producerProduct.producer',
'shippingAddress'
]
],
orderBy: { id: 'DESC' }
}
);
return orders;
Expand All @@ -94,7 +97,7 @@ export class OrderGateway {
public async findByConsumerAndOrder(consumerId: number, orderId: number): Promise<Order | null> {
const order = await this.repository.findOne(
{ consumer: { user: consumerId }, id: orderId },
{ populate: ['items', 'items.shipment.events.status', 'shippingAddress'] }
{ populate: ['items', 'items.shipment.events.status', 'shippingAddress'], orderBy: { id: 'DESC' } }
);
return order;
}
Expand Down Expand Up @@ -124,7 +127,8 @@ export class OrderGateway {
'items.producerProduct',
'items.producerProduct.productionUnit',
'items.producerProduct.productionUnit.address'
]
],
orderBy: { id: 'DESC' }
});
return order;
}
Expand All @@ -133,7 +137,8 @@ export class OrderGateway {
const orders = await this.repository.find(
{ consumer: { user: consumerId } },
{
populate: ['items', 'items.shipment', 'items.shipment.events', 'items.shipment.events.status']
populate: ['items', 'items.shipment', 'items.shipment.events', 'items.shipment.events.status'],
orderBy: { id: 'DESC' }
}
);
return orders;
Expand Down
24 changes: 16 additions & 8 deletions src/gateways/OrderItemGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class OrderItemGateway {
.select('oi.order_id', true)
.leftJoin('oi.producerProduct', 'pp')
.where(`oi.producer_product_id = pp.id and pp.producer_id = '${producerId}'`)
.orderBy({ order: { id: 'DESC' } })
.getResult();

return orderIds;
Expand All @@ -38,7 +39,8 @@ export class OrderItemGateway {
'order.consumer',
'order.consumer.user',
'order.items.shipment.events.status'
]
],
orderBy: { order: { id: 'DESC' } }
}
);

Expand All @@ -65,7 +67,8 @@ export class OrderItemGateway {
'shipment.events.status'
],
limit: pagination.limit,
offset: pagination.offset
offset: pagination.offset,
orderBy: { producerProduct: { productSpec: { name: 'ASC' } } }
}
),
this.repository.count({ order: orderId, producerProduct: { producer: { user: producerId } } })
Expand All @@ -83,7 +86,8 @@ export class OrderItemGateway {
const orderItem = await this.repository.findOne(
{ order: orderId, producerProduct: { producer: { user: producerId }, id: producerProductId } },
{
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec']
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec'],
orderBy: { order: { id: 'DESC' } }
}
);
return orderItem;
Expand All @@ -96,7 +100,7 @@ export class OrderItemGateway {
): Promise<OrderItem | null> {
const orderItem = await this.repository.findOne(
{ order: orderId, producerProduct: { producer: { user: producerId }, id: producerProductId } },
{ populate: ['shipment', 'shipment.carrier', 'shipment.events.address'] }
{ populate: ['shipment', 'shipment.carrier', 'shipment.events.address'], orderBy: { order: { id: 'DESC' } } }
);
return orderItem;
}
Expand All @@ -116,7 +120,8 @@ export class OrderItemGateway {
],
limit: pagination.limit,
offset: pagination.offset,
filters: { [SOFT_DELETABLE_FILTER]: false }
filters: { [SOFT_DELETABLE_FILTER]: false },
orderBy: { producerProduct: { productSpec: { name: 'ASC' } } }
}
),
this.repository.count({ order: { id: orderId, consumer: { user: consumerId } } })
Expand All @@ -136,7 +141,8 @@ export class OrderItemGateway {
{ order: { id: orderId, consumer: { user: consumerId } }, producerProduct: { id: producerProductId } },
{
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec'],
filters: { [SOFT_DELETABLE_FILTER]: false }
filters: { [SOFT_DELETABLE_FILTER]: false },
orderBy: { order: { id: 'DESC' } }
}
);
return q2;
Expand All @@ -146,7 +152,8 @@ export class OrderItemGateway {
const products = await this.repository.find(
{ producerProduct: { producer: { user: producerId } } },
{
populate: ['shipment', 'shipment.events', 'shipment.events.status']
populate: ['shipment', 'shipment.events', 'shipment.events.status'],
orderBy: { order: { id: 'DESC' } }
}
);
return products;
Expand All @@ -165,7 +172,8 @@ export class OrderItemGateway {
'shipment.events.status',
'order.items',
'order.items.shipment.events.status'
]
],
orderBy: { order: { id: 'DESC' } }
}
);
return orderItems;
Expand Down
18 changes: 14 additions & 4 deletions src/gateways/ProducerGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ProducerGateway {
public async findAll(options: PaginatedOptions): Promise<BaseItems<Producer>> {
const pagination = paginate(options);
const [producers, totalResults] = await Promise.all([
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset }),
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset, orderBy: { user: { name: 'ASC' } } }),
this.repository.count()
]);

Expand Down Expand Up @@ -59,7 +59,8 @@ export class ProducerGateway {
{
populate: ['productionUnits'],
limit: pagination.limit,
offset: pagination.offset
offset: pagination.offset,
orderBy: { user: { name: 'ASC' } }
}
),
this.repository.count({
Expand Down Expand Up @@ -89,7 +90,8 @@ export class ProducerGateway {
{ user: id },
{
populate: ['productionUnits', 'producerProducts'],
filters: { [SOFT_DELETABLE_FILTER]: false }
filters: { [SOFT_DELETABLE_FILTER]: false },
orderBy: { user: { name: 'ASC' } }
}
);
return producer;
Expand All @@ -103,7 +105,15 @@ export class ProducerGateway {
public async findAllWithDeletedAt(options: PaginatedOptions): Promise<BaseItems<Producer>> {
const pagination = paginate(options);
const [producers, totalResults] = await Promise.all([
this.repository.find({}, { filters: { [SOFT_DELETABLE_FILTER]: false }, limit: pagination.limit, offset: pagination.offset }),
this.repository.find(
{},
{
filters: { [SOFT_DELETABLE_FILTER]: false },
limit: pagination.limit,
offset: pagination.offset,
orderBy: { user: { name: 'ASC' } }
}
),
this.repository.count({}, { filters: { [SOFT_DELETABLE_FILTER]: false } })
]);

Expand Down
27 changes: 20 additions & 7 deletions src/gateways/ProducerProductGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export class ProducerProductGateway {

const totalItemsQb = qb.clone();

// Paginate
void qb.offset(pagination.offset).limit(pagination.limit);
// Order & Paginate
void qb.orderBy({ currentPrice: 'ASC' }).offset(pagination.offset).limit(pagination.limit);

// Fetch results and map them
const [totalResults, products] = await Promise.all([totalItemsQb.getCount(), qb.getResultList()]);
Expand Down Expand Up @@ -102,8 +102,11 @@ export class ProducerProductGateway {

const totalItemsQb = qb.clone();

// Paginate
void qb.offset(pagination.offset).limit(pagination.limit);
// Order & Paginate
void qb
.orderBy({ productSpec: { name: 'ASC' } })
.offset(pagination.offset)
.limit(pagination.limit);

// Populate
if (options?.populate) {
Expand Down Expand Up @@ -151,17 +154,27 @@ export class ProducerProductGateway {
}

if (filter?.search) {
void qb.leftJoin('producerProduct.specification', 'spec').andWhere({
void qb.andWhere({
$or: [
{ 'lower(spec.name)': { $like: stringSearchType(filter.search) } },
{ 'lower(spec.description)': { $like: stringSearchType(filter.search) } }
{ 'lower(producerProduct_productSpec.name)': { $like: stringSearchType(filter.search) } },
{ 'lower(producerProduct_productSpec.description)': { $like: stringSearchType(filter.search) } }
]
});
}

// Calculate items count before grouping and paginating
const totalItemsQb = qb.clone();

// Order
switch (options?.orderBy) {
case 'currentPrice':
void qb.orderBy({ currentPrice: 'ASC' });
break;
case 'name':
default:
void qb.orderBy({ 'producerProduct_productSpec.name': 'ASC' });
}

// Paginate
void qb.offset(pagination.offset).limit(pagination.limit);

Expand Down
1 change: 1 addition & 0 deletions src/gateways/ProductSpecCategoryGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class ProductSpecCategoryGateway {
.leftJoinAndSelect('category.parent', 'parent')
.limit(pagination.limit)
.offset(pagination.offset)
.orderBy({ category: { name: 'ASC' } })
.getResult(),
this.repository
.createQueryBuilder('productSpecCategory')
Expand Down
7 changes: 6 additions & 1 deletion src/gateways/ProductSpecFieldGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export class ProductSpecFieldGateway {
const [productSpecField, totalResults] = await Promise.all([
this.repository.find(
{ productSpecCategory: { productSpec: productSpecId, category: categoryId } },
{ fields: ['field', 'value'], limit: pagination.limit, offset: pagination.offset }
{
fields: ['field', 'value'],
limit: pagination.limit,
offset: pagination.offset,
orderBy: { productSpecCategory: { category: { name: 'ASC' } }, field: { name: 'ASC' } }
}
),
this.repository.count({ productSpecCategory: { productSpec: productSpecId, category: categoryId } })
]);
Expand Down
5 changes: 2 additions & 3 deletions src/gateways/ProductSpecGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ export class ProductSpecGateway {
.addSelect('MIN(producerProduct.current_price) as minPrice')
.addSelect('MAX(producerProduct.current_price) as maxPrice');

// Paginate
void qb.offset(pagination.offset).limit(pagination.limit);
// Order & Paginate
void qb.orderBy({ name: 'ASC' }).offset(pagination.offset).limit(pagination.limit);

// Fetch results and map them
const [miscData, productSpecs] = await Promise.all([miscQb.execute('get'), qb.getResultList()]);
console.log('\n\n\n\n\n\n', qb.getQuery(), '\n\n\n\n\n\n');

const totalPages = Math.ceil(miscData.totalItems / pagination.limit);
const page = Math.ceil(pagination.offset / pagination.limit) + 1;
Expand Down
Loading