From eb58b03a20b7d2d291e506e61214b7ce46d23bd9 Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Wed, 17 Sep 2025 17:38:06 +0200 Subject: [PATCH 01/11] feat: :sparkles: start making call to translate from assistant application to team application --- src/db-access/applications.ts | 5 ++++ src/routers/applications.ts | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index b2a7893..d18a4ff 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -6,6 +6,7 @@ import { import type { OrmResult } from "@/src/error/orm-error"; import type { NewApplication, + NewAssistantApplication, NewTeamApplication, } from "@/src/request-handling/applications"; import type { QueryParameters } from "@/src/request-handling/common"; @@ -145,3 +146,7 @@ export async function insertTeamApplication( }; }); } + +export async function createTeamApplicationFromAssistantApplication( + +) diff --git a/src/routers/applications.ts b/src/routers/applications.ts index d2a2c33..c079833 100644 --- a/src/routers/applications.ts +++ b/src/routers/applications.ts @@ -1,5 +1,7 @@ import { + createTeamApplicationFromAssistantApplication, insertTeamApplication, + makeTeamApplicationFromAssistantApplication, selectTeamApplications, selectTeamApplicationsByTeamId, } from "@/src/db-access/applications"; @@ -147,3 +149,51 @@ teamApplicationRouter.post("/", async (req, res, next) => { } res.status(201).json(databaseResult.data); }); + +/** + * @openapi + * /teamapplications/: + * post: + * tags: [teamapplications] + * summary: Make teamapplication from assistantapplication + * description: Make teamapplication from assistantapplication + * requestBody: + * required: true + * content: + * json: + * schema: + * $ref: "#/components/schemas/teamApplicationRequest" + * responses: + * 201: + * description: Successfull response + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/teamApplication" + */ +teamApplicationRouter.post("/", async (req, res, next) => { + const teamApplicationBodyResult = teamApplicationToInsertParser.safeParse( + req.body, + ); + + if (!teamApplicationBodyResult.success) { + const error = clientError( + 400, + "Invalid request format", + teamApplicationBodyResult.error, + ); + return next(error); + } + const databaseResult = await createTeamApplicationFromAssistantApplication( + teamApplicationBodyResult.data, + ); + if (!databaseResult.success) { + const error = clientError( + 400, + "Failed to execute the database command", + databaseResult.error, + ); + return next(error); + } + res.status(201).json(databaseResult.data); +}); From 415afe9a020822eaa52ee7e6b2949a475f713902 Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Thu, 18 Sep 2025 18:32:51 +0200 Subject: [PATCH 02/11] made composite key for team application table, issue: vf-319 --- db/tables/applications.ts | 10 +++++++--- src/db-access/applications.ts | 25 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/db/tables/applications.ts b/db/tables/applications.ts index 1082ed2..43222cb 100644 --- a/db/tables/applications.ts +++ b/db/tables/applications.ts @@ -1,6 +1,6 @@ import { mainSchema } from "@/db/tables/schema"; import { relations } from "drizzle-orm"; -import { date, integer, serial, text } from "drizzle-orm/pg-core"; +import { boolean, date, integer, primaryKey, serial, text } from "drizzle-orm/pg-core"; import { teamsTable } from "@/db/tables/teams"; import { fieldsOfStudyTable } from "./fields-of-study"; @@ -41,7 +41,8 @@ export const applicationsRelations = relations( ); export const teamApplicationsTable = mainSchema.table("teamApplications", { - id: integer("id") + id: serial("id"), + applicationParentId: integer("id") .primaryKey() .references(() => applicationsTable.id), teamId: integer("teamId") @@ -49,7 +50,10 @@ export const teamApplicationsTable = mainSchema.table("teamApplications", { .references(() => teamsTable.id), motivationText: text("motivationText").notNull(), biography: text("biography").notNull(), -}); + teamInterest: boolean("teamInterest").notNull(), +}, (table) => ({ + primaryKey: primaryKey({ columns: [table.id, table.applicationParentId]}) +})); export const teamApplicationsRelations = relations( teamApplicationsTable, diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index d18a4ff..e4dc019 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -137,6 +137,7 @@ export async function insertTeamApplication( teamId: teamApplication.teamId, motivationText: teamApplication.motivationText, biography: teamApplication.biography, + teamInterest: teamApplication.teamInterest, }) .returning(); @@ -148,5 +149,25 @@ export async function insertTeamApplication( } export async function createTeamApplicationFromAssistantApplication( - -) + assistantApplicationId: Number, + teamId: Number +): Promise> { + return await newDatabaseTransaction(database, async (tx) => { + // TODO: generate id + + const newTeamApplicationResult = await tx + .insert(teamApplicationsTable) + .values({ + id: assistantApplicationId, + teamId: teamId, + motivationText: null, + biography: null, + teamInterest: null, + }) + .returning(); + + return { + ...newTeamApplicationResult[0], + }; + }); +} From 73375abe555412414b96903bb4ed0c39895b9ce4 Mon Sep 17 00:00:00 2001 From: solvhold Date: Thu, 18 Sep 2025 19:03:38 +0200 Subject: [PATCH 03/11] fix: fix following faults from issue vf-319 (composite key in teamapplication table) vf-319, vf-295 --- src/db-access/applications.ts | 20 ++++++++++++-------- src/response-handling/applications.ts | 5 ++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index e4dc019..66eb5bb 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -25,6 +25,7 @@ export const selectTeamApplications = async ( const teamApplications = await tx .select({ id: applicationsTable.id, + applicationParentId: teamApplicationsTable.applicationParentId, teamId: teamApplicationsTable.teamId, firstName: applicationsTable.firstName, lastName: applicationsTable.lastName, @@ -35,12 +36,13 @@ export const selectTeamApplications = async ( phonenumber: applicationsTable.phonenumber, motivationText: teamApplicationsTable.motivationText, biography: teamApplicationsTable.biography, + teamInterest: teamApplicationsTable.teamInterest, submitDate: applicationsTable.submitDate, }) .from(teamApplicationsTable) .innerJoin( applicationsTable, - eq(teamApplicationsTable.id, applicationsTable.id), + eq(teamApplicationsTable.applicationParentId, applicationsTable.id), ) .limit(parameters.limit) .offset(parameters.offset); @@ -57,6 +59,7 @@ export const selectTeamApplicationsByTeamId = async ( const selectResult = await tx .select({ id: applicationsTable.id, + applicationParentId: teamApplicationsTable.applicationParentId, teamId: teamApplicationsTable.teamId, firstName: applicationsTable.firstName, lastName: applicationsTable.lastName, @@ -67,13 +70,14 @@ export const selectTeamApplicationsByTeamId = async ( phonenumber: applicationsTable.phonenumber, motivationText: teamApplicationsTable.motivationText, biography: teamApplicationsTable.biography, + teamInterest: teamApplicationsTable.teamInterest, submitDate: applicationsTable.submitDate, }) .from(teamApplicationsTable) .where(inArray(teamApplicationsTable.id, teamId)) .innerJoin( applicationsTable, - eq(teamApplicationsTable.id, applicationsTable.id), + eq(teamApplicationsTable.applicationParentId, applicationsTable.id), ) .limit(parameters.limit) .offset(parameters.offset); @@ -89,6 +93,7 @@ export const selectTeamApplicationsById = async ( const selectResult = await tx .select({ id: applicationsTable.id, + applicationParentId: teamApplicationsTable.applicationParentId, teamId: teamApplicationsTable.teamId, firstName: applicationsTable.firstName, lastName: applicationsTable.lastName, @@ -99,13 +104,14 @@ export const selectTeamApplicationsById = async ( phonenumber: applicationsTable.phonenumber, motivationText: teamApplicationsTable.motivationText, biography: teamApplicationsTable.biography, + teamInterest: teamApplicationsTable.teamInterest, submitDate: applicationsTable.submitDate, }) .from(teamApplicationsTable) .where(inArray(teamApplicationsTable.id, applicationIds)) .innerJoin( applicationsTable, - eq(teamApplicationsTable.id, applicationsTable.id), + eq(teamApplicationsTable.applicationParentId, applicationsTable.id), ); return selectResult; @@ -133,7 +139,7 @@ export async function insertTeamApplication( const newTeamApplicationResult = await tx .insert(teamApplicationsTable) .values({ - id: newApplicationId, + applicationParentId: newApplicationId, teamId: teamApplication.teamId, motivationText: teamApplication.motivationText, biography: teamApplication.biography, @@ -158,7 +164,7 @@ export async function createTeamApplicationFromAssistantApplication( const newTeamApplicationResult = await tx .insert(teamApplicationsTable) .values({ - id: assistantApplicationId, + applicationParentId: assistantApplicationId, teamId: teamId, motivationText: null, biography: null, @@ -166,8 +172,6 @@ export async function createTeamApplicationFromAssistantApplication( }) .returning(); - return { - ...newTeamApplicationResult[0], - }; + return newTeamApplicationResult }); } diff --git a/src/response-handling/applications.ts b/src/response-handling/applications.ts index fa09a77..fdc649b 100644 --- a/src/response-handling/applications.ts +++ b/src/response-handling/applications.ts @@ -21,5 +21,8 @@ export const teamApplicationSelectSchema = createSelectSchema( .readonly(); export type TeamApplication = z.infer; -export type TeamApplicationKey = TeamApplication["id"]; +export type TeamApplicationKey = { + id: TeamApplication["id"], + applicationParentId: TeamApplication["applicationParentId"], +}; export type TeamKey = TeamApplication["teamId"]; From 47c9f2d4022deec63a1adf203e32b9e5d9b2781b Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Mon, 29 Sep 2025 17:44:06 +0200 Subject: [PATCH 04/11] fix: :bug: fixed following error --- db/tables/applications.ts | 4 ++-- src/db-access/applications.ts | 34 ++++++++++++++++++++++------------ src/routers/applications.ts | 6 ++++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/db/tables/applications.ts b/db/tables/applications.ts index 43222cb..433cb7d 100644 --- a/db/tables/applications.ts +++ b/db/tables/applications.ts @@ -48,8 +48,8 @@ export const teamApplicationsTable = mainSchema.table("teamApplications", { teamId: integer("teamId") .notNull() .references(() => teamsTable.id), - motivationText: text("motivationText").notNull(), - biography: text("biography").notNull(), + motivationText: text("motivationText"), + biography: text("biography"), teamInterest: boolean("teamInterest").notNull(), }, (table) => ({ primaryKey: primaryKey({ columns: [table.id, table.applicationParentId]}) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index 66eb5bb..fd3e350 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -3,7 +3,7 @@ import { applicationsTable, teamApplicationsTable, } from "@/db/tables/applications"; -import type { OrmResult } from "@/src/error/orm-error"; +import { ormError, type OrmResult } from "@/src/error/orm-error"; import type { NewApplication, NewAssistantApplication, @@ -15,7 +15,7 @@ import type { TeamApplication, TeamKey, } from "@/src/response-handling/applications"; -import { eq, inArray } from "drizzle-orm"; +import { and, eq, inArray } from "drizzle-orm"; import { newDatabaseTransaction } from "./common"; export const selectTeamApplications = async ( @@ -88,6 +88,7 @@ export const selectTeamApplicationsByTeamId = async ( export const selectTeamApplicationsById = async ( applicationIds: ApplicationKey[], + teamApplicationIds: number[], ): Promise> => { return await newDatabaseTransaction(database, async (tx) => { const selectResult = await tx @@ -108,7 +109,7 @@ export const selectTeamApplicationsById = async ( submitDate: applicationsTable.submitDate, }) .from(teamApplicationsTable) - .where(inArray(teamApplicationsTable.id, applicationIds)) + .where(and(inArray(teamApplicationsTable.applicationParentId, applicationIds), inArray(teamApplicationsTable.id, teamApplicationIds))) .innerJoin( applicationsTable, eq(teamApplicationsTable.applicationParentId, applicationsTable.id), @@ -155,23 +156,32 @@ export async function insertTeamApplication( } export async function createTeamApplicationFromAssistantApplication( - assistantApplicationId: Number, - teamId: Number -): Promise> { + assistantApplicationId: number, + teamId: number, + biography: string|null|undefined, // Wasn't able to make biography and motivationText fields unable to be undefined + motivationText: string|null|undefined, +): Promise> { return await newDatabaseTransaction(database, async (tx) => { - // TODO: generate id const newTeamApplicationResult = await tx .insert(teamApplicationsTable) .values({ - applicationParentId: assistantApplicationId, + applicationParentId: 0, teamId: teamId, - motivationText: null, - biography: null, - teamInterest: null, + motivationText: motivationText, + biography: biography, + teamInterest: true, }) .returning(); - return newTeamApplicationResult + const teamApplicationResult = await selectTeamApplicationsById([assistantApplicationId], [newTeamApplicationResult[0].id]); + if (!teamApplicationResult.success){ + throw ormError( + "Transaction failed", + teamApplicationResult.error + ) + } + + return teamApplicationResult.data; }); } diff --git a/src/routers/applications.ts b/src/routers/applications.ts index c079833..633a9d7 100644 --- a/src/routers/applications.ts +++ b/src/routers/applications.ts @@ -1,7 +1,6 @@ import { createTeamApplicationFromAssistantApplication, insertTeamApplication, - makeTeamApplicationFromAssistantApplication, selectTeamApplications, selectTeamApplicationsByTeamId, } from "@/src/db-access/applications"; @@ -185,7 +184,10 @@ teamApplicationRouter.post("/", async (req, res, next) => { return next(error); } const databaseResult = await createTeamApplicationFromAssistantApplication( - teamApplicationBodyResult.data, + teamApplicationBodyResult.data.applicationParentId, + teamApplicationBodyResult.data.teamId, + teamApplicationBodyResult.data.biography, + teamApplicationBodyResult.data.motivationText, ); if (!databaseResult.success) { const error = clientError( From b39355b7dce8fefe608c88d126ef163dc39f0722 Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Mon, 29 Sep 2025 18:55:58 +0200 Subject: [PATCH 05/11] fix: :bug: fixed issue that we had 2 id fields --- db/tables/applications.ts | 3 +-- src/routers/applications.ts | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/db/tables/applications.ts b/db/tables/applications.ts index 433cb7d..ac5caef 100644 --- a/db/tables/applications.ts +++ b/db/tables/applications.ts @@ -42,8 +42,7 @@ export const applicationsRelations = relations( export const teamApplicationsTable = mainSchema.table("teamApplications", { id: serial("id"), - applicationParentId: integer("id") - .primaryKey() + applicationParentId: integer("applicationParentId") .references(() => applicationsTable.id), teamId: integer("teamId") .notNull() diff --git a/src/routers/applications.ts b/src/routers/applications.ts index 633a9d7..36721fc 100644 --- a/src/routers/applications.ts +++ b/src/routers/applications.ts @@ -151,7 +151,7 @@ teamApplicationRouter.post("/", async (req, res, next) => { /** * @openapi - * /teamapplications/: + * /teamapplications/createFromAssistantApplication: * post: * tags: [teamapplications] * summary: Make teamapplication from assistantapplication @@ -170,7 +170,7 @@ teamApplicationRouter.post("/", async (req, res, next) => { * schema: * $ref: "#/components/schemas/teamApplication" */ -teamApplicationRouter.post("/", async (req, res, next) => { +teamApplicationRouter.post("/createFromAssistantApplication/", async (req, res, next) => { const teamApplicationBodyResult = teamApplicationToInsertParser.safeParse( req.body, ); From 563f72013326a6e20926e21420a0759f65172963 Mon Sep 17 00:00:00 2001 From: solvhold Date: Wed, 8 Oct 2025 17:16:51 +0200 Subject: [PATCH 06/11] feat: create parser for teaminterest vf-295 --- src/request-handling/applications.ts | 7 +++++++ src/routers/applications.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/request-handling/applications.ts b/src/request-handling/applications.ts index 57e19a7..45bfe65 100644 --- a/src/request-handling/applications.ts +++ b/src/request-handling/applications.ts @@ -86,6 +86,13 @@ export const assistantApplicationToInsertParser = assistantApplicationParser .readonly(), ); +export const teamInterestParser = z.object({ + applicationParentId: serialIdParser, + teamId: teamApplicationParser.shape.teamId, + biography: teamApplicationParser.shape.biography.nullable(), + motivationText: teamApplicationParser.shape.motivationText.nullable(), +}); + export type NewApplication = z.infer; export type NewTeamApplication = z.infer; export type NewAssistantApplication = z.infer< diff --git a/src/routers/applications.ts b/src/routers/applications.ts index 36721fc..2c8b2a2 100644 --- a/src/routers/applications.ts +++ b/src/routers/applications.ts @@ -5,7 +5,7 @@ import { selectTeamApplicationsByTeamId, } from "@/src/db-access/applications"; import { clientError } from "@/src/error/http-errors"; -import { teamApplicationToInsertParser } from "@/src/request-handling/applications"; +import { teamApplicationToInsertParser, teamInterestParser } from "@/src/request-handling/applications"; import { toListQueryParser, toSerialIdParser, @@ -171,7 +171,7 @@ teamApplicationRouter.post("/", async (req, res, next) => { * $ref: "#/components/schemas/teamApplication" */ teamApplicationRouter.post("/createFromAssistantApplication/", async (req, res, next) => { - const teamApplicationBodyResult = teamApplicationToInsertParser.safeParse( + const teamApplicationBodyResult = teamInterestParser.safeParse( req.body, ); From d02f2d65f61b44e5a14181b95656ea5987cb1ee8 Mon Sep 17 00:00:00 2001 From: solvhold Date: Wed, 8 Oct 2025 17:25:17 +0200 Subject: [PATCH 07/11] fix: fix small writing error vf-295 --- src/db-access/applications.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index fd3e350..d3d1fa1 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -166,7 +166,7 @@ export async function createTeamApplicationFromAssistantApplication( const newTeamApplicationResult = await tx .insert(teamApplicationsTable) .values({ - applicationParentId: 0, + applicationParentId: assistantApplicationId, teamId: teamId, motivationText: motivationText, biography: biography, From 606d25b6e862ef17120b707679a3b46ac26c73a2 Mon Sep 17 00:00:00 2001 From: solvhold Date: Wed, 8 Oct 2025 17:31:26 +0200 Subject: [PATCH 08/11] fix: fix small error vf-295 --- src/db-access/applications.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index d3d1fa1..cee9c74 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -93,7 +93,7 @@ export const selectTeamApplicationsById = async ( return await newDatabaseTransaction(database, async (tx) => { const selectResult = await tx .select({ - id: applicationsTable.id, + id: teamApplicationsTable.id, applicationParentId: teamApplicationsTable.applicationParentId, teamId: teamApplicationsTable.teamId, firstName: applicationsTable.firstName, From c33f2f480d0b583112a6cca003f6f040a0bdece2 Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Wed, 8 Oct 2025 18:12:59 +0200 Subject: [PATCH 09/11] style: :art: pnpm check --- db/tables/applications.ts | 40 +++++++++++------- src/db-access/applications.ts | 27 ++++++------ src/response-handling/applications.ts | 4 +- src/routers/applications.ts | 60 ++++++++++++++------------- 4 files changed, 75 insertions(+), 56 deletions(-) diff --git a/db/tables/applications.ts b/db/tables/applications.ts index ac5caef..cbf3d49 100644 --- a/db/tables/applications.ts +++ b/db/tables/applications.ts @@ -1,6 +1,13 @@ import { mainSchema } from "@/db/tables/schema"; import { relations } from "drizzle-orm"; -import { boolean, date, integer, primaryKey, serial, text } from "drizzle-orm/pg-core"; +import { + boolean, + date, + integer, + primaryKey, + serial, + text, +} from "drizzle-orm/pg-core"; import { teamsTable } from "@/db/tables/teams"; import { fieldsOfStudyTable } from "./fields-of-study"; @@ -40,19 +47,24 @@ export const applicationsRelations = relations( }), ); -export const teamApplicationsTable = mainSchema.table("teamApplications", { - id: serial("id"), - applicationParentId: integer("applicationParentId") - .references(() => applicationsTable.id), - teamId: integer("teamId") - .notNull() - .references(() => teamsTable.id), - motivationText: text("motivationText"), - biography: text("biography"), - teamInterest: boolean("teamInterest").notNull(), -}, (table) => ({ - primaryKey: primaryKey({ columns: [table.id, table.applicationParentId]}) -})); +export const teamApplicationsTable = mainSchema.table( + "teamApplications", + { + id: serial("id"), + applicationParentId: integer("applicationParentId").references( + () => applicationsTable.id, + ), + teamId: integer("teamId") + .notNull() + .references(() => teamsTable.id), + motivationText: text("motivationText"), + biography: text("biography"), + teamInterest: boolean("teamInterest").notNull(), + }, + (table) => ({ + primaryKey: primaryKey({ columns: [table.id, table.applicationParentId] }), + }), +); export const teamApplicationsRelations = relations( teamApplicationsTable, diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index cee9c74..9662098 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -3,10 +3,9 @@ import { applicationsTable, teamApplicationsTable, } from "@/db/tables/applications"; -import { ormError, type OrmResult } from "@/src/error/orm-error"; +import { type OrmResult, ormError } from "@/src/error/orm-error"; import type { NewApplication, - NewAssistantApplication, NewTeamApplication, } from "@/src/request-handling/applications"; import type { QueryParameters } from "@/src/request-handling/common"; @@ -109,7 +108,12 @@ export const selectTeamApplicationsById = async ( submitDate: applicationsTable.submitDate, }) .from(teamApplicationsTable) - .where(and(inArray(teamApplicationsTable.applicationParentId, applicationIds), inArray(teamApplicationsTable.id, teamApplicationIds))) + .where( + and( + inArray(teamApplicationsTable.applicationParentId, applicationIds), + inArray(teamApplicationsTable.id, teamApplicationIds), + ), + ) .innerJoin( applicationsTable, eq(teamApplicationsTable.applicationParentId, applicationsTable.id), @@ -158,11 +162,10 @@ export async function insertTeamApplication( export async function createTeamApplicationFromAssistantApplication( assistantApplicationId: number, teamId: number, - biography: string|null|undefined, // Wasn't able to make biography and motivationText fields unable to be undefined - motivationText: string|null|undefined, + biography: string | null | undefined, // Wasn't able to make biography and motivationText fields unable to be undefined + motivationText: string | null | undefined, ): Promise> { return await newDatabaseTransaction(database, async (tx) => { - const newTeamApplicationResult = await tx .insert(teamApplicationsTable) .values({ @@ -174,12 +177,12 @@ export async function createTeamApplicationFromAssistantApplication( }) .returning(); - const teamApplicationResult = await selectTeamApplicationsById([assistantApplicationId], [newTeamApplicationResult[0].id]); - if (!teamApplicationResult.success){ - throw ormError( - "Transaction failed", - teamApplicationResult.error - ) + const teamApplicationResult = await selectTeamApplicationsById( + [assistantApplicationId], + [newTeamApplicationResult[0].id], + ); + if (!teamApplicationResult.success) { + throw ormError("Transaction failed", teamApplicationResult.error); } return teamApplicationResult.data; diff --git a/src/response-handling/applications.ts b/src/response-handling/applications.ts index fdc649b..517a677 100644 --- a/src/response-handling/applications.ts +++ b/src/response-handling/applications.ts @@ -22,7 +22,7 @@ export const teamApplicationSelectSchema = createSelectSchema( export type TeamApplication = z.infer; export type TeamApplicationKey = { - id: TeamApplication["id"], - applicationParentId: TeamApplication["applicationParentId"], + id: TeamApplication["id"]; + applicationParentId: TeamApplication["applicationParentId"]; }; export type TeamKey = TeamApplication["teamId"]; diff --git a/src/routers/applications.ts b/src/routers/applications.ts index 2c8b2a2..f58af8c 100644 --- a/src/routers/applications.ts +++ b/src/routers/applications.ts @@ -5,7 +5,10 @@ import { selectTeamApplicationsByTeamId, } from "@/src/db-access/applications"; import { clientError } from "@/src/error/http-errors"; -import { teamApplicationToInsertParser, teamInterestParser } from "@/src/request-handling/applications"; +import { + teamApplicationToInsertParser, + teamInterestParser, +} from "@/src/request-handling/applications"; import { toListQueryParser, toSerialIdParser, @@ -170,32 +173,33 @@ teamApplicationRouter.post("/", async (req, res, next) => { * schema: * $ref: "#/components/schemas/teamApplication" */ -teamApplicationRouter.post("/createFromAssistantApplication/", async (req, res, next) => { - const teamApplicationBodyResult = teamInterestParser.safeParse( - req.body, - ); +teamApplicationRouter.post( + "/createFromAssistantApplication/", + async (req, res, next) => { + const teamApplicationBodyResult = teamInterestParser.safeParse(req.body); - if (!teamApplicationBodyResult.success) { - const error = clientError( - 400, - "Invalid request format", - teamApplicationBodyResult.error, - ); - return next(error); - } - const databaseResult = await createTeamApplicationFromAssistantApplication( - teamApplicationBodyResult.data.applicationParentId, - teamApplicationBodyResult.data.teamId, - teamApplicationBodyResult.data.biography, - teamApplicationBodyResult.data.motivationText, - ); - if (!databaseResult.success) { - const error = clientError( - 400, - "Failed to execute the database command", - databaseResult.error, + if (!teamApplicationBodyResult.success) { + const error = clientError( + 400, + "Invalid request format", + teamApplicationBodyResult.error, + ); + return next(error); + } + const databaseResult = await createTeamApplicationFromAssistantApplication( + teamApplicationBodyResult.data.applicationParentId, + teamApplicationBodyResult.data.teamId, + teamApplicationBodyResult.data.biography, + teamApplicationBodyResult.data.motivationText, ); - return next(error); - } - res.status(201).json(databaseResult.data); -}); + if (!databaseResult.success) { + const error = clientError( + 400, + "Failed to execute the database command", + databaseResult.error, + ); + return next(error); + } + res.status(201).json(databaseResult.data); + }, +); From d1708d32c6d59ef0d496e19dab9c4cb7e91f2d3f Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Wed, 8 Oct 2025 18:31:49 +0200 Subject: [PATCH 10/11] feat: :sparkles: made function that creates application use z.infer as input type --- src/db-access/applications.ts | 18 ++++++++---------- src/request-handling/applications.ts | 1 + src/routers/applications.ts | 5 +---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index 9662098..6a282e5 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -7,6 +7,7 @@ import { type OrmResult, ormError } from "@/src/error/orm-error"; import type { NewApplication, NewTeamApplication, + NewTeamInterestApplication, } from "@/src/request-handling/applications"; import type { QueryParameters } from "@/src/request-handling/common"; import type { @@ -160,25 +161,22 @@ export async function insertTeamApplication( } export async function createTeamApplicationFromAssistantApplication( - assistantApplicationId: number, - teamId: number, - biography: string | null | undefined, // Wasn't able to make biography and motivationText fields unable to be undefined - motivationText: string | null | undefined, + teamInterestApplication: NewTeamInterestApplication, ): Promise> { return await newDatabaseTransaction(database, async (tx) => { const newTeamApplicationResult = await tx .insert(teamApplicationsTable) .values({ - applicationParentId: assistantApplicationId, - teamId: teamId, - motivationText: motivationText, - biography: biography, + applicationParentId: teamInterestApplication.applicationParentId, + teamId: teamInterestApplication.teamId, + motivationText: teamInterestApplication.motivationText, + biography: teamInterestApplication.biography, teamInterest: true, }) .returning(); const teamApplicationResult = await selectTeamApplicationsById( - [assistantApplicationId], + [teamInterestApplication.applicationParentId], [newTeamApplicationResult[0].id], ); if (!teamApplicationResult.success) { @@ -187,4 +185,4 @@ export async function createTeamApplicationFromAssistantApplication( return teamApplicationResult.data; }); -} +} \ No newline at end of file diff --git a/src/request-handling/applications.ts b/src/request-handling/applications.ts index 45bfe65..101c6e2 100644 --- a/src/request-handling/applications.ts +++ b/src/request-handling/applications.ts @@ -98,3 +98,4 @@ export type NewTeamApplication = z.infer; export type NewAssistantApplication = z.infer< typeof assistantApplicationToInsertParser >; +export type NewTeamInterestApplication = z.infer; diff --git a/src/routers/applications.ts b/src/routers/applications.ts index f58af8c..e517927 100644 --- a/src/routers/applications.ts +++ b/src/routers/applications.ts @@ -187,10 +187,7 @@ teamApplicationRouter.post( return next(error); } const databaseResult = await createTeamApplicationFromAssistantApplication( - teamApplicationBodyResult.data.applicationParentId, - teamApplicationBodyResult.data.teamId, - teamApplicationBodyResult.data.biography, - teamApplicationBodyResult.data.motivationText, + teamApplicationBodyResult.data, ); if (!databaseResult.success) { const error = clientError( From 9a73d0f4d752525c0a4278d97aed0507e9a9f3df Mon Sep 17 00:00:00 2001 From: TobiasH05 Date: Wed, 8 Oct 2025 18:32:20 +0200 Subject: [PATCH 11/11] feat: :art: pnpm check --- src/db-access/applications.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db-access/applications.ts b/src/db-access/applications.ts index 6a282e5..2fa95b2 100644 --- a/src/db-access/applications.ts +++ b/src/db-access/applications.ts @@ -185,4 +185,4 @@ export async function createTeamApplicationFromAssistantApplication( return teamApplicationResult.data; }); -} \ No newline at end of file +}