Skip to content

Commit

Permalink
feat: add aircraftIcaoCode field (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh authored Apr 25, 2023
1 parent 7fbe5b9 commit b32b579
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 37 deletions.
46 changes: 32 additions & 14 deletions packages/app/pages/api/flights/all.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { FlightModel } from "@mach/database";
import type { NextApiRequest, NextApiResponse } from "next";
import NextCors from "nextjs-cors";
import { Op } from "sequelize";
import z from "zod";

const schema = z.object({
departureIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
arrivalIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
company: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
});
const schema = z
.object({
departureIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
arrivalIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
company: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
aircraftIcaoCode: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
})
.transform(({ aircraftIcaoCode, ...rest }) => {
return {
...rest,
...(aircraftIcaoCode && {
aircraft: {
icaoCode: {
[Op.in]: aircraftIcaoCode,
},
},
}),
};
});

export default async (req: NextApiRequest, res: NextApiResponse) => {
try {
Expand Down
64 changes: 41 additions & 23 deletions packages/app/pages/api/flights/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
import { FlightModel } from "@mach/database";
import type { NextApiRequest, NextApiResponse } from "next";
import z from "zod";
import NextCors from "nextjs-cors";
import { Op } from "sequelize";
import z from "zod";

const schema = z.object({
departureIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
arrivalIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
company: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
limit: z.preprocess(
(x) => (x ? Number(x) : undefined),
z.number().min(1).default(15)
),
offset: z.preprocess(
(x) => (x ? Number(x) : undefined),
z.number().min(0).default(0)
),
});
const schema = z
.object({
departureIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
arrivalIcao: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
company: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
aircraftIcaoCode: z
.preprocess((x) => (Array.isArray(x) ? x : [x]), z.array(z.string()))
.transform((values) => values.map((value) => value.toUpperCase()))
.optional(),
limit: z.preprocess(
(x) => (x ? Number(x) : undefined),
z.number().min(1).default(15)
),
offset: z.preprocess(
(x) => (x ? Number(x) : undefined),
z.number().min(0).default(0)
),
})
.transform(({ aircraftIcaoCode, ...rest }) => {
return {
...rest,
...(aircraftIcaoCode && {
aircraft: {
icaoCode: {
[Op.in]: aircraftIcaoCode,
},
},
}),
};
});

export default async (req: NextApiRequest, res: NextApiResponse) => {
await NextCors(req, res, { methods: ["GET"], origin: "*" });
Expand Down
24 changes: 24 additions & 0 deletions packages/app/pages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ const openApi = {
minimum: 0,
},
},
{
in: "query",
name: "aircraftIcaoCode",
style: "form",
explode: true,
schema: {
type: "array",
items: {
type: "string",
},
},
},
{
in: "query",
name: "company",
Expand Down Expand Up @@ -129,6 +141,18 @@ const openApi = {
},
},
},
{
in: "query",
name: "aircraftIcaoCode",
style: "form",
explode: true,
schema: {
type: "array",
items: {
type: "string",
},
},
},
{
in: "query",
name: "departureIcao",
Expand Down

0 comments on commit b32b579

Please sign in to comment.