fix package api bug and add some package query
This commit is contained in:
@@ -34,6 +34,7 @@ import {
|
||||
packageDetailParamsSchema,
|
||||
packageDetailRequestSchema,
|
||||
packageParamsSchema,
|
||||
packageQuerySchema,
|
||||
packageRequestSchema,
|
||||
} from "@/modules/package/package.schemas";
|
||||
import type {
|
||||
@@ -41,7 +42,7 @@ import type {
|
||||
PackageDetailResponse,
|
||||
PackageResponse,
|
||||
} from "@/modules/package/package.types";
|
||||
import { wrap } from "@mikro-orm/core";
|
||||
import { wrap, type OrderDefinition } from "@mikro-orm/core";
|
||||
import { Router, type Request, type Response } from "express";
|
||||
import slugify from "slugify";
|
||||
import { ulid } from "ulid";
|
||||
@@ -143,7 +144,7 @@ export class PackageController extends Controller {
|
||||
}
|
||||
|
||||
async list(req: Request, res: Response) {
|
||||
const parseQueryResult = paginationQuerySchema.safeParse(req.query);
|
||||
const parseQueryResult = packageQuerySchema.safeParse(req.query);
|
||||
if (!parseQueryResult.success) {
|
||||
return this.handleZodError(parseQueryResult.error, res, "query");
|
||||
}
|
||||
@@ -151,16 +152,45 @@ export class PackageController extends Controller {
|
||||
|
||||
const count = await orm.em.count(Package);
|
||||
|
||||
const packages = await orm.em.find(
|
||||
Package,
|
||||
{},
|
||||
{
|
||||
limit: query.per_page,
|
||||
offset: (query.page - 1) * query.per_page,
|
||||
orderBy: { createdAt: "DESC" },
|
||||
populate: ["*"],
|
||||
},
|
||||
);
|
||||
const orderBy: OrderDefinition<Package> = {};
|
||||
switch (query.sort_by) {
|
||||
case "newest":
|
||||
orderBy.createdAt = "DESC";
|
||||
break;
|
||||
case "oldest":
|
||||
orderBy.createdAt = "ASC";
|
||||
break;
|
||||
}
|
||||
|
||||
let packageQueryBuilder = orm.em
|
||||
.createQueryBuilder(Package, "_package")
|
||||
.select(["*"], true)
|
||||
.distinctOn(["class"])
|
||||
.limit(query.per_page)
|
||||
.offset((query.page - 1) * query.per_page)
|
||||
.leftJoinAndSelect("_package.partner", "_partner");
|
||||
|
||||
if ("class" in query && query.class) {
|
||||
packageQueryBuilder = packageQueryBuilder.where({ class: query.class });
|
||||
}
|
||||
if ("by_ideal" in query && query.by_ideal) {
|
||||
packageQueryBuilder = packageQueryBuilder.where({ class: "by_ideal" });
|
||||
}
|
||||
|
||||
switch (query.sort_by) {
|
||||
case "newest":
|
||||
packageQueryBuilder = packageQueryBuilder.orderBy({
|
||||
"_package.created_at": "DESC",
|
||||
});
|
||||
break;
|
||||
case "oldest":
|
||||
packageQueryBuilder = packageQueryBuilder.orderBy({
|
||||
"_package.created_at": "ASC",
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
const packages = await packageQueryBuilder.getResultList();
|
||||
|
||||
return res.status(200).json({
|
||||
data: packages.map(this.mapper.mapEntityToResponse.bind(this.mapper)),
|
||||
@@ -376,10 +406,9 @@ export class PackageController extends Controller {
|
||||
}
|
||||
|
||||
let outboundFlightSchedule: FlightSchedule | null = null;
|
||||
for (const [
|
||||
index,
|
||||
outboundFlightId,
|
||||
] of body.outbound_flight_ids.entries()) {
|
||||
for (const [index, outboundFlightId] of body.outbound_flight_ids
|
||||
.toReversed()
|
||||
.entries()) {
|
||||
const outboundFlight = await orm.em.findOne(
|
||||
FlightClass,
|
||||
{ id: outboundFlightId },
|
||||
@@ -954,10 +983,9 @@ export class PackageController extends Controller {
|
||||
orm.em.remove(outboundFlight);
|
||||
}
|
||||
let outboundFlightSchedule: FlightSchedule | null = null;
|
||||
for (const [
|
||||
index,
|
||||
outboundFlightId,
|
||||
] of body.outbound_flight_ids.entries()) {
|
||||
for (const [index, outboundFlightId] of body.outbound_flight_ids
|
||||
.toReversed()
|
||||
.entries()) {
|
||||
const outboundFlight = await orm.em.findOne(
|
||||
FlightClass,
|
||||
{ id: outboundFlightId },
|
||||
|
||||
Reference in New Issue
Block a user