Skip to content

Commit cb45511

Browse files
Merge pull request #67 from hivetown/sortedCollections
Sorted collections
2 parents 83ffd7f + 74c6b90 commit cb45511

16 files changed

+101
-45
lines changed

src/controllers/producers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ export class ProducersController {
283283
}),
284284
query: Joi.object({
285285
page: Joi.number().min(1).optional(),
286-
pageSize: Joi.number().min(1).optional()
286+
pageSize: Joi.number().min(1).optional(),
287+
orderBy: Joi.string().valid('currentPrice', 'name').optional()
287288
})
288289
})
289290
])
@@ -294,7 +295,8 @@ export class ProducersController {
294295
const options: ProducerProductOptions = {
295296
page: Number(req.query.page) || -1,
296297
size: Number(req.query.pageSize) || -1,
297-
populate: ['producerProduct.productSpec', 'producerProduct.productionUnit', 'producerProduct_productSpec.images']
298+
populate: ['producerProduct.productSpec', 'producerProduct.productionUnit', 'producerProduct_productSpec.images'],
299+
orderBy: req.query.orderBy as any // as any is fine here because Joi validates the value
298300
};
299301

300302
const producerProducts = await container.producerProductGateway.findAll({ producerId }, options);

src/gateways/AddressGateway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class AddressGateway {
3131

3232
const totalItemsQb = qb.clone();
3333

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

3737
// Fetch results and map them
3838
const [totalItems, addresses] = await Promise.all([totalItemsQb.getCount(), qb.getResultList()]);

src/gateways/CarrierGateway.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class CarrierGateway {
2626

2727
const totalItemsQb = qb.clone();
2828

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

3232
// Fetch results and map them
3333
const [totalResults, carriers] = await Promise.all([totalItemsQb.getCount(), qb.getResultList()]);
@@ -49,7 +49,8 @@ export class CarrierGateway {
4949
{
5050
populate: ['productionUnit'],
5151
limit: paginataion.limit,
52-
offset: paginataion.offset
52+
offset: paginataion.offset,
53+
orderBy: { licensePlate: 'ASC' }
5354
}
5455
),
5556
this.repository.count({ productionUnit: { producer: { user: producerId } }, deletedAt: null })

src/gateways/CartItemGateway.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class CartItemGateway {
1919
{
2020
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec'],
2121
limit: pagination.limit,
22-
offset: pagination.offset
22+
offset: pagination.offset,
23+
orderBy: { producerProduct: { productSpec: { name: 'ASC' } } }
2324
}
2425
),
2526
this.repository.count({ consumer: { user: consumerId } })

src/gateways/CategoryGateway.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export class CategoryGateway {
6969

7070
const totalResultsQb = qb.clone();
7171

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

7475
const [categories, totalResults] = await Promise.all([qb.getResult(), totalResultsQb.count()]);
7576
return {

src/gateways/ConsumerGateway.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class ConsumerGateway {
3232
public async findAll(options: PaginatedOptions): Promise<BaseItems<Consumer>> {
3333
const pagination = paginate(options);
3434
const [consumers, totalResults] = await Promise.all([
35-
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset }),
35+
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset, orderBy: { user: { name: 'ASC' } } }),
3636
this.repository.count()
3737
]);
3838

@@ -77,7 +77,15 @@ export class ConsumerGateway {
7777
public async findAllWithDeletedAt(options: PaginatedOptions): Promise<BaseItems<Consumer>> {
7878
const pagination = paginate(options);
7979
const [consumers, totalResults] = await Promise.all([
80-
this.repository.find({}, { filters: { [SOFT_DELETABLE_FILTER]: false }, limit: pagination.limit, offset: pagination.offset }),
80+
this.repository.find(
81+
{},
82+
{
83+
filters: { [SOFT_DELETABLE_FILTER]: false },
84+
limit: pagination.limit,
85+
offset: pagination.offset,
86+
orderBy: { user: { name: 'ASC' } }
87+
}
88+
),
8189
this.repository.count({}, { filters: { [SOFT_DELETABLE_FILTER]: false } })
8290
]);
8391

src/gateways/FieldGateway.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class FieldGateway {
2020
const pagination = paginate(options);
2121

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

src/gateways/OrderGateway.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export class OrderGateway {
3333
fields: ['shippingAddress'],
3434
limit: pagination.limit,
3535
offset: pagination.offset,
36-
populate: ['items', 'items.shipment.events.status', 'shippingAddress', 'items.producerProduct.producer']
36+
populate: ['items', 'items.shipment.events.status', 'shippingAddress', 'items.producerProduct.producer'],
37+
orderBy: { id: 'DESC' }
3738
}
3839
),
3940
this.repository.count({ id: { $in: orderIds } })
@@ -57,7 +58,8 @@ export class OrderGateway {
5758
populate: ['items', 'items.shipment.events.status', 'shippingAddress'],
5859
fields: ['shippingAddress'],
5960
limit: pagination.limit,
60-
offset: pagination.offset
61+
offset: pagination.offset,
62+
orderBy: { id: 'DESC' }
6163
}
6264
),
6365
this.repository.count({ consumer: { user: consumerId } })
@@ -85,7 +87,8 @@ export class OrderGateway {
8587
'items.producerProduct.productSpec',
8688
'items.producerProduct.producer',
8789
'shippingAddress'
88-
]
90+
],
91+
orderBy: { id: 'DESC' }
8992
}
9093
);
9194
return orders;
@@ -94,7 +97,7 @@ export class OrderGateway {
9497
public async findByConsumerAndOrder(consumerId: number, orderId: number): Promise<Order | null> {
9598
const order = await this.repository.findOne(
9699
{ consumer: { user: consumerId }, id: orderId },
97-
{ populate: ['items', 'items.shipment.events.status', 'shippingAddress'] }
100+
{ populate: ['items', 'items.shipment.events.status', 'shippingAddress'], orderBy: { id: 'DESC' } }
98101
);
99102
return order;
100103
}
@@ -124,7 +127,8 @@ export class OrderGateway {
124127
'items.producerProduct',
125128
'items.producerProduct.productionUnit',
126129
'items.producerProduct.productionUnit.address'
127-
]
130+
],
131+
orderBy: { id: 'DESC' }
128132
});
129133
return order;
130134
}
@@ -133,7 +137,8 @@ export class OrderGateway {
133137
const orders = await this.repository.find(
134138
{ consumer: { user: consumerId } },
135139
{
136-
populate: ['items', 'items.shipment', 'items.shipment.events', 'items.shipment.events.status']
140+
populate: ['items', 'items.shipment', 'items.shipment.events', 'items.shipment.events.status'],
141+
orderBy: { id: 'DESC' }
137142
}
138143
);
139144
return orders;

src/gateways/OrderItemGateway.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class OrderItemGateway {
1818
.select('oi.order_id', true)
1919
.leftJoin('oi.producerProduct', 'pp')
2020
.where(`oi.producer_product_id = pp.id and pp.producer_id = '${producerId}'`)
21+
.orderBy({ order: { id: 'DESC' } })
2122
.getResult();
2223

2324
return orderIds;
@@ -38,7 +39,8 @@ export class OrderItemGateway {
3839
'order.consumer',
3940
'order.consumer.user',
4041
'order.items.shipment.events.status'
41-
]
42+
],
43+
orderBy: { order: { id: 'DESC' } }
4244
}
4345
);
4446

@@ -65,7 +67,8 @@ export class OrderItemGateway {
6567
'shipment.events.status'
6668
],
6769
limit: pagination.limit,
68-
offset: pagination.offset
70+
offset: pagination.offset,
71+
orderBy: { producerProduct: { productSpec: { name: 'ASC' } } }
6972
}
7073
),
7174
this.repository.count({ order: orderId, producerProduct: { producer: { user: producerId } } })
@@ -83,7 +86,8 @@ export class OrderItemGateway {
8386
const orderItem = await this.repository.findOne(
8487
{ order: orderId, producerProduct: { producer: { user: producerId }, id: producerProductId } },
8588
{
86-
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec']
89+
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec'],
90+
orderBy: { order: { id: 'DESC' } }
8791
}
8892
);
8993
return orderItem;
@@ -96,7 +100,7 @@ export class OrderItemGateway {
96100
): Promise<OrderItem | null> {
97101
const orderItem = await this.repository.findOne(
98102
{ order: orderId, producerProduct: { producer: { user: producerId }, id: producerProductId } },
99-
{ populate: ['shipment', 'shipment.carrier', 'shipment.events.address'] }
103+
{ populate: ['shipment', 'shipment.carrier', 'shipment.events.address'], orderBy: { order: { id: 'DESC' } } }
100104
);
101105
return orderItem;
102106
}
@@ -116,7 +120,8 @@ export class OrderItemGateway {
116120
],
117121
limit: pagination.limit,
118122
offset: pagination.offset,
119-
filters: { [SOFT_DELETABLE_FILTER]: false }
123+
filters: { [SOFT_DELETABLE_FILTER]: false },
124+
orderBy: { producerProduct: { productSpec: { name: 'ASC' } } }
120125
}
121126
),
122127
this.repository.count({ order: { id: orderId, consumer: { user: consumerId } } })
@@ -136,7 +141,8 @@ export class OrderItemGateway {
136141
{ order: { id: orderId, consumer: { user: consumerId } }, producerProduct: { id: producerProductId } },
137142
{
138143
populate: ['producerProduct', 'producerProduct.producer', 'producerProduct.productionUnit', 'producerProduct.productSpec'],
139-
filters: { [SOFT_DELETABLE_FILTER]: false }
144+
filters: { [SOFT_DELETABLE_FILTER]: false },
145+
orderBy: { order: { id: 'DESC' } }
140146
}
141147
);
142148
return q2;
@@ -146,7 +152,8 @@ export class OrderItemGateway {
146152
const products = await this.repository.find(
147153
{ producerProduct: { producer: { user: producerId } } },
148154
{
149-
populate: ['shipment', 'shipment.events', 'shipment.events.status']
155+
populate: ['shipment', 'shipment.events', 'shipment.events.status'],
156+
orderBy: { order: { id: 'DESC' } }
150157
}
151158
);
152159
return products;
@@ -165,7 +172,8 @@ export class OrderItemGateway {
165172
'shipment.events.status',
166173
'order.items',
167174
'order.items.shipment.events.status'
168-
]
175+
],
176+
orderBy: { order: { id: 'DESC' } }
169177
}
170178
);
171179
return orderItems;

src/gateways/ProducerGateway.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ProducerGateway {
1616
public async findAll(options: PaginatedOptions): Promise<BaseItems<Producer>> {
1717
const pagination = paginate(options);
1818
const [producers, totalResults] = await Promise.all([
19-
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset }),
19+
this.repository.find({}, { limit: pagination.limit, offset: pagination.offset, orderBy: { user: { name: 'ASC' } } }),
2020
this.repository.count()
2121
]);
2222

@@ -59,7 +59,8 @@ export class ProducerGateway {
5959
{
6060
populate: ['productionUnits'],
6161
limit: pagination.limit,
62-
offset: pagination.offset
62+
offset: pagination.offset,
63+
orderBy: { user: { name: 'ASC' } }
6364
}
6465
),
6566
this.repository.count({
@@ -89,7 +90,8 @@ export class ProducerGateway {
8990
{ user: id },
9091
{
9192
populate: ['productionUnits', 'producerProducts'],
92-
filters: { [SOFT_DELETABLE_FILTER]: false }
93+
filters: { [SOFT_DELETABLE_FILTER]: false },
94+
orderBy: { user: { name: 'ASC' } }
9395
}
9496
);
9597
return producer;
@@ -103,7 +105,15 @@ export class ProducerGateway {
103105
public async findAllWithDeletedAt(options: PaginatedOptions): Promise<BaseItems<Producer>> {
104106
const pagination = paginate(options);
105107
const [producers, totalResults] = await Promise.all([
106-
this.repository.find({}, { filters: { [SOFT_DELETABLE_FILTER]: false }, limit: pagination.limit, offset: pagination.offset }),
108+
this.repository.find(
109+
{},
110+
{
111+
filters: { [SOFT_DELETABLE_FILTER]: false },
112+
limit: pagination.limit,
113+
offset: pagination.offset,
114+
orderBy: { user: { name: 'ASC' } }
115+
}
116+
),
107117
this.repository.count({}, { filters: { [SOFT_DELETABLE_FILTER]: false } })
108118
]);
109119

0 commit comments

Comments
 (0)