[go: nahoru, domu]

Skip to content

Commit

Permalink
Revert API Tampering
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonXDev committed Mar 2, 2024
1 parent aeeac36 commit c2212ad
Show file tree
Hide file tree
Showing 4 changed files with 2,481 additions and 2,883 deletions.
79 changes: 32 additions & 47 deletions apps/nextjs/src/lib/utils/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ function getBaseUrl() {
// browser should use relative path
return "";

if (process.env.VERCEL_URL)
// reference for vercel.com
return `https://${process.env.VERCEL_URL}`;
if (process.env.VERCEL_URL)
// reference for vercel.com
return `https://${process.env.VERCEL_URL}`;

if (process.env.RENDER_INTERNAL_HOSTNAME)
// reference for render.com
return `http://${process.env.RENDER_INTERNAL_HOSTNAME}:${process.env.PORT}`;
if (process.env.RENDER_INTERNAL_HOSTNAME)
// reference for render.com
return `http://${process.env.RENDER_INTERNAL_HOSTNAME}:${process.env.PORT}`;

// assume localhost
return `http://localhost:${process.env.PORT ?? 3000}`;
// assume localhost
return `http://localhost:${process.env.PORT ?? 3000}`;
if (process.env.VERCEL_URL)
// reference for vercel.com
return `https://${process.env.VERCEL_URL}`;
if (process.env.VERCEL_URL)
// reference for vercel.com
return `https://${process.env.VERCEL_URL}`;

if (process.env.RENDER_INTERNAL_HOSTNAME)
// reference for render.com
return `http://${process.env.RENDER_INTERNAL_HOSTNAME}:${process.env.PORT}`;
if (process.env.RENDER_INTERNAL_HOSTNAME)
// reference for render.com
return `http://${process.env.RENDER_INTERNAL_HOSTNAME}:${process.env.PORT}`;

// assume localhost
return `http://localhost:${process.env.PORT ?? 3000}`;
// assume localhost
// return `http://localhost:${process.env.PORT ?? 3000}`;
}

export const trpc = createTRPCNext<AppRouter>({
Expand All @@ -42,33 +42,18 @@ export const trpc = createTRPCNext<AppRouter>({
**/
url: `${getBaseUrl()}/api/trpc`,

// You can pass any HTTP headers you wish here
async headers() {
return {
// authorization: getAuthCookie(),
};
},
}),
],
};
},
/**
* @link https://trpc.io/docs/v11/ssr
**/
ssr: false,
// You can pass any HTTP headers you wish here
async headers() {
return {
// authorization: getAuthCookie(),
};
},
}),
],
};
},
/**
* @link https://trpc.io/docs/v11/ssr
**/
ssr: false,
// You can pass any HTTP headers you wish here
async headers() {
return {
// authorization: getAuthCookie(),
};
},
}),
],
};
},
/**
* @link https://trpc.io/docs/v11/ssr
**/
ssr: false,
});

241 changes: 113 additions & 128 deletions packages/api/src/router/scouting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,83 +18,90 @@ export const eventLog = z.object({
endgame: UltimateHistory,
});
export const scoutingRouter = createTRPCRouter({
// createMatch: publicProcedure.input(z.object({id:z.string({})}))
// updateMatch: publicProcedure.input()
getAssignments: publicProcedure
.input(
z.object({ event: z.string(), assignee: z.string().uuid().optional() }),
)
.query(async ({ ctx, input }) => {
let query = ctx.supabase
.from("assignments")
.select(
"matches (key, event, events (key, name)), team, alliance, assignee",
)
.eq("matches.event", input.event);
if (input?.assignee === undefined) {
query = query.is("assignee", null);
} else {
query = query.eq("assignee", input.assignee);
}
const { data, error } = await query;
console.log("RPC: getAssignments | input:", input, " | db output:", data);
if (error !== null || data === null) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error fetching assignments",
cause: error,
});
}
// Some data wrangling to make it easier to work with
const groupBy = <T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => string,
) =>
array.reduce(
(acc, value, index, array) => {
(acc[predicate(value, index, array)] ||= []).push(value);
return acc;
},
{} as Record<string, T[]>,
);
// createMatch: publicProcedure.input(z.object({id:z.string({})}))
// updateMatch: publicProcedure.input()
getAssignments: publicProcedure
.input(
z.object({ event: z.string(), assignee: z.string().uuid().optional() }),
)
.query(async ({ ctx, input }) => {
let query = ctx.supabase
.from("assignments")
.select(
"matches (key, event, events (key, name)), team, alliance, assignee",
)
.eq("matches.event", input.event);
if (input?.assignee === undefined) {
query = query.is("assignee", null);
} else {
query = query.eq("assignee", input.assignee);
}
const { data, error } = await query;
console.log("RPC: getAssignments | input:", input, " | db output:", data);
if (error !== null || data === null) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error fetching assignments",
cause: error,
});
}
// Some data wrangling to make it easier to work with
const groupBy = <T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => string,
) =>
array.reduce(
(acc, value, index, array) => {
(acc[predicate(value, index, array)] ||= []).push(value);
return acc;
},
{} as Record<string, T[]>,
);

const byMatch = groupBy(
data,
({ matches }) =>
// biome-ignore lint/style/noNonNullAssertion: <explanation>
matches!.key,
);
return Object.values(byMatch).map((matches) => {
// Keys are guaranteed to be in the form of "frcXXXX" where XXXX is a number
const redTeams = matches
.filter((team) => team.alliance.startsWith("red"))
// biome-ignore lint/style/noNonNullAssertion: see above
.map((x) => parseInt(x.team.match(/\d+/)![0]));
const blueTeams = matches
.filter((team) => team.alliance.startsWith("blue"))
// biome-ignore lint/style/noNonNullAssertion: see above
.map((x) => parseInt(x.team.match(/\d+/)![0]));
return matches.map((x) => {
return {
// biome-ignore lint/style/noNonNullAssertion: Matches to a singular match
matchKey: x.matches!.key,
// biome-ignore lint/style/noNonNullAssertion: The match should be guaranteed to map to a singular event
eventKey: x.matches!.event,
// biome-ignore lint/style/noNonNullAssertion: see above
eventName: x.matches!.events!.name,
// biome-ignore lint/style/noNonNullAssertion: see above
team: parseInt(x.team.match(/\d+/)![0]),
red: redTeams,
blue: blueTeams,
assignee: x.assignee,
};
});
});
// return byMatch.map(
// ({
// matches,
// events,
// team,
const byMatch = groupBy(
data,
({ matches }) =>
// biome-ignore lint/style/noNonNullAssertion: <explanation>
matches!.key,
);
return Object.values(byMatch).map((matches) => {
// Keys are guaranteed to be in the form of "frcXXXX" where XXXX is a number
const redTeams = matches
.filter((team) => team.alliance.startsWith("red"))
// biome-ignore lint/style/noNonNullAssertion: see above
.map((x) => parseInt(x.team.match(/\d+/)![0]));
const blueTeams = matches
.filter((team) => team.alliance.startsWith("blue"))
// biome-ignore lint/style/noNonNullAssertion: see above
.map((x) => parseInt(x.team.match(/\d+/)![0]));
return matches.map((x) => {
return {
// biome-ignore lint/style/noNonNullAssertion: Matches to a singular match
matchKey: x.matches!.key,
// biome-ignore lint/style/noNonNullAssertion: The match should be guaranteed to map to a singular event
eventKey: x.matches!.event,
// biome-ignore lint/style/noNonNullAssertion: see above
eventName: x.matches!.events!.name,
// biome-ignore lint/style/noNonNullAssertion: see above
team: parseInt(x.team.match(/\d+/)![0]),
red: redTeams,
blue: blueTeams,
assignee: x.assignee,
};
});
});
// return byMatch.map(
// ({
// matches,
// events,
// team,

// alliance,
// }) => {
//
// return { team: };
// },
// );

// return ctx.db
// .select()
Expand All @@ -114,65 +121,43 @@ export const scoutingRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
const { error } = await ctx.supabase
.from("assigmments")
const { data, error } = await ctx.supabase
.from("assignments")
.update({ event_log: input.eventLog })
.eq("match", input.matchKey)
.eq("team", input.team);
.eq("team", input.team)
.select();
if (error !== null) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error updating match log",
cause: error,
});
}

updateMatchLog: publicProcedure
.input(
z.object({
eventLog,
matchKey: z.string(),
team: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
const { data, error } = await ctx.supabase
.from("assignments")
.update({ event_log: input.eventLog })
.eq("match", input.matchKey)
.eq("team", input.team)
.select();
if (error !== null) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error updating match log",
cause: error,
});
}
console.log(
"RPC: updateMatchLog (db returned data, error, input):",
data,
error,
input,
);
// TODO: Actually return somthing for data
// .where(eq(matches.teamNum, input.teamNum));
}),
getMatchLog: publicProcedure
.input(z.object({ matchKey: z.string(), team: z.string() }))
.query(async ({ ctx, input }) => {
const { data: assignments, error } = await ctx.supabase
.from("assignments")
.select("event_log")
.eq("match", input.matchKey)
.eq("team", input.team);
if (error !== null) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error fetching match log",
cause: error,
});
}
return assignments;
}),
console.log(
"RPC: updateMatchLog (db returned data, error, input):",
data,
error,
input,
);
// TODO: Actually return somthing for data
// .where(eq(matches.teamNum, input.teamNum));
}),
getMatchLog: publicProcedure
.input(z.object({ matchKey: z.string(), team: z.string() }))
.query(async ({ ctx, input }) => {
const { data: assignments, error } = await ctx.supabase
.from("assignments")
.select("event_log")
.eq("match", input.matchKey)
.eq("team", input.team);
if (error !== null) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error fetching match log",
cause: error,
});
}
return assignments;
}),
});
Loading

0 comments on commit c2212ad

Please sign in to comment.