Skip to content

feat: implement expense creation functionality with validation#24

Open
RishikaBothra wants to merge 10 commits into
KensiPatel:mainfrom
RishikaBothra:feat/create-expense
Open

feat: implement expense creation functionality with validation#24
RishikaBothra wants to merge 10 commits into
KensiPatel:mainfrom
RishikaBothra:feat/create-expense

Conversation

@RishikaBothra
Copy link
Copy Markdown
Collaborator

closes #23

@RishikaBothra RishikaBothra self-assigned this Feb 28, 2026
Comment thread back_end/src/db/repositories/expense_repository.ts Outdated
Comment on lines +6 to +21
export const createExpense = async ({
title,
description,
amount,
userId,
}: CreateExpenseInput) => {
await db().insert(expense).values({
title,
description,
amount,
userId,
status: "draft",
});

return { msg: "status: draft" };
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return promise

Comment on lines +23 to +36
export const getExpenseById = async (id: number) => {
const result = await db()
.select({
id: expense.id,
userId: expense.userId,
title: expense.title,
amount: expense.amount,
status: expense.status,
})
.from(expense)
.where(eq(expense.id, id));

return result[0];
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return promise

Comment on lines +38 to +44
export const updateExpenseStatus = async (id: number, status: "draft" | "submitted" | "accepted" | "rejected") => {
await db()
.update(expense)
.set({ status,
updatedAt: new Date() })
.where(eq(expense.id, id));
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return promise

Comment on lines +94 to +112
export const deleteExpenseService = async (expenseId: number, userId: number) => {
const expenseData = await getExpenseById(expenseId);

if (!expenseData) {
throw new Error("Expense not found");
}

if (expenseData.userId !== userId) {
throw new Error("Unauthorized to delete this expense");
}

if (expenseData.status !== "draft") {
throw new Error("Only draft expenses can be deleted");
}

await deleteExpense(expenseId);

return;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for deleting you don't need to perform two database operations


export const getExpensesByUserId = async (userId: number) => {
const result = await db()
.select()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not select *
select exactly what's needed

return;
};

export const updateExpenseService = async (expenseId: number, userId: number, data: Partial<CreateExpenseInput>) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some redundant input validation checks.
if zod is already doing it at the route level, you don't need to do it here again

Comment on lines +31 to +49
export const submitExpenseService = async (expenseId: number, userId: number) => {
const expenseData = await getExpenseById(expenseId);

if (!expenseData) {
throw new Error("Expense not found");
}

if (expenseData.userId !== userId) {
throw new Error("Unauthorized to submit this expense");
}

if (expenseData.status !== "draft") {
throw new Error("Only draft expenses can be submitted");
}

await updateExpenseStatus(expenseId, "submitted");

return;
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please research it this the best way of doing this?
can we do it but without multiple times querying the database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: implement create expense endpoint

2 participants