From b32b579d5f604e2b057cc613dc3f614717974c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Henrique?= Date: Tue, 25 Apr 2023 15:01:41 -0300 Subject: [PATCH] feat: add aircraftIcaoCode field (#133) --- packages/app/pages/api/flights/all.ts | 46 ++++++++++++------ packages/app/pages/api/flights/index.ts | 64 ++++++++++++++++--------- packages/app/pages/api/index.ts | 24 ++++++++++ 3 files changed, 97 insertions(+), 37 deletions(-) diff --git a/packages/app/pages/api/flights/all.ts b/packages/app/pages/api/flights/all.ts index 01edbab3..f486752e 100644 --- a/packages/app/pages/api/flights/all.ts +++ b/packages/app/pages/api/flights/all.ts @@ -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 { diff --git a/packages/app/pages/api/flights/index.ts b/packages/app/pages/api/flights/index.ts index 823f217b..f9291094 100644 --- a/packages/app/pages/api/flights/index.ts +++ b/packages/app/pages/api/flights/index.ts @@ -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: "*" }); diff --git a/packages/app/pages/api/index.ts b/packages/app/pages/api/index.ts index 25f41c2a..61336687 100644 --- a/packages/app/pages/api/index.ts +++ b/packages/app/pages/api/index.ts @@ -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", @@ -129,6 +141,18 @@ const openApi = { }, }, }, + { + in: "query", + name: "aircraftIcaoCode", + style: "form", + explode: true, + schema: { + type: "array", + items: { + type: "string", + }, + }, + }, { in: "query", name: "departureIcao",